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