Docker: improve tagging, distinguish main
and latest
#551
Workflow file for this run
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: Docker | |
on: | |
push: | |
branches: | |
- main | |
paths-ignore: | |
- web/** | |
- doc/** | |
pull_request: | |
branches: | |
- main | |
paths-ignore: | |
- web/** | |
- doc/** | |
# Publish `v1.2.3` tags as releases. | |
tags: | |
- v* | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref }} | |
cancel-in-progress: true | |
env: | |
IMAGE_NAME: conky | |
DOCKERHUB_ACCOUNT: brndnmtthws | |
DOCKER_BUILDKIT: 1 | |
jobs: | |
docker-buildx: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
- name: Set up Docker Buildx | |
id: buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Log into Dockerhub registry | |
run: echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login -u $DOCKERHUB_ACCOUNT --password-stdin | |
- name: Build and push Docker image | |
env: | |
RELEASE: "${{ startsWith(github.ref, 'refs/tags/') && 'ON' || 'OFF' }}" | |
run: | | |
set -ex | |
DOCKERHUB_IMAGE_ID=$DOCKERHUB_ACCOUNT/$IMAGE_NAME | |
# Change all uppercase to lowercase | |
DOCKERHUB_IMAGE_ID=$(echo $DOCKERHUB_IMAGE_ID | tr '[A-Z]' '[a-z]') | |
image_tags=() | |
# Strip git ref prefix from version | |
VERSION_TAG=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') | |
# Strip "v" prefix from tag name | |
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION_TAG=$(echo $VERSION_TAG | sed -e 's/^v//') | |
image_tags+=("--tag" "$DOCKERHUB_IMAGE_ID:$VERSION_TAG") | |
if [ "$RELEASE" == "ON" ]; then | |
# tag as latest on releases | |
image_tags+=("--tag" "$DOCKERHUB_IMAGE_ID:latest") | |
fi | |
# Only build amd64 on PRs, build all platforms on main. The arm builds | |
# take far too long. | |
image_platforms="--platform linux/amd64" | |
push_image="" | |
cache_tag="pr-cache" | |
if [ "${{ github.ref }}" == "refs/head/main" ]; then | |
# Only push on main | |
push_image="--push" | |
image_platforms="--platform linux/arm/v7,linux/arm64/v8,linux/amd64" | |
cache_tag="main-cache" | |
fi | |
docker buildx build \ | |
${push_image} \ | |
${image_platforms} \ | |
--cache-from=type=registry,ref=$DOCKERHUB_ACCOUNT/$IMAGE_NAME:$cache_tag \ | |
--cache-to=type=registry,ref=$DOCKERHUB_ACCOUNT/$IMAGE_NAME:$cache_tag,mode=max \ | |
"${image_tags[@]}" \ | |
. |