Skip to content

Docker publish

Docker publish #187

name: Docker publish
on:
push:
branches:
- main
tags:
- wp*
schedule:
- cron: '0 4 * * 0'
env:
IMAGE_NAME: wordpress-xdebug
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: docker build -f Dockerfile .
tag:
runs-on: ubuntu-latest
outputs:
tag: ${{ steps.create-tag.outputs.tag }}
tag_exists: ${{ steps.check-tag.outputs.result }}
steps:
- uses: actions/checkout@v4
- name: Get dockerhub wordpress latest version tag
id: image-tag
uses: ./.github/actions/get-dockerhub-version-tag
with:
org: 'library'
repo: 'wordpress'
- name: Get Xdebug pecl package version
id: package-version
uses: ./.github/actions/get-pecl-package-version
with:
package: 'xdebug'
stability: 'stable'
- name: Create tag
id: create-tag
run: echo "tag=wp${{ steps.image-tag.outputs.tag}}-xdebug${{ steps.package-version.outputs.version }}" >> $GITHUB_OUTPUT
- name: Check if tag already exists
id: check-tag
uses: actions/github-script@v7
with:
script: |
let tag_exists = 0;
try {
await github.rest.git.getRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: "tags/${{ steps.create-tag.outputs.tag }}"
})
tag_exists = 1;
} catch(err) {
if (err.status != 404) throw err;
}
return tag_exists;
- name: Push tag
id: push-tag
if: steps.check-tag.outputs.result == 0
uses: actions/github-script@v7
with:
script: |
github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: "refs/tags/${{ steps.create-tag.outputs.tag }}",
sha: context.sha
})
push:
environment: production
needs: [tag, test]
runs-on: ubuntu-latest
if: needs.tag.outputs.tag_exists == 0
steps:
- uses: actions/checkout@v4
- run: docker build -f Dockerfile -t $IMAGE_NAME .
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ github.actor }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Login to Github Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Push image
run: |
GH_IMAGE_ID=ghcr.io/${{ github.repository }}/$IMAGE_NAME
DH_IMAGE_ID=${{ github.actor }}/$IMAGE_NAME
# Change all uppercase to lowercase
GH_IMAGE_ID=$(echo $GH_IMAGE_ID | tr '[A-Z]' '[a-z]')
DH_IMAGE_ID=$(echo $DH_IMAGE_ID | tr '[A-Z]' '[a-z]')
# Version from created tag
VERSION=${{ needs.tag.outputs.tag }}
docker tag $IMAGE_NAME $GH_IMAGE_ID:latest
docker tag $IMAGE_NAME $GH_IMAGE_ID:$VERSION
docker tag $IMAGE_NAME $DH_IMAGE_ID:latest
docker tag $IMAGE_NAME $DH_IMAGE_ID:$VERSION
docker push --all-tags $GH_IMAGE_ID
docker push --all-tags $DH_IMAGE_ID