Skip to content

build-and-push-image #544

build-and-push-image

build-and-push-image #544

name: build-and-push-image
on:
schedule:
- cron: '32 16 * * *'
push:
branches: [ main ]
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build:
runs-on: ubuntu-22.04
permissions:
contents: read
packages: write
steps:
- name: Check if there is a new base image
if: github.event_name != 'push'
id: check
uses: twiddler/is-my-docker-parent-image-out-of-date@v1
with:
parent-image: alpine:latest
my-image: ghcr.io/marius/postfix-sendgrid
- name: Get the postfix package version
id: check-package
run: |
set -Eeuxo pipefail
sudo apt install -y curl jq unzip gnupg skopeo
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
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'
uses: actions/checkout@v3
- name: Log into registry
if: steps.check.outputs.out-of-date == 'true' || steps.check-package.outputs.new == 'true' || github.event_name == 'push'
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract Docker metadata
if: steps.check.outputs.out-of-date == 'true' || steps.check-package.outputs.new == 'true' || github.event_name == 'push'
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Build and push Docker image
if: steps.check.outputs.out-of-date == 'true' || steps.check-package.outputs.new == 'true' || github.event_name == 'push'
id: build-and-push
uses: docker/build-push-action@v3
with:
context: .
push: true
tags: ghcr.io/marius/postfix-sendgrid:latest
labels: |
${{ steps.meta.outputs.labels }}
postfix-package-version=${{ steps.check-package.outputs.version }}