From 8345065864861f6b18a447dad9efdfff07fd5b8e Mon Sep 17 00:00:00 2001 From: Kyle Colantonio Date: Fri, 30 Aug 2024 21:36:47 -0400 Subject: [PATCH] feat: Refactoring workflows --- .github/workflows/cache-sync.yml | 39 ++++ .github/workflows/cache.yml | 54 ------ .github/workflows/docker.yml | 291 +++++++++++++++++------------- .github/workflows/readme-sync.yml | 31 ++++ .github/workflows/readme.yml | 28 --- .github/workflows/trivy.yml | 102 +++++------ README.md | 37 +++- 7 files changed, 310 insertions(+), 272 deletions(-) create mode 100644 .github/workflows/cache-sync.yml delete mode 100644 .github/workflows/cache.yml create mode 100644 .github/workflows/readme-sync.yml delete mode 100644 .github/workflows/readme.yml diff --git a/.github/workflows/cache-sync.yml b/.github/workflows/cache-sync.yml new file mode 100644 index 0000000..f101e0f --- /dev/null +++ b/.github/workflows/cache-sync.yml @@ -0,0 +1,39 @@ +# This workflow is necessary in order for the the proper Event to be used +# so we can pull the source and destination branch details +name: PR Cache Sync + +on: + pull_request: + branches: [main] + types: + - closed + paths: + - Dockerfile + - .dockerignore + +env: + DOCKER_BUILDKIT: "1" + COSIGN_EXPERIMENTAL: "1" + +jobs: + cache-sync: + name: PR cache sync + runs-on: ubuntu-latest + if: github.event.pull_request.merged == true + steps: + - name: Install regctl + uses: iarekylew00t/regctl-installer@273ea1255c27762c52a9a481c78ba685f30a9335 # v1.2.15 + + - name: Copy PR build cache to branch + shell: bash + run: | + IMAGE_NAME=$(echo "${GITHUB_REPOSITORY/docker-/}" | tr '[:upper:]' '[:lower:]') + BASE_BRANCH=$(echo ${GITHUB_BASE_REF//[^a-zA-Z0-9]/-} | tr '[:upper:]' '[:lower:]') + HEAD_BRANCH=$(echo ${GITHUB_HEAD_REF//[^a-zA-Z0-9]/-} | tr '[:upper:]' '[:lower:]') + echo "ttl.sh/$IMAGE_NAME:$HEAD_BRANCH --> ttl.sh/$IMAGE_NAME:$BASE_BRANCH" + regctl image copy \ + --verbosity info \ + --digest-tags \ + --force-recursive \ + "ttl.sh/$IMAGE_NAME:$HEAD_BRANCH" \ + "ttl.sh/$IMAGE_NAME:$BASE_BRANCH" diff --git a/.github/workflows/cache.yml b/.github/workflows/cache.yml deleted file mode 100644 index 4817361..0000000 --- a/.github/workflows/cache.yml +++ /dev/null @@ -1,54 +0,0 @@ -name: Cache Sync - -on: - pull_request: - branches: [main] - types: - - closed - paths: - - Dockerfile - - .dockerignore - -env: - DOCKER_BUILDKIT: 1 - COSIGN_EXPERIMENTAL: 1 - -jobs: - pr-cache: - name: Copy PR build cache - runs-on: ubuntu-latest - - env: - DOCKER_BUILDKIT: 1 - - if: github.event.pull_request.merged - steps: - - name: Generate docker-compliant image name - run: echo "IMAGE_NAME=$(echo ${GITHUB_REPOSITORY,,} | sed 's/docker-//')" | tee -a $GITHUB_ENV - - - name: Generate base tag - run: echo "BASE_BRANCH=$(echo ${GITHUB_BASE_REF,,} | sed 's/[^a-zA-Z0-9]/-/g')" | tee -a $GITHUB_ENV - - - name: Generate head tag - run: echo "HEAD_BRANCH=$(echo ${GITHUB_HEAD_REF,,} | sed 's/[^a-zA-Z0-9]/-/g')" | tee -a $GITHUB_ENV - - - name: Install Cosign - uses: sigstore/cosign-installer@v3.6.0 - - - name: Install regctl - uses: iarekylew00t/regctl-installer@v1 - - - name: Verify container images - run: | - cosign verify \ - --certificate-oidc-issuer https://token.actions.githubusercontent.com \ - --certificate-identity-regexp https://github.com/$GITHUB_REPOSITORY/.github/workflows/ \ - "ttl.sh/$IMAGE_NAME:$BASE_BRANCH" - env: - COSIGN_EXPERIMENTAL: 1 - - - name: Copy PR build cache to branch tag - run: | - regctl image copy --verbosity info --digest-tags --force-recursive \ - "ttl.sh/$IMAGE_NAME:$HEAD_BRANCH" \ - "ttl.sh/$IMAGE_NAME:$BASE_BRANCH" diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index a93d8ef..1380026 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -1,7 +1,7 @@ -name: Docker +name: Docker Build & Release on: - workflow_dispatch: # manual + workflow_dispatch: push: branches: [main] paths: @@ -14,89 +14,100 @@ on: - .dockerignore env: - DOCKER_BUILDKIT: 1 - COSIGN_EXPERIMENTAL: 1 + DOCKER_BUILDKIT: "1" + DOCKER_BUILD_SUMMARY: "false" + COSIGN_EXPERIMENTAL: "1" jobs: metadata: - name: Get image and repo details + name: Build metadata runs-on: ubuntu-latest - + permissions: + contents: read + pull-requests: read outputs: - name: ${{ steps.name.outputs.name }} - title: ${{ steps.title.outputs.title }} - version: ${{ steps.version.outputs.version }} - branch: ${{ steps.branch.outputs.branch }} - labels: ${{ steps.metadata.outputs.labels }} - tags: ${{ steps.metadata.outputs.tags }} - platforms: linux/amd64,linux/arm64,linux/ppc64le,linux/s390x,linux/arm/v7,linux/arm/v6 # match caddy - + image-title: ${{ steps.build-metadata.outputs.image-title }} + image-name: ${{ steps.build-metadata.outputs.image-name }} + caddy-version: ${{ steps.build-metadata.outputs.caddy-version }} + branch-name: ${{ steps.build-metadata.outputs.branch-name }} + labels: ${{ steps.docker-metadata.outputs.labels }} + tags: ${{ steps.docker-metadata.outputs.tags }} + platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6,linux/ppc64le,linux/s390x steps: - name: Checkout repo - uses: actions/checkout@v4 - - - name: Generate docker-compliant image name - id: name - run: echo "name=$(echo ${GITHUB_REPOSITORY,,} | sed 's/docker-//')" | tee -a $GITHUB_OUTPUT + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 - - name: Generate OCI image title - id: title - run: echo "title=$(echo ${GITHUB_REPOSITORY#*/} | sed 's/docker-//')" | tee -a $GITHUB_OUTPUT - - - name: Parse Caddy version - id: version - run: echo "version=$(grep -Eo 'caddy:[0-9]+\.[0-9]+\.[0-9]+$' Dockerfile | cut -d ':' -f2)" | tee -a $GITHUB_OUTPUT - - - name: Generate build tag from head - id: branch + - name: Generate build metadata + id: build-metadata + shell: bash run: | - export GIT_REF=${GITHUB_HEAD_REF:-$GITHUB_REF_NAME} - echo "branch=$(echo ${GIT_REF,,} | sed 's/[^a-zA-Z0-9]/-/g')" | tee -a $GITHUB_OUTPUT - - - name: Generate Docker metadata with Caddy version - uses: docker/metadata-action@v5 - id: metadata + IMAGE_TITLE=$(echo "$GITHUB_REPOSITORY" | sed 's/.*docker-//g') + IMAGE_NAME=$(echo "${GITHUB_REPOSITORY/docker-/}" | tr '[:upper:]' '[:lower:]') + CADDY_VERSION=$(grep -m 1 -Eo 'caddy:[0-9]+\.[0-9]+\.[0-9]+' Dockerfile \ + | sed -E 's/.+:([0-9]+\.[0-9]+\.[0-9]+)(.+)?$/\1/g') + BRANCH_NAME=$(echo "${GITHUB_HEAD_REF:-$GITHUB_REF_NAME}" \ + | tr '[:upper:]' '[:lower:]' | sed 's/[^a-zA-Z0-9]/-/g') + cat < [!NOTE] +> This image does not change anything with original Caddy Docker image except +> replace the `caddy` binary. ```sh # Docker Hub @@ -24,9 +32,15 @@ The following tags are available for the `iarekylew00t/caddy-cloudflare` image. ## Usage -Since this is built off the official Docker image all of the same Volumes, Environment variables, etc. can be used with this container. Please refer to the official [Caddy](https://hub.docker.com/_/caddy) Docker image and [docs](https://caddyserver.com/docs/) for more information on using Caddy. +Since this is built off the official Docker image all of the same Volumes, +Environment variables, etc. can be used with this container. Please refer to the +official [Caddy](https://hub.docker.com/_/caddy) Docker image and +[docs](https://caddyserver.com/docs/) for more information on using Caddy. -Simply create the container as usual and include your `CF_API_TOKEN` (email no longer required for API Tokens). We can utilizing Caddy's support for [Environment varaiables](https://caddyserver.com/docs/caddyfile/concepts#environment-variables) to pass these values into our `Caddyfile`. +Simply create the container as usual and include your `CF_API_TOKEN` (email no +longer required for API Tokens). We can utilizing Caddy's support for +[Environment varaiables](https://caddyserver.com/docs/caddyfile/concepts#environment-variables) +to pass these values into our `Caddyfile`. ```sh docker run --rm -it \ @@ -40,7 +54,9 @@ docker run --rm -it \ iarekylew00t/caddy-cloudflare:latest ``` -Then set the global [acme_dns](https://caddyserver.com/docs/caddyfile/options#acme-dns) directive in your `Caddyfile` +Then set the global +[acme_dns](https://caddyserver.com/docs/caddyfile/options#acme-dns) directive +in your `Caddyfile` ```Caddyfile { @@ -64,7 +80,9 @@ or via JSON } ``` -See the [caddy-dns/cloudflare](https://github.com/caddy-dns/cloudflare) module and [`tls`](https://caddyserver.com/docs/caddyfile/directives/tls#tls) directive for advanced usage. +See the [caddy-dns/cloudflare](https://github.com/caddy-dns/cloudflare) module +and [`tls`](https://caddyserver.com/docs/caddyfile/directives/tls#tls) directive +for advanced usage. ### Creating a Cloudflare API Token @@ -87,7 +105,10 @@ docker build -t caddy-cloudflare . ## Container signatures -All container images will be automatically signed via [Cosign](https://docs.sigstore.dev/cosign/overview/) using [keyless signatures](https://docs.sigstore.dev/cosign/keyless/). You can use the following command to verify the integrity of these images yourself. +All container images will be automatically signed via +[Cosign](https://docs.sigstore.dev/cosign/overview/) using +[keyless signatures](https://docs.sigstore.dev/cosign/keyless/). You can use the +following command to verify the integrity of these images yourself. ```sh cosign verify \ @@ -98,7 +119,9 @@ cosign verify \ ## Contributing -Feel free to contribute and make things better by opening an [Issue](https://github.com/IAreKyleW00t/docker-caddy-cloudflare/issues) or [Pull Request](https://github.com/IAreKyleW00t/docker-caddy-cloudflare/pulls). +Feel free to contribute and make things better by opening an +[Issue](https://github.com/IAreKyleW00t/docker-caddy-cloudflare/issues) or +[Pull Request](https://github.com/IAreKyleW00t/docker-caddy-cloudflare/pulls). ## License