On: workflow_run: does not work with release created

Hi, I’m having some problems with GitHub Actions and I really can’t find any useful documentation about that.

To summarize:
Expected = Create Release(UI) → CI runs → [once completed] CD runs
But I got = Create Release(UI) → CI runs | CD is NOT triggered

CI

name: CI

# triggers
on:
  push:
    branches: ['*']
  release:
    types: [ created ]
  pull_request:
    branches:
      - master

# env variables
env:
  CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

# jobs
jobs:
  build:

    runs-on: ubuntu-latest
    strategy:
      matrix:
        # supported scala versions
        scala:
          - 2.12.8
          - 2.13.4

    steps:
      - uses: actions/checkout@v2

      #----------- JDK -----------
      - name: Set up JDK 11
        uses: actions/setup-java@v1
        with:
          java-version: 11

      #----------- CACHE -----------
      - name: Cache SBT
        uses: actions/cache@v2.1.4
        with:
          # A list of files, directories, and wildcard patterns to cache and restore
          path: |
            ~/.ivy2/cache
            ~/.sbt
          # An explicit key for restoring and saving the cache
          key: ${{ runner.os }}-sbt-${{ matrix.scala }}-${{ hashFiles('**/build.sb') }}

      #----------- COMPILE -----------
      - name: Compile, Format, Test and Coverage for ${{ matrix.scala }}
        run: sbt ++${{ matrix.scala }} clean scalafmtCheck coverage test coverageReport

CD

name: CD

# triggers
on:
  workflow_run:
    workflows: ["CI"]
    branches:
      - master
    types:
      - completed

# env variables
env:
  SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }}
  SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }}
  PGP_SECRET: ${{ secrets.PGP_SECRET }}

# jobs
jobs:
  deploy:
    runs-on: ubuntu-latest
    if: github.event_name != 'pull_request'
    steps:
      - uses: actions/checkout@v2

      #----------- JDK -----------
      - name: Set up JDK 11
        uses: actions/setup-java@v1
        with:
          java-version: 11

      #----------- CACHE -----------
      - name: Cache SBT
        uses: actions/cache@v2.1.4
        with:
          # A list of files, directories, and wildcard patterns to cache and restore
          path: |
            ~/.ivy2/cache
            ~/.sbt
          # An explicit key for restoring and saving the cache
          key: ${{ runner.os }}-sbt-${{ matrix.scala }}-${{ hashFiles('**/build.sb') }}

      #----------- RELEASE -----------
      - name: Release
        run: sbt clean compile ci-release

Can someone tell me why ? Am I wrong or is this a bug ?
Thank you in advice for the help :slight_smile:

Do you have both workflow files in the same repo?

Yes for sure, this is the project

CI runs, CD no

huh. First glance it looks right, but we are missing something, logic or otherwise. I’ll take a look at it later today when I get a chance in more detail, and see if I can recreate it.

Question: What about your other triggers in the CI? For example, if you do a PR to master, not create a release, does that trigger the CD? Or does the CD still not run?

Thank you!
Yes CI works fine for PR on master and CD is triggered once CI is successfully completed, this creates the snapshot versions.

interesting, so it only occurs when the Release creation triggers the CI. Good to know.

Exactly, is seems so. Once I create the Release CI works but once completed CD is not triggered

Another question: RIght now you have two workflow files CI and CD. Have you considered just doing it all in one workflow file instead? Any reason you don’t want to do that?

Just to keep them clean and separated, no other reasons honestly. Do you think this can be problematic?

Not necessarily problematic. But in general right now, putting things in one file can be a little simpler. You can still keep it clean by using multiple jobs.

Ok thanks for the advice!

That said I still want to figure out why this scenario isn’t working. Still looking into it.

Any news? Did you replicate it?

OK, I was able to recreate it, and I know why it is happening.

This repo is where I was working on it: GitHub - mickeygoussetorg/deleteme

The problem is in your CD triggers, and in what is in the GitHub context object when you trigger off a created release vs when you just do a push.

If you just do a push, then the “ref” in the GitHub context object is refs/heads/main (or master, depending on what you use for your default branch). You can see that here.

However, when you trigger off a release, the ref is instead: refs/tags/releasetag. You can see that here.

Therefor, when the CD does its check, one thing you have it checking for is branch: master. Well, the CI wasn’t triggered from branch master it was triggered from refs/tags/releasetag, so you logic equates to false and it doesn’t run.

You can test this yourself by commenting out branch: master in your CD, and you’ll see that once you create a release, the CI runs, and then the CD runs.

Its a logic issue. You need to rethink your logic.

5 Likes

Great! Thank you!
So maybe I can use this check instead github.ref == 'refs/tags/releasetag', right ? I mean, if there is no possibility to check the release target branch.

UPDATE:
Just tried but still not triggered

github.ref == 'refs/tags/releasetag'

This line isn’t going to work, because you didn’t call your release “releasetag”. You called it v2.3.1. So the logic is going to evaluate to false, because the tag didn’t match.

I’m out of office for the next few days, but I’ll see if I can wrap my head around a way to figure out the logic on this.