Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a dynamic tag in github actions workflow

I've setup a CI flow on github actions that creates a docker container in the first job. This container image is used for later jobs, so that all building of dependencies and testing happens in the same environment.

name: CI

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]
  workflow_dispatch:

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  build-container:
    runs-on: ubuntu-latest
    container: docker:20.10.24-git
    permissions:
      contents: read
      packages: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          submodules: True
      - name: Log in to Container registry
        uses: docker/login-action@v2
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
      - name: Build and push Docker image
        uses: docker/build-push-action@v4
        with:
          context: .
          file: diana/docker/Dockerfile.tvm
          push: true
          labels: ${{ steps.meta.outputs.labels }}
          tags: ${{ steps.meta.outputs.tags }}
    outputs:
      container_tag: ${{ steps.meta.outputs.tags }}
  
  build-htvm-release:
    runs-on: ubuntu-latest
    container:
      image: 'ghcr.io/kuleuven-micas/htvm:main'
    needs: build-container
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          submodules: True
      - name: Build HTVM Release
        run: |
          mkdir build
          cp diana/config.cmake build
          cd build
          cmake ..
          make -j$(nproc)
          cd ..
      - name: Store HTVM Build
        uses: actions/upload-artifact@v3
        with:
          name: build
          path: build

This flow works, however it fails when a person makes a PR that creates a change to the docker image.

This is because the the first job will push a new image to the registry (e.g. ghcr.io/htvm:pr-7), but the later jobs always use the main tag, so only after the PR is accepted the CI can actually pass, which is obviously not ideal.

I've tried to change build-htvm-release.container.image line from ghcr.io/kuleuven-micas/htvm:main to 'ghcr.io/kuleuven-micas/htvm:${{ needs.build_container.outputs.container_tag}}' But this constantly seems to fail. It appears that github actions does not like me to put a variable in that image line. So now the CI just fails because ghcr.io/kuleuven-micas/htvm: is not a valid image reference.

Does anyone have any suggestions to make this possible with minimal adaptation of the current flow?

Thanks in advance!

like image 507
JosseVanDelm Avatar asked Aug 09 '26 12:08

JosseVanDelm


1 Answers

I found out that I had already asked myself this question before and found an answer to the question and posted it here: Github Actions dynamic container name

This question is about using a dynamic name, but the solution can also be used for dynamic tags.

If someone could mark this as a duplicate that would be great, thanks!

like image 179
JosseVanDelm Avatar answered Aug 11 '26 07:08

JosseVanDelm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!