build docker image for each branch/PR?

This commit is contained in:
Ralf Vogler 2025-05-14 01:31:04 +02:00
parent a5ce5ec816
commit 7c8682ac91

View file

@ -2,24 +2,20 @@ name: Build and push Docker image (amd64, arm64 to hub.docker.com and ghcr.io)
on:
workflow_dispatch: # allow manual trigger
# https://github.com/orgs/community/discussions/26276
push:
branches:
- "main"
- "v*"
tags:
- "v*"
paths: # ignore changes to certain files
push: # build for each branch
# branches: ["main"]
paths: # ignore changes to .md files
- '**'
- '!*.md'
# - '!.github/**'
pull_request: # runs when opened/reopned or when the head branch is updated, see https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
branches:
- "main" # only PRs against main
pull_request: # runs when opened/reopned or when the head branch is updated
permissions:
contents: read
env:
BRANCH: ${{ github.head_ref || github.ref_name }} # head_ref/base_ref are only set for PRs, for branches ref_name will be used
jobs:
docker:
runs-on: ubuntu-latest
@ -30,13 +26,11 @@ jobs:
-
name: Set environment variables
run: |
BRANCH="${GITHUB_REF#refs/heads/}"
echo "BRANCH=$BRANCH" >> "$GITHUB_ENV"
echo "NOW=$(date -R)" >> "$GITHUB_ENV" # date -Iseconds; date +'%Y-%m-%dT%H:%M:%S'
echo "NOW=$(date -R)" >> $GITHUB_ENV # date -Iseconds; date +'%Y-%m-%dT%H:%M:%S'
if [[ "$BRANCH" == "main" ]]; then
echo "IMAGE_TAG=latest" >> "$GITHUB_ENV"
echo "IMAGE_TAG=latest" >> $GITHUB_ENV
else
echo "IMAGE_TAG=$BRANCH" >> "$GITHUB_ENV"
echo "IMAGE_TAG=$BRANCH" >> $GITHUB_ENV
fi
-
name: Set up QEMU
@ -47,7 +41,7 @@ jobs:
-
name: Login to Docker Hub
uses: docker/login-action@v3
if: github.event_name != 'pull_request' # TODO if DOCKERHUB_* are set?
if: ${{ secrets.DOCKERHUB_USERNAME != '' && secrets.DOCKERHUB_TOKEN != '' }}
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
@ -61,16 +55,15 @@ jobs:
-
name: Build and push
uses: docker/build-push-action@v6
# if: github.event_name != 'pull_request' # still want to build image
if: ${{ env.IMAGE_TAG != '' }}
with:
context: .
push: ${{ github.event_name != 'pull_request' }} # TODO push for forks?
push: ${{ secrets.DOCKERHUB_USERNAME != '' }}
build-args: |
COMMIT=${{ github.sha }}
BRANCH=${{ env.BRANCH }}
NOW=${{ env.NOW }}
platforms: linux/amd64,linux/arm64 # ,linux/arm/v7
# TODO docker tag only if DOCKERHUB_* are set?
platforms: linux/amd64,linux/arm64
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/free-games-claimer:${{env.IMAGE_TAG}}
ghcr.io/${{ github.actor }}/free-games-claimer:${{env.IMAGE_TAG}}