Build container image #304
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build container image | |
on: | |
push: | |
branches: [ main, devel ] | |
tags: [ v*.*.* ] | |
pull_request: | |
types: [ assigned, opened, synchronize, reopened ] | |
schedule: | |
- cron: "0 13 * * 1" | |
workflow_dispatch: | |
env: | |
# build for multiple platforms when not a pull-request | |
PLATFORMS: ${{ fromJSON('[ "linux/amd64", "linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6,linux/ppc64le" ]')[ github.event_name != 'pull_request' ] }} | |
# how to name the image | |
IMAGENAME: speedtest | |
TESTIMAGE: "${{ github.repository_owner }}/speedtest:testing" | |
# dockerhub credentials | |
DOCKERHUB_USER: ansemjo | |
#DOCKERHUB_TOKEN - add as secret value | |
jobs: | |
container: | |
name: build image 📦 | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Prepare Tags | |
id: prep | |
shell: bash | |
run: | | |
TAGS=() | |
case "${GITHUB_REF}" in | |
# version releases | |
refs/tags/*) | |
VERSION="${GITHUB_REF#refs/tags/}" | |
if [[ ${VERSION} =~ ^v([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then | |
V=("${BASH_REMATCH[@]}") | |
TAGS+=("${{ env.IMAGENAME }}:${V[1]}" \ | |
"${{ env.IMAGENAME }}:${V[1]}.${V[2]}" \ | |
"${{ env.IMAGENAME }}:${V[1]}.${V[2]}.${V[3]}") | |
else | |
TAGS+=("${{ env.IMAGENAME }}:${VERSION}") | |
fi | |
;& | |
# branch heads (+ fallthorugh) | |
refs/heads/*) | |
TAGS+=("${{ env.IMAGENAME }}:latest") | |
TAGS=$({ IFS=","; echo "${TAGS[*]/#/${{ env.DOCKERHUB_USER }}/}","${TAGS[*]/#/ghcr.io/${{ github.repository_owner }}/}"; }) | |
;; | |
# pull requests | |
refs/pull/*) | |
TAGS=("${{ github.repository_owner }}/${{ env.IMAGENAME }}:pr-${{ github.event.number }}") | |
;; | |
esac | |
echo "TAGS ${TAGS}" | |
echo "tags=${TAGS}" >> $GITHUB_OUTPUT | |
echo "head=${GITHUB_REF#refs/heads/}" >> $GITHUB_OUTPUT | |
echo "created=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT | |
- name: Set up QEMU | |
uses: docker/[email protected] | |
- name: Set up Docker Buildx | |
uses: docker/[email protected] | |
- name: Login to Docker Hub | |
uses: docker/[email protected] | |
if: ${{ github.event_name != 'pull_request' && steps.prep.outputs.head != 'devel' }} | |
with: | |
username: ${{ env.DOCKERHUB_USER }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Login to GitHub Container Registry | |
uses: docker/[email protected] | |
if: ${{ github.event_name != 'pull_request' && steps.prep.outputs.head != 'devel' }} | |
with: | |
registry: ghcr.io | |
username: ${{ github.repository_owner }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Build and export to Docker | |
uses: docker/[email protected] | |
with: | |
context: . | |
file: ./container/Dockerfile | |
push: false | |
load: true | |
tags: ${{ env.TESTIMAGE }} | |
- name: Run a test measurement in built image | |
run: | | |
docker run --rm ${{ env.TESTIMAGE }} measure | |
- name: Build and push | |
uses: docker/[email protected] | |
if: ${{ github.event_name != 'pull_request' && steps.prep.outputs.head != 'devel' }} | |
with: | |
context: . | |
file: ./container/Dockerfile | |
platforms: ${{ env.PLATFORMS }} | |
push: true | |
tags: ${{ steps.prep.outputs.tags }} | |
labels: | | |
org.opencontainers.image.title=${{ github.event.repository.name }} | |
org.opencontainers.image.description=${{ github.event.repository.description }} | |
org.opencontainers.image.url=${{ github.event.repository.html_url }} | |
org.opencontainers.image.source=${{ github.event.repository.clone_url }} | |
org.opencontainers.image.created=${{ steps.prep.outputs.created }} | |
org.opencontainers.image.revision=${{ github.sha }} | |
org.opencontainers.image.licenses=${{ github.event.repository.license.spdx_id }} | |
dependabot: | |
# https://nicolasiensen.github.io/2022-07-23-automating-dependency-updates-with-dependabot-github-auto-merge-and-github-actions/ | |
name: merge dependabot pr 🏗️ | |
needs: [ container ] | |
runs-on: ubuntu-latest | |
if: ${{ github.event_name == 'pull_request' && github.actor == 'dependabot[bot]' }} | |
permissions: | |
pull-requests: write | |
contents: write | |
steps: | |
- name: Get Dependabot metadata | |
id: dependabot-metadata | |
uses: dependabot/[email protected] | |
- name: Enable auto-merge for PR | |
run: | | |
gh pr merge --auto --rebase "$PR" | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
PR: ${{ github.event.pull_request.html_url }} | |
- name: Approve patch and minor updates | |
if: ${{ steps.dependabot-metadata.outputs.update-type == 'version-update:semver-patch' || steps.dependabot-metadata.outputs.update-type == 'version-update:semver-minor' }} | |
run: | | |
gh pr review "$PR" --approve --body "Automatically **approving** this pull request because it includes a **patch or minor** update." | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
PR: ${{ github.event.pull_request.html_url }} | |
- name: Comment on major updates | |
if: ${{ steps.dependabot-metadata.outputs.update-type == 'version-update:semver-major' }} | |
run: | | |
gh pr comment "$PR" --body "Requires manual approval due to **major update**." | |
gh pr edit "$PR" --add-label "dependabot-major" | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
PR: ${{ github.event.pull_request.html_url }} | |