Skip to content

Build Docker Image

Build Docker Image #2

name: Build Docker Image
on:
workflow_dispatch:
push:
tags:
- "v[0-9]+.[0-9]+.[0-9]+"
branches-ignore:
- "*"
jobs:
create-runner-amd:
name: Create Hetzner Cloud AMD runner
runs-on: ubuntu-latest
outputs:
label: ${{ steps.create-hcloud-runner.outputs.label }}
server_id: ${{ steps.create-hcloud-runner.outputs.server_id }}
steps:
- name: Create runner
id: create-hcloud-runner
uses: Cyclenerd/hcloud-github-runner@v1
with:
mode: create
github_token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
hcloud_token: ${{ secrets.HCLOUD_TOKEN }}
server_type: cx32
location: hel1 # Helsinki, Finland
image: docker-ce # Docker CE
create-runner-arm:
name: Create Hetzner Cloud ARM runner
runs-on: ubuntu-latest
outputs:
label: ${{ steps.create-hcloud-runner.outputs.label }}
server_id: ${{ steps.create-hcloud-runner.outputs.server_id }}
steps:
- name: Create runner
id: create-hcloud-runner
uses: Cyclenerd/hcloud-github-runner@v1
with:
mode: create
github_token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
hcloud_token: ${{ secrets.HCLOUD_TOKEN }}
server_type: cax21
location: hel1 # Helsinki, Finland
image: docker-ce # Docker CE
build-and-push-amd:
if: startsWith(github.ref, 'refs/tags/v') || github.event_name == 'workflow_dispatch'
environment: production
needs: create-runner-amd # required to start the main job when the runner is ready
runs-on: ${{ needs.create-runner-amd.outputs.label }} # run the job on the newly created runner
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- name: Login GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Extract version parts
id: version
if: startsWith(github.ref, 'refs/tags/v')
run: |
VERSION=${{ github.ref_name }}
echo "full=$VERSION" >> $GITHUB_OUTPUT
echo "major=$(echo $VERSION | cut -d. -f1)" >> $GITHUB_OUTPUT
echo "minor=$(echo $VERSION | cut -d. -f1-2)" >> $GITHUB_OUTPUT
- name: Build and push
uses: docker/build-push-action@v5
with:
push: true
context: .
file: ./Dockerfile
tags: |
ghcr.io/${{ github.repository }}:${{ github.ref_name }}-amd
build-and-push-arm:
if: startsWith(github.ref, 'refs/tags/v') || github.event_name == 'workflow_dispatch'
environment: production
needs: create-runner-arm # required to start the main job when the runner is ready
runs-on: ${{ needs.create-runner-arm.outputs.label }} # run the job on the newly created runner
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- name: Login GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Extract version parts
id: version
if: startsWith(github.ref, 'refs/tags/v')
run: |
VERSION=${{ github.ref_name }}
echo "full=$VERSION" >> $GITHUB_OUTPUT
echo "major=$(echo $VERSION | cut -d. -f1)" >> $GITHUB_OUTPUT
echo "minor=$(echo $VERSION | cut -d. -f1-2)" >> $GITHUB_OUTPUT
- name: Build and push
uses: docker/build-push-action@v5
with:
push: true
context: .
file: ./Dockerfile
tags: |
ghcr.io/${{ github.repository }}:${{ github.ref_name }}-arm
merge-manifest:
if: startsWith(github.ref, 'refs/tags/v') || github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
needs: [build-and-push-amd, build-and-push-arm]
permissions:
contents: read
packages: write
steps:
- name: Login GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Extract version parts
id: version
if: startsWith(github.ref, 'refs/tags/v')
run: |
VERSION=${{ github.ref_name }}
echo "full=$VERSION" >> $GITHUB_OUTPUT
echo "major=$(echo $VERSION | cut -d. -f1)" >> $GITHUB_OUTPUT
echo "minor=$(echo $VERSION | cut -d. -f1-2)" >> $GITHUB_OUTPUT
- name: Create Versioned Manifest (GHCR) (${{ github.ref_name }})
run: |
docker buildx imagetools create -t ghcr.io/${{ github.repository }}:${{ github.ref_name }} \
ghcr.io/${{ github.repository }}:${{ github.ref_name }}-amd \
ghcr.io/${{ github.repository }}:${{ github.ref_name }}-arm
- name: Create Major Version Manifest (GHCR)
if: startsWith(github.ref, 'refs/tags/v')
run: |
docker buildx imagetools create -t ghcr.io/${{ github.repository }}:${{ steps.version.outputs.major }} \
ghcr.io/${{ github.repository }}:${{ github.ref_name }}-amd \
ghcr.io/${{ github.repository }}:${{ github.ref_name }}-arm
- name: Create Minor Version Manifest (GHCR)
if: startsWith(github.ref, 'refs/tags/v')
run: |
docker buildx imagetools create -t ghcr.io/${{ github.repository }}:${{ steps.version.outputs.minor }} \
ghcr.io/${{ github.repository }}:${{ github.ref_name }}-amd \
ghcr.io/${{ github.repository }}:${{ github.ref_name }}-arm
delete-runner-amd:
name: Delete Hetzner Cloud AMD runner
needs:
- create-runner-amd # required to get output from the create-runner-amd job
- build-and-push-amd # required to wait when the main job is done
runs-on: ubuntu-latest
if: ${{ always() }} # required to stop the runner even if the error happened in the previous jobs
steps:
- name: Delete runner
uses: Cyclenerd/hcloud-github-runner@v1
with:
mode: delete
github_token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
hcloud_token: ${{ secrets.HCLOUD_TOKEN }}
name: ${{ needs.create-runner-amd.outputs.label }}
server_id: ${{ needs.create-runner-amd.outputs.server_id }}
delete-runner-arm:
name: Delete Hetzner Cloud ARM runner
needs:
- create-runner-arm # required to get output from the create-runner-arm job
- build-and-push-arm # required to wait when the main job is done
runs-on: ubuntu-latest
if: ${{ always() }} # required to stop the runner even if the error happened in the previous jobs
steps:
- name: Delete runner
uses: Cyclenerd/hcloud-github-runner@v1
with:
mode: delete
github_token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
hcloud_token: ${{ secrets.HCLOUD_TOKEN }}
name: ${{ needs.create-runner-arm.outputs.label }}
server_id: ${{ needs.create-runner-arm.outputs.server_id }}