docker / build-push-action

GitHub Action to build and push Docker images with Buildx
https://github.com/marketplace/actions/build-and-push-docker-images
Apache License 2.0
4.36k stars 558 forks source link

Quesion regarding default environment variables #786

Closed aduzsardi closed 1 year ago

aduzsardi commented 1 year ago

Hi, I just started using GH Actions i'm confused as to why the GITHUB_RUN_NUMBER is not working in this action withing a workflow For example

name: CI
on:
  push:
    branches:
      - "master"
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Docker Setup Buildx
        uses: docker/setup-buildx-action@v2.3.0

      - name: Build and push Docker images
        uses: docker/build-push-action@v3.3.0
        with:
          context: .
          push: true
          tags: |
            ghcr.io/${{ github.repository }}:latest
            ghcr.io/${{ github.repository }}:$GITHUB_RUN_NUMBER
      # or
            ghcr.io/${{ github.repository }}:${ GITHUB_RUN_NUMBER }
      # or
            ghcr.io/${{ github.repository }}:${{ env.GITHUB_RUN_NUMBER }}
crazy-max commented 1 year ago

This is the right syntax: ghcr.io/${{ github.repository }}:${{ env.GITHUB_RUN_NUMBER }}

aduzsardi commented 1 year ago

Thank you @crazy-max , i tried that but when it's building the image the variable reference is empty. I've seen people setting it at an earlier step , shouldn't this be available in all steps since it's a default env variable ?

/usr/bin/docker buildx build --iidfile /tmp/docker-build-push-2lFKFi/iidfile --tag ghcr.io/aduzsardi/test-cicd:latest --tag ghcr.io/aduzsardi/test-cicd: --metadata-file /tmp/docker-build-push-2lFKFi/metadata-file --push . ERROR: invalid tag "ghcr.io/aduzsardi/test-cicd:": invalid reference format Error: buildx failed with: ERROR: invalid tag "ghcr.io/aduzsardi/test-cicd:": invalid reference format

crazy-max commented 1 year ago

Maybe using the github context? ghcr.io/${{ github.repository }}:${{ github.run_number }} ?

aduzsardi commented 1 year ago

Yep , that works. Well ¯_(ツ)_/¯ , i must have been reading something wrong.

Thank you!