diff --git a/.github/actions/image-publisher/action.yml b/.github/actions/image-publisher/action.yml new file mode 100644 index 0000000..e0ec878 --- /dev/null +++ b/.github/actions/image-publisher/action.yml @@ -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 \ No newline at end of file diff --git a/.github/workflows/docker_deploy.yml b/.github/workflows/docker_deploy.yml new file mode 100644 index 0000000..3af85b5 --- /dev/null +++ b/.github/workflows/docker_deploy.yml @@ -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 }} diff --git a/docker/wordpress-cli/Dockerfile b/docker/wordpress-cli/Dockerfile index a8e45e4..0bc17a0 100644 --- a/docker/wordpress-cli/Dockerfile +++ b/docker/wordpress-cli/Dockerfile @@ -1,5 +1,7 @@ FROM wordpress:cli +ARG IMAGE_VERSION=1.0.0 + ARG USER=www-data USER root diff --git a/docker/wordpress/Caddyfile b/docker/wordpress-franken/Caddyfile similarity index 100% rename from docker/wordpress/Caddyfile rename to docker/wordpress-franken/Caddyfile diff --git a/docker/wordpress/Dockerfile b/docker/wordpress-franken/Dockerfile similarity index 98% rename from docker/wordpress/Dockerfile rename to docker/wordpress-franken/Dockerfile index 8f7ff66..5b63c3c 100644 --- a/docker/wordpress/Dockerfile +++ b/docker/wordpress-franken/Dockerfile @@ -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 \ diff --git a/templates/docker-compose.yml.edge b/templates/docker-compose.yml.edge index 2225400..7d87baf 100644 --- a/templates/docker-compose.yml.edge +++ b/templates/docker-compose.yml.edge @@ -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: