Skip to content

Build and Push Docker image #70

Build and Push Docker image

Build and Push Docker image #70

name: Build and Push Docker image
env:
PHP_VERSION: 8.3
BASE_IMAGE: wordpress:php8.3-fpm-alpine
OWN_IMAGE: ghcr.io/hueske-digital/wordpress:latest
on:
push:
branches:
- main
schedule:
- cron: '0 1 * * *'
workflow_dispatch:
inputs:
force_build:
description: 'Set to "true" to force rebuild and push the Docker image regardless of changes'
required: false
default: 'false'
jobs:
build:
permissions:
contents: write
packages: write
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitLab Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Pull Base Image for Comparison
run: docker pull ${{ env.BASE_IMAGE }} || true
- name: Pull Last Built Image for Comparison
run: docker pull ${{ env.OWN_IMAGE }} || true
- name: Check if base image has changed
id: check_layer
run: |
layers_image1=$(docker inspect --format='{{range .RootFS.Layers}}{{.}} {{end}}' ${{ env.BASE_IMAGE }})
layers_image2=$(docker inspect --format='{{range .RootFS.Layers}}{{.}} {{end}}' ${{ env.OWN_IMAGE }})
last_layer_image1=$(echo $layers_image1 | awk '{print $NF}')
if echo $layers_image2 | grep -q $last_layer_image1; then
echo "image_changed=false" >> $GITHUB_ENV
echo "Der letzte Layer von ${{ env.BASE_IMAGE }} ist ein Bestandteil von ${{ env.OWN_IMAGE }}."
else
echo "image_changed=true" >> $GITHUB_ENV
echo "Der letzte Layer von ${{ env.BASE_IMAGE }} ist kein Bestandteil von ${{ env.OWN_IMAGE }}."
fi
if [ "${{ github.event.inputs.force_build }}" == "true" ]; then
echo "Force build requested."
echo "image_changed=true" >> $GITHUB_ENV
fi
- name: Build and push Docker image
if: env.image_changed == 'true'
uses: docker/build-push-action@v3
with:
push: true
context: ./build
tags: ${{ env.OWN_IMAGE }}
platforms: linux/amd64,linux/arm64
build-args: |
PHP_VERSION=${{ env.PHP_VERSION }}
- name: Do not automatically disable workflow execution
uses: gautamkrishnar/keepalive-workflow@v1