-1

I have the following action that first runs a semantic release and then builds a docker image. At the moment the image is tagged with master but I want it to be tagged with the release i.e. v1.0.8

I assumed that ${{ github.ref_name }} would refer to the release number that was created in the previous step rather than the branch name.

Is there a different variable I need to be using?

name: Release

on:
  push:
    branches: [ master ]

jobs:
  release:
    permissions:
      contents: write
      issues: write
      pull-requests: write
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: 18
      - run: npm ci
      - run: npm run build
      - run: npx semantic-release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  build:
    needs: release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Build the Docker image
        run: docker build . --file Dockerfile --tag gitflow-test-gui:${{ github.ref_name }}
      - name: Log in to the Container registry
        uses: docker/login-action@v1 
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Push the latest Docker image
        uses: docker/build-push-action@v2
        with:
          push: true
          tags: ghcr.io/ioncoder/gitflow-test-gui:latest
      - name: Push the Docker image with the release tag
        uses: docker/build-push-action@v2
        with:
          push: true
          tags: ghcr.io/ioncoder/gitflow-test-gui:${{ github.ref_name}}

PrestonDocks
  • 4,851
  • 9
  • 47
  • 82

1 Answers1

0

I didn't use semantic-release before, but as I read it releases your Node packages and it takes care of running something like npm version patch.

What I would do is to read the new version from package.json either with npm pkg get version or cat package.json | jq -r .version and save the input to a variable that I would output from my release job

jobs:
  release:
    outputs:
      pkg-version: ${{ steps.get-version.outputs.pkg-version }}
    permissions:
      contents: write
      issues: write
      pull-requests: write
    runs-on: ubuntu-latest
    steps:
      # ...
      - run: npx semantic-release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Export version
        id: get-version
        run: |
          MY_PKG_VERSION=$(npm pkg get version)
          echo "pkg-version=$MY_PKG_VERSION" >> $GITHUB_OUTPUT
      
  build:
    needs: release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Build the Docker image
        run: docker build . --file Dockerfile --tag gitflow-test-gui:${{ needs.release.outputs.pkg-version }}

github.ref context returned master as you are running this workflow on push to master:

on:
  push:
    branches: [ master ]

If you want to run it when creating a Github release, you should use on release published event instead:

on:
  release:
    types: [published]
Fcmam5
  • 4,888
  • 1
  • 16
  • 33