name: Build and push Docker image (amd64, arm64 to hub.docker.com and ghcr.io) on: workflow_dispatch: # allow manual trigger 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 permissions: contents: read packages: write 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 steps: - name: Checkout uses: actions/checkout@v4 - name: Set environment variables run: | 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 else echo "IMAGE_TAG=$BRANCH" >> $GITHUB_ENV fi - name: Set up QEMU uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Login to Docker Hub uses: docker/login-action@v3 # if: ${{ secrets.DOCKERHUB_USERNAME != '' && secrets.DOCKERHUB_TOKEN != '' }} # does not work: Unrecognized named-value: 'secrets' - https://www.cloudtruth.com/blog/skipping-jobs-in-github-actions-when-secrets-are-unavailable-securely-inject-configuration-secrets-into-github with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Login to GitHub Container Registry uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.actor }} # actor is user that opened PR, was repository_owner before password: ${{ secrets.GITHUB_TOKEN }} - name: Build and push uses: docker/build-push-action@v6 if: ${{ env.IMAGE_TAG != '' }} with: context: . push: ${{ secrets.DOCKERHUB_USERNAME != '' }} build-args: | COMMIT=${{ github.sha }} BRANCH=${{ env.BRANCH }} NOW=${{ env.NOW }} 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}} cache-from: type=gha cache-to: type=gha,mode=max