Publish Docker Image #308
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Publish Docker Image | |
on: | |
workflow_dispatch: | |
schedule: | |
- cron: '0 0 * * 0' | |
jobs: | |
build: | |
name: Build | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out | |
uses: actions/checkout@v4 | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Login to DockerHub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Login to GitHub Container Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.repository_owner }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Run script to check version | |
id: shell | |
env: | |
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} | |
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} | |
run: | | |
DOCKER_HUB_UP="library/caddy" | |
DOCKER_HUB_DOWN="xjasonlyu/caddy" | |
AUTH_DOMAIN="auth.docker.io" | |
AUTH_SERVICE="registry.docker.io" | |
AUTH_OFFLINE_TOKEN="1" | |
AUTH_CLIENT_ID="shell" | |
API_DOMAIN="registry-1.docker.io" | |
TOKEN=$(curl -s -f -X GET -u ${DOCKER_USERNAME}:${DOCKER_PASSWORD} "https://${AUTH_DOMAIN}/token?service=${AUTH_SERVICE}&scope=repository:${DOCKER_HUB_UP}:pull&offline_token=${AUTH_OFFLINE_TOKEN}&client_id=${AUTH_CLIENT_ID}" | jq -r '.token') | |
VERSION_UP=$(curl -s -f -H "Authorization: Bearer ${TOKEN}" https://${API_DOMAIN}/v2/${DOCKER_HUB_UP}/tags/list | jq -r '.tags[]' | grep -E '^[0-9.]+$' | sort --version-sort | tail -n 1) | |
[ "$?" -ne 0 ] && exit 1 | |
TOKEN=$(curl -s -f -X GET -u ${DOCKER_USERNAME}:${DOCKER_PASSWORD} "https://${AUTH_DOMAIN}/token?service=${AUTH_SERVICE}&scope=repository:${DOCKER_HUB_DOWN}:pull&offline_token=${AUTH_OFFLINE_TOKEN}&client_id=${AUTH_CLIENT_ID}" | jq -r '.token') | |
VERSION_DOWN=$(curl -s -f -H "Authorization: Bearer ${TOKEN}" https://${API_DOMAIN}/v2/${DOCKER_HUB_DOWN}/tags/list | jq -r '.tags[]' | grep -E '^[0-9.]+' | sort --version-sort | tail -n 1) | |
[ "$?" -ne 0 ] && exit 1 | |
if [ "${VERSION_UP}" == "${VERSION_DOWN}" ]; then | |
echo "version=pass:${VERSION_UP}" >> $GITHUB_OUTPUT | |
else | |
echo "version=${VERSION_UP}" >> $GITHUB_OUTPUT | |
fi | |
- name: Build and Push | |
if: steps.shell.outputs.version != '' && !startsWith(steps.shell.outputs.version, 'pass:') | |
uses: docker/build-push-action@v5 | |
with: | |
push: true | |
platforms: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/ppc64le | |
build-args: CADDY_VERSION=v${{ steps.shell.outputs.version }} | |
tags: | | |
xjasonlyu/caddy:latest | |
xjasonlyu/caddy:${{ steps.shell.outputs.version }} | |
ghcr.io/xjasonlyu/caddy:latest | |
ghcr.io/xjasonlyu/caddy:${{ steps.shell.outputs.version }} |