Skip to content

Commit

Permalink
Improved docker publish action to use official actions
Browse files Browse the repository at this point in the history
  • Loading branch information
afritzler committed Sep 17, 2021
1 parent 49f30f7 commit 3809394
Showing 1 changed file with 44 additions and 32 deletions.
76 changes: 44 additions & 32 deletions .github/workflows/publish-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,52 @@ on:
- 'docs/**'
- '**/*.md'

env:
IMAGE_NAME: onmetal-api-controller-manager

jobs:
test:
runs-on: self-hosted
steps:
- uses: actions/checkout@v2
- name: Run test build
run: |
docker build . --file Dockerfile
push:
# Ensure test job passes before pushing image.
needs: test

buildAndPush:
runs-on: self-hosted
if: github.event_name == 'push'
steps:
- uses: actions/checkout@v2
- name: Build image
run: docker build . --file Dockerfile --tag image
- name: Login to registry
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
- name: Push image
- uses: docker/metadata-action@v3
id: meta
with:
images: |
ghcr.io/${{ github.repository_owner }}/onmetal-api-controller-manager
tags: |
type=semver,pattern={{version}}
type=schedule
type=ref,event=branch
type=ref,event=tag
type=ref,event=pr
type=sha
flavor: |
latest=auto
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
with:
platforms: all
# workaround for self-hosted runner
# https://github.com/mumoshu/actions-runner-controller-ci/commit/e91c8c0f6ca82aa7618010c6d2f417aa46c4a4bf
- name: Set up Docker Context for Buildx
id: buildx-context
run: |
IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME
# Strip git ref prefix from version
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
# Strip "v" prefix from tag name
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
# Use Docker `latest` tag convention
[ "$VERSION" == "main" ] && VERSION=latest
echo IMAGE_ID=$IMAGE_ID
echo VERSION=$VERSION
docker tag image $IMAGE_ID:$VERSION
docker push $IMAGE_ID:$VERSION
docker context create builders
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
with:
version: latest
endpoint: builders # self-hosted
- name: Login to GHCR
if: github.event_name != 'pull_request'
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v2
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

0 comments on commit 3809394

Please sign in to comment.