Skip to content

build-and-push-image #970

build-and-push-image

build-and-push-image #970

name: build-and-push-image
on:
schedule:
- cron: '32 16 * * *'
push:
branches: [ main ]
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Check if there is a new base image
if: github.event_name != 'push' && github.event_name != 'workflow_dispatch'
id: check
run: |
set -Eeuxo pipefail
PARENT=$(skopeo inspect docker://alpine:latest | jq .Layers)
OWN=$(skopeo inspect docker://ghcr.io/marius/postfix-sendgrid | jq .Layers)
OUTOFDATE=$(jq -cn "$OWN - ($OWN - $PARENT) | .==[]")
echo "out-of-date=$OUTOFDATE" >> $GITHUB_OUTPUT
# This action needs to run every time to set the package version.
- name: Get the postfix package version
id: check-package
run: |
set -Eeuxo pipefail
if [ ! -x pup ]; then
curl -L -o pup.zip https://github.com/ericchiang/pup/releases/download/v0.4.0/pup_v0.4.0_linux_amd64.zip
unzip pup.zip
chmod +x pup
fi
echo "7e285aef7140609dcfe94153d6ee412fd9ea3a5b804670e1c772a75150ab711d pup" | sha256sum --check --status
package_version=$(curl -s https://pkgs.alpinelinux.org/package/edge/main/x86_64/postfix | ./pup 'th:contains("Version") + td strong a text{}')
container_version=$(skopeo inspect docker://ghcr.io/marius/postfix-sendgrid | jq -r '.Labels."postfix-package-version"')
echo "version=${package_version}" >> $GITHUB_OUTPUT
if [ "${container_version}" == "${package_version}" ]; then
echo "new=false" >> $GITHUB_OUTPUT
else
echo "new=true" >> $GITHUB_OUTPUT
fi
- name: Checkout repository
if: steps.check.outputs.out-of-date == 'true' || steps.check-package.outputs.new == 'true' || github.event_name == 'push' || github.event_name == 'workflow_dispatch'
uses: actions/checkout@v4
- name: Extract Docker metadata
if: steps.check.outputs.out-of-date == 'true' || steps.check-package.outputs.new == 'true' || github.event_name == 'push' || github.event_name == 'workflow_dispatch'
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/marius/postfix-sendgrid
- name: Set up Docker Buildx
if: steps.check.outputs.out-of-date == 'true' || steps.check-package.outputs.new == 'true' || github.event_name == 'push' || github.event_name == 'workflow_dispatch'
uses: docker/setup-buildx-action@v3
- name: Log into registry
if: steps.check.outputs.out-of-date == 'true' || steps.check-package.outputs.new == 'true' || github.event_name == 'push' || github.event_name == 'workflow_dispatch'
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push Docker image
if: steps.check.outputs.out-of-date == 'true' || steps.check-package.outputs.new == 'true' || github.event_name == 'push' || github.event_name == 'workflow_dispatch'
id: build-and-push
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ghcr.io/marius/postfix-sendgrid:latest
labels: |
${{ steps.meta.outputs.labels }}
postfix-package-version=${{ steps.check-package.outputs.version }}