Skip to content

Docker Image CI

Docker Image CI #71

name: Docker Image CI
# For each project: modify the inputs, release tags, ARGs (at the very end), and anything at the top end of the file.
on:
workflow_dispatch:
inputs:
image_purpose:
description: 'Purpose of the image'
required: true
type: choice
options:
- testing
- production
caddy_version:
description: 'Version of Caddy to build from'
required: true
default: '2.8.4'
is_cloudflare_support:
description: 'add Cloudflare support?'
required: true
type: boolean
default: true
is_geoip_support:
description: 'add GeoIP support?'
required: true
type: boolean
is_logging_template_support:
description: 'add logging file templating support?'
required: true
type: boolean
default: true
is_crowdsec_support:
description: 'add CrowdSec support?'
required: true
type: boolean
is_waf_support:
description: 'add Web Application Firewall support?'
required: true
type: boolean
is_layer4_support:
description: 'add Layer 4 support?'
required: true
type: boolean
is_tailscale_machine_support:
description: 'add Tailscale Machine support?'
required: true
type: boolean
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Create the `xcaddy build` string
id: xcaddy
env:
CADDY_STR: ""
EXTRA_TAG: ""
run: |
if [ "${{ github.event.inputs.is_cloudflare_support }}" == 'true' ]; then
CADDY_STR="$CADDY_STR --with github.com/caddy-dns/cloudflare"
CADDY_STR="$CADDY_STR --with github.com/WeidiDeng/caddy-cloudflare-ip"
EXTRA_TAG="11${EXTRA_TAG}"
else
EXTRA_TAG="00${EXTRA_TAG}"
fi
if [ "${{ github.event.inputs.is_geoip_support }}" == 'true' ]; then
CADDY_STR="$CADDY_STR --with github.com/zhangjiayin/caddy-geoip2"
EXTRA_TAG="1${EXTRA_TAG}"
else
EXTRA_TAG="0${EXTRA_TAG}"
fi
if [ "${{ github.event.inputs.is_logging_template_support }}" == 'true' ]; then
CADDY_STR="$CADDY_STR --with github.com/caddyserver/transform-encoder "
EXTRA_TAG="1${EXTRA_TAG}"
else
EXTRA_TAG="0${EXTRA_TAG}"
fi
EXTRA_TAG="-${EXTRA_TAG}"
if [ "${{ github.event.inputs.is_crowdsec_support }}" == 'true' ]; then
CADDY_STR="$CADDY_STR --with github.com/hslatman/caddy-crowdsec-bouncer"
EXTRA_TAG="1${EXTRA_TAG}"
else
EXTRA_TAG="0${EXTRA_TAG}"
fi
if [ "${{ github.event.inputs.is_waf_support }}" == 'true' ]; then
CADDY_STR="$CADDY_STR --with github.com/corazawaf/coraza-caddy/v2"
EXTRA_TAG="1${EXTRA_TAG}"
else
EXTRA_TAG="0${EXTRA_TAG}"
fi
if [ "${{ github.event.inputs.is_layer4_support }}" == 'true' ]; then
CADDY_STR="$CADDY_STR --with github.com/mholt/caddy-l4"
CADDY_STR="$CADDY_STR --with github.com/abiosoft/caddy-json-schema"
EXTRA_TAG="1${EXTRA_TAG}"
else
EXTRA_TAG="0${EXTRA_TAG}"
fi
if [ "${{ github.event.inputs.is_tailscale_machine_support }}" == 'true' ]; then
# CADDY_STR="$CADDY_STR --with github.com/tailscale/caddy-tailscale@0f105e89fbe2222c690b94b5b0b2a8150fa2540f"
CADDY_STR="$CADDY_STR --with github.com/tailscale/caddy-tailscale@main"
EXTRA_TAG="1${EXTRA_TAG}"
else
EXTRA_TAG="0${EXTRA_TAG}"
fi
CADDY_STR="$CADDY_STR --with github.com/hairyhenderson/caddy-teapot-module@v0.0.3-0"
XCADDY_LABEL="xcaddy build ${CADDY_STR}"
echo "XCADDY_LABEL=$XCADDY_LABEL" >> $GITHUB_OUTPUT
echo "XCADDY_STR=$CADDY_STR" >> $GITHUB_OUTPUT
echo "extra_tag=$EXTRA_TAG" >> $GITHUB_OUTPUT
# -----------------------------------------------------
# Aside from the Release tag, pretty standard here on out.
- name: Run date command
id: date
run: |
buildTime=$(date +%Y-%m-%d" "%X)
echo "BUILD_TIME=$buildTime" >> $GITHUB_OUTPUT
# modify me!
- name: Get release tag
id: tag
run: |
if [ "${{ github.event.inputs.image_purpose }}" == "testing" ]; then
buildTag=$(date +%Y-%m-%d--%H-%M)
echo "GH_BUILD_TAG=${{ steps.xcaddy.outputs.extra_tag }}-${buildTag}" >> $GITHUB_OUTPUT
echo "GH_LATEST_TAG=testing" >> $GITHUB_OUTPUT
elif [ "${{ github.event.inputs.image_purpose }}" == "production" ]; then
echo "GH_BUILD_TAG=${{ github.event.inputs.caddy_version }}-alpine-${{ steps.xcaddy.outputs.extra_tag }}" >> $GITHUB_OUTPUT
echo "GH_LATEST_TAG=latest" >> $GITHUB_OUTPUT
else
echo "Invalid image purpose specified" >&2
exit 1
fi
- name: Set platform/repo var
id: docker_build_vars
run: |
if [ "${{ github.event.inputs.image_purpose }}" == "production" ]; then
# echo "PLATFORMS=linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6" >> $GITHUB_OUTPUT
# golang can't handle linux/arm/v6
echo "PLATFORMS=linux/amd64,linux/arm64,linux/arm/v7" >> $GITHUB_OUTPUT
else
# echo "PLATFORMS=linux/amd64" >> $GITHUB_OUTPUT
echo "PLATFORMS=linux/amd64,linux/arm64" >> $GITHUB_OUTPUT
fi
# assume the DockerHub repository name is the same as the GitHub repository name
# GITHUB_REPOSITORY is <user>/<repo>, remove <user>
REPOSITORY=${GITHUB_REPOSITORY##*/}
echo "REPOSITORY_NAME=$REPOSITORY" >> $GITHUB_OUTPUT
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push
id: docker_build
uses: docker/build-push-action@v5
with:
context: .
push: true
platforms: ${{ steps.docker_build_vars.outputs.PLATFORMS }}
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/${{ steps.docker_build_vars.outputs.REPOSITORY_NAME }}:${{ steps.tag.outputs.GH_BUILD_TAG }}
${{ secrets.DOCKERHUB_USERNAME }}/${{ steps.docker_build_vars.outputs.REPOSITORY_NAME }}:${{ steps.tag.outputs.GH_LATEST_TAG }}
build-args: |
BUILD_TIME=${{ steps.date.outputs.BUILD_TIME }}
CADDY_VERSION=${{ github.event.inputs.caddy_version }}
XCADDY_STRING=${{ steps.xcaddy.outputs.XCADDY_STR }}
XCADDY_LABEL=${{ steps.xcaddy.outputs.XCADDY_LABEL }}