Skip to content

Commit

Permalink
feat(build): add new sha ghrc docker tags using docker/metadata-action (
Browse files Browse the repository at this point in the history
runatlantis#3121)

* feat(build): use docker meta for tags
  • Loading branch information
nitrocode authored and ijames-gc committed Feb 13, 2024
1 parent 164850c commit 8b798b0
Showing 1 changed file with 40 additions and 56 deletions.
96 changes: 40 additions & 56 deletions .github/workflows/atlantis-image.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: atlantis-image


on:
push:
branches:
Expand All @@ -24,10 +25,10 @@ jobs:
image_type: [alpine, debian]
runs-on: ubuntu-22.04
env:
RELEASE_TYPE: ${{ contains(github.ref, 'pre') && 'pre' || 'stable' }}
RELEASE_TAG: ${{ contains(github.ref, 'pre') && 'prerelease-latest' || 'latest' }}
IMAGE_BASE: ghcr.io/${{ github.repository_owner }}/atlantis
IMAGE_SUFFIX: ${{ matrix.image_type != 'alpine' && format('-{0}', matrix.image_type) || '' }}
# Set docker repo to either the fork or the main repo where the branch exists
DOCKER_REPO: ${{ github.repository }}
# Push if not a pull request or this is a fork
PUSH: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.repository }}

steps:
- uses: actions/checkout@v3
Expand All @@ -40,27 +41,44 @@ jobs:

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
# related issues for pinning buildkit
# https://github.com/docker/build-push-action/issues/761
# https://github.com/containerd/containerd/issues/7972
# https://github.com/containerd/containerd/pull/6995
with:
driver-opts: |
image=moby/buildkit:v0.10.6

# release version is the name of the tag i.e. v0.10.0
# release version also has the image type appended i.e. v0.10.0-alpine
# release tag is either pre-release or latest i.e. latest
# release tag also has the image type appended i.e. latest-alpine
# if it's v0.10.0 and alpine, it will do v0.10.0, v0.10.0-alpine, latest, latest-alpine
# if it's v0.10.0 and debian, it will do v0.10.0-debian, latest-debian
- name: Docker meta
id: meta
uses: docker/metadata-action@v4
env:
SUFFIX: ${{ format('-{0}', matrix.image_type) }}
with:
images: |
atlantis
${{ env.DOCKER_REPO }}
labels: |
org.opencontainers.image.licenses=Apache-2.0
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
# semver
type=semver,pattern={{version}},suffix=${{ env.SUFFIX }}
type=semver,pattern={{version}},enable=${{ matrix.image_type == 'alpine' }}
type=semver,pattern={{major}}.{{minor}},suffix=${{ env.SUFFIX }}
# dev
type=raw,value=dev,suffix=${{ env.SUFFIX }}-{{ sha }}
type=raw,event=push,value=dev,enable=${{ github.ref == format('refs/heads/{0}', 'main') && matrix.image_type == 'alpine' }},suffix=
type=raw,event=push,value=dev,enable={{is_default_branch}},suffix=${{ env.SUFFIX }}
# prerelease
type=raw,event=tag,value=prerelease-latest,enable=${{ startsWith(github.ref, 'refs/tags/') && contains(github.ref, 'pre') && matrix.image_type == 'alpine' }},suffix=
type=raw,event=tag,value=prerelease-latest,enable=${{ startsWith(github.ref, 'refs/tags/') && contains(github.ref, 'pre') }},suffix=${{ env.SUFFIX }}
# latest
type=raw,event=tag,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/') && !contains(github.ref, 'pre') && matrix.image_type == 'alpine' }},suffix=
type=raw,event=tag,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/') && !contains(github.ref, 'pre') }},suffix=${{ env.SUFFIX }}
# pr
type=ref,event=pr,suffix=${{ env.SUFFIX }}
flavor: |
# This is disabled here so we can use the raw form above
latest=false
# Suffix is not used here since there's no way to disable it above
- name: Login to Packages Container registry
uses: docker/login-action@v2
Expand All @@ -69,37 +87,13 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# Publish dev image to container registry
- name: Build and push atlantis:dev${{ env.IMAGE_SUFFIX }} image
if: ${{ contains(fromJson('["push", "pull_request"]'), github.event_name) }}
uses: docker/build-push-action@v4
with:
cache-from: type=gha
cache-to: type=gha,mode=max
context: .
build-args: |
ATLANTIS_BASE_TAG_TYPE=${{ matrix.image_type }}
ATLANTIS_VERSION=dev
ATLANTIS_COMMIT=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }}
ATLANTIS_DATE=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }}
platforms: linux/arm64/v8,linux/amd64,linux/arm/v7
push: ${{ github.event_name != 'pull_request' }}
tags: |
ghcr.io/${{ github.repository_owner }}/atlantis:dev${{ env.IMAGE_SUFFIX }}
ghcr.io/${{ github.repository_owner }}/atlantis:dev-${{ matrix.image_type }}
labels: ${{ steps.meta.outputs.labels }}

# Publish release to container registry
- name: Populate release version
if: |
contains(fromJson('["push", "pull_request"]'), github.event_name) &&
startsWith(github.ref, 'refs/tags/')
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
if: contains(fromJson('["push", "pull_request"]'), github.event_name)
run: echo "RELEASE_VERSION=${{ startsWith(github.ref, 'refs/tags/') && '${GITHUB_REF#refs/*/}' || 'dev' }}" >> $GITHUB_ENV

- name: "Build and push atlantis:${{ env.RELEASE_VERSION }} image for ${{ env.RELEASE_TYPE }} release"
if: |
contains(fromJson('["push", "pull_request"]'), github.event_name) &&
startsWith(github.ref, 'refs/tags/')
- name: "Build ${{ env.PUSH == 'true' && 'and push' || '' }} ${{ env.DOCKER_REPO }} image"
if: contains(fromJson('["push", "pull_request"]'), github.event_name)
uses: docker/build-push-action@v4
with:
cache-from: type=gha
Expand All @@ -111,16 +105,6 @@ jobs:
ATLANTIS_COMMIT=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }}
ATLANTIS_DATE=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }}
platforms: linux/arm64/v8,linux/amd64,linux/arm/v7
push: ${{ github.event_name != 'pull_request' }}
# release version is the name of the tag i.e. v0.10.0
# release version also has the image type appended i.e. v0.10.0-alpine
# release tag is either pre-release or latest i.e. latest
# release tag also has the image type appended i.e. latest-alpine
# if it's v0.10.0 and alpine, it will do v0.10.0, v0.10.0-alpine, latest, latest-alpine
# if it's v0.10.0 and debian, it will do v0.10.0-debian, latest-debian
tags: |
${{ env.IMAGE_BASE }}:${{ env.RELEASE_VERSION }}${{ env.IMAGE_SUFFIX }}
${{ env.IMAGE_BASE }}:${{ env.RELEASE_VERSION }}-${{ matrix.image_type }}
${{ env.IMAGE_BASE }}:${{ env.RELEASE_TAG }}${{ env.IMAGE_SUFFIX }}
${{ env.IMAGE_BASE }}:${{ env.RELEASE_TAG }}-${{ matrix.image_type }}
push: ${{ env.PUSH }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

0 comments on commit 8b798b0

Please sign in to comment.