Build and Push Custom Caddy Container #10
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: Build and Push Custom Caddy Container | |
on: | |
schedule: | |
- cron: "*/15 * * * *" # Runs every 15 minutes | |
workflow_dispatch: # Allows manual trigger | |
jobs: | |
build-and-push-container: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check Out Repository | |
uses: actions/checkout@v3 | |
- name: Fetch Latest Caddy Release | |
id: fetch_release | |
run: | | |
latest_release=$(curl -s https://api.github.com/repos/caddyserver/caddy/releases/latest) | |
echo "$latest_release" > release.json | |
# Extract the tag name | |
raw_tag=$(jq -r '.tag_name' release.json) | |
echo "Raw tag: $raw_tag" | |
# Remove the 'v' prefix if present and strip trailing '.0' | |
processed_tag=$(echo "$raw_tag" | sed -E 's/^v//; s/\.0$//') | |
echo "Processed tag: $processed_tag" | |
# Save to environment variable | |
echo "latest_tag=$processed_tag" >> $GITHUB_ENV | |
- name: Check Current Container Tag | |
id: check_current_tag | |
run: | | |
if curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ | |
https://ghcr.io/v2/callumau/caddy-cloudflare/caddy/manifests/latest | jq -r '.annotations."org.opencontainers.image.version"' > /dev/null 2>&1; then | |
current_tag=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ | |
https://ghcr.io/v2/callumau/caddy-cloudflare/caddy/manifests/latest | jq -r '.annotations."org.opencontainers.image.version"') | |
else | |
current_tag="none" | |
fi | |
echo "current_tag=$current_tag" >> $GITHUB_ENV | |
- name: Compare Tags and Build New Image | |
if: env.latest_tag != env.current_tag | |
run: | | |
echo "New release detected: ${{ env.latest_tag }}" | |
docker build \ | |
--build-arg CADDY_VERSION=${{ env.latest_tag }} \ | |
-t ghcr.io/callumau/caddy-cloudflare/caddy:${{ env.latest_tag }} \ | |
-t ghcr.io/callumau/caddy-cloudflare/caddy:latest . | |
- name: Push New Image to GHCR | |
if: env.latest_tag != env.current_tag | |
run: | | |
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u USERNAME --password-stdin | |
docker push ghcr.io/callumau/caddy-cloudflare/caddy:${{ env.latest_tag }} | |
docker push ghcr.io/callumau/caddy-cloudflare/caddy:latest | |
- name: No New Release Detected | |
if: env.latest_tag == env.current_tag | |
run: echo "No new release detected. Skipping build and push." |