Skip to content

Commit

Permalink
chore: publish images
Browse files Browse the repository at this point in the history
  • Loading branch information
Luc-cpl committed Jun 2, 2024
1 parent 3b11c13 commit c0a8b0c
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 1 deletion.
72 changes: 72 additions & 0 deletions .github/actions/image-publisher/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: "Publish to Docker"
description: "Pushes built artifacts to Docker"

inputs:
docker_username:
description: Username for Docker Hub account
required: true
docker_token:
description: Token for Docker Hub account
required: true
image_registry:
description: Registry to push images to
required: true
image_repository:
description: Repository to push images to
required: true
image_tag:
description: Tag to apply to images
required: true
image_latest:
description: Apply the latest tag to the image
required: true
context:
description: Path to Dockerfile
required: true
file:
description: Path to Dockerfile
required: true

runs:
using: "composite"
steps:
- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
with:
install: true

- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ inputs.docker_username }}
password: ${{ inputs.docker_token }}

- name: Prepare
id: prep
shell: bash
env:
REGISTRY: ${{ inputs.image_registry }}
REPOSITORY: ${{ inputs.image_repository }}
TAG: ${{ inputs.image_tag }}
run: |
IMAGE="$REGISTRY/$REPOSITORY"
echo "IMAGE=${IMAGE}" >> $GITHUB_OUTPUT
echo "TAG=${TAG}" >> $GITHUB_OUTPUT
echo "TAGGED_IMAGE=${IMAGE}:${TAG}" >> $GITHUB_OUTPUT
if [ "${{ inputs.image_latest }}" = "true" ]; then
echo "TAGGED_IMAGE=${IMAGE}:${TAG},${IMAGE}:latest" >> $GITHUB_OUTPUT
fi
- name: Build and push
uses: docker/build-push-action@v3
with:
push: true
context: ${{ inputs.context }}
file: ${{ inputs.file }}
platforms: linux/amd64, linux/arm64
tags: ${{ steps.prep.outputs.TAGGED_IMAGE }}
cache-from: type=gha
cache-to: type=gha,mode=max
86 changes: 86 additions & 0 deletions .github/workflows/docker_deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
name: Build and publish docker images
on:
push:
branches:
- main
- development # Remove after testing
jobs:
maybe_build:
runs-on: ubuntu-latest
strategy:
matrix:
image:
- wordpress-franken
- wordpress-cli
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Process vars
id: vars
run: |
IMAGE_DIR=./docker/${{ matrix.image }}
IMAGE_VERSION=$(grep -oP '(?<=ARG IMAGE_VERSION=).+' $IMAGE_DIR/Dockerfile)
if [[ -z "$IMAGE_VERSION" ]]; then
echo "No image version found in Dockerfile"
exit 0
fi
echo "IMAGE_REPOSITORY=${{ matrix.image }}" >> $GITHUB_ENV
echo "IMAGE_DIR=$IMAGE_DIR" >> $GITHUB_ENV
echo "IMAGE_VERSION=$IMAGE_VERSION" >> $GITHUB_ENV
- name: Get current version
id: current_version
if: steps.vars.outputs.IMAGE_VERSION
run: |
IMAGE_REPOSITORY=${{ steps.vars.outputs.IMAGE_REPOSITORY }}
REGISTRY=${{ secrets.DOCKERHUB_REGISTRY }}
# Get the jwt token from Docker Hub
TOKEN=$(curl -s -H "Content-Type: application/json" -X POST -d '{"username": "'${{ secrets.DOCKERHUB_USERNAME }}'", "password": "'${{ secrets.DOCKERHUB_TOKEN }}'"}' https://hub.docker.com/v2/users/login/ | jq -r .token)
# Get the tag latest from the registry
IMAGE_DIGEST=$(curl -s -H "Authorization: JWT $TOKEN" -H "Accept: application/json" https://hub.docker.com/v2/repositories/${REGISTRY}/${IMAGE_REPOSITORY}/tags/latest | jq -r '.digest')
echo "Image digest found: $IMAGE_DIGEST"
if [ -z "$IMAGE_DIGEST" ]; then
echo "No image digest found in Docker Hub. Assuming it's the first build."
echo "CURRENT_VERSION=0.0.0" >> $GITHUB_ENV
exit 0
fi
# Get the other tag with the same digest (the version)
DIGESTS=$(curl -s -H "Authorization: JWT $TOKEN" -H "Accept: application/json" https://hub.docker.com/v2/repositories/${REGISTRY}/${IMAGE_REPOSITORY}/tags | jq -r '.results[] | "\(.digest)$\(.name)"')
for DIGEST in $DIGESTS; do
CURRENT_VERSION=$(echo $DIGEST | cut -d$ -f2)
CURRENT_DIGEST=$(echo $DIGEST | cut -d$ -f1)
if [ "$CURRENT_DIGEST" = "$IMAGE_DIGEST" -a "$CURRENT_VERSION" != "latest" ]; then
echo "Image version found: $CURRENT_VERSION"
echo "CURRENT_VERSION=$CURRENT_VERSION" >> $GITHUB_ENV
exit 0
break
fi
done
echo "No image version found in Docker Hub. Cannot continue."
exit 1
- name: "Read Semver Info"
id: "semver_info"
uses: YunaBraska/semver-info-action@main
if: steps.vars.outputs.IMAGE_VERSION && steps.current_version.outputs.CURRENT_VERSION
with:
semver-a: ${{ steps.vars.outputs.IMAGE_VERSION }}
semver-b: ${{ steps.current_version.outputs.CURRENT_VERSION }}

- name: Publish to DockerHub
if: steps.semver_info.outputs.is_greater_a && steps.semver_info.outputs.is_stable_a
uses: ./.github/actions/image-publisher
with:
docker_username: ${{ secrets.DOCKERHUB_USERNAME }}
docker_token: ${{ secrets.DOCKERHUB_TOKEN }}
image_registry: ${{ secrets.DOCKERHUB_REGISTRY }}
image_repository: ${{ steps.vars.outputs.IMAGE_REPOSITORY }}
image_tag: ${{ steps.vars.outputs.IMAGE_VERSION }}
image_latest: true
file: ${{ steps.vars.outputs.IMAGE_DIR }}/Dockerfile
context: ${{ steps.vars.outputs.IMAGE_DIR }}
2 changes: 2 additions & 0 deletions docker/wordpress-cli/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM wordpress:cli

ARG IMAGE_VERSION=1.0.0

ARG USER=www-data

USER root
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM dunglas/frankenphp:latest-php8.2

ARG IMAGE_VERSION=1.0.0

# install the PHP extensions we need (https://make.wordpress.org/hosting/handbook/handbook/server-environment/#php-extensions)
RUN install-php-extensions \
bcmath \
Expand Down
2 changes: 1 addition & 1 deletion templates/docker-compose.yml.edge
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: "3.8"
services:
wp:
build:
context: {{root}}docker/wordpress
context: {{root}}docker/wordpress-franken
dockerfile: Dockerfile
user: "${UID}:${GID}"
extra_hosts:
Expand Down

0 comments on commit c0a8b0c

Please sign in to comment.