Skip to content

Build and Push Alpine Docker Image #82

Build and Push Alpine Docker Image

Build and Push Alpine Docker Image #82

name: Build and Push Alpine Docker Image
on:
repository_dispatch:
types: [caddy-release]
workflow_dispatch:
inputs:
build_version:
description: 'Caddy version to build (without v prefix, e.g., 2.8.0)'
required: false
permissions:
packages: write
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout
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 GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
env:
user: ${{ secrets.DOCKERHUB_USERNAME }}
token: ${{ secrets.DOCKERHUB_TOKEN }}
if: ${{ env.user != '' && env.token != '' }}
- name: Set Repository Owner and Name in Lowercase
run: |
REPO_OWNER=$(echo "${GITHUB_REPOSITORY_OWNER}" | tr '[:upper:]' '[:lower:]')
echo "REPO_OWNER=${REPO_OWNER}" >> $GITHUB_ENV
REPO_NAME=$(echo "${GITHUB_REPOSITORY#*/}" | tr '[:upper:]' '[:lower:]')
echo "REPO_NAME=${REPO_NAME}" >> $GITHUB_ENV
- name: Set version
id: set_version
run: |
# Get version from input or payload
VERSION="${{ github.event.inputs.build_version || github.event.client_payload.caddy_version }}"
# Exit if no version specified
if [ -z "$VERSION" ]; then
echo "No version specified. Exiting."
exit 1
fi
# Strip v prefix if present
VERSION="${VERSION#v}"
echo "VERSION=${VERSION}" >> $GITHUB_ENV
- name: Set Docker Hub image (if credentials exist)
run: |
# Check if Docker Hub credentials are provided
if [ -n "${{ secrets.DOCKERHUB_USERNAME }}" ] && [ -n "${{ secrets.DOCKERHUB_TOKEN }}" ]; then
if [ -n "${{ secrets.DOCKERHUB_REPOSITORY_NAME }}" ]; then
echo "DOCKERHUB_IMAGE=${{ secrets.DOCKERHUB_REPOSITORY_NAME }}" >> $GITHUB_ENV
else
echo "DOCKERHUB_IMAGE=${{ secrets.DOCKERHUB_USERNAME }}/${{ env.REPO_NAME }}" >> $GITHUB_ENV
fi
else
echo "Warning: DOCKERHUB_USERNAME or DOCKERHUB_TOKEN not set. Docker Hub image will not be pushed."
fi
- name: Docker Metadata
id: meta
uses: docker/metadata-action@v5
with:
images: |
ghcr.io/${{ env.REPO_OWNER }}/${{ env.REPO_NAME }}
${{ env.DOCKERHUB_IMAGE }}
flavor: |
latest=false
tags: |
# Raw version tags
type=raw,value=${{ env.VERSION }}-alpine
type=raw,value=alpine
# Semver tagging strategy
type=semver,pattern={{version}}-alpine,value=${{ env.VERSION }}
type=semver,pattern={{major}}.{{minor}}-alpine,value=${{ env.VERSION }}
type=semver,pattern={{major}}-alpine,value=${{ env.VERSION }}
labels: |
org.opencontainers.image.title=Caddy Cloudflare Alpine
org.opencontainers.image.description=Caddy with Cloudflare DNS module (Alpine)
org.opencontainers.image.url=${{ github.server_url }}/${{ github.repository }}
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
org.opencontainers.image.version=${{ env.VERSION }}-alpine
org.opencontainers.image.licenses=MIT
org.opencontainers.image.vendor=${{ github.repository_owner }}
- name: Build and push Docker image
id: build
uses: docker/build-push-action@v5
with:
context: .
file: Dockerfile.alpine
push: true
build-args: |
CADDY_VERSION=${{ env.VERSION }}
platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/ppc64le,linux/s390x
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}