In a GitHub Action how to conditionalize a step based off the previous step's output?
Asked Answered
M

2

21

Building a GitHub action based on the commit message I'm trying to base a step on whether the commit message contains a particular string, set it to a variable and then in the next step check with a condition.

My current implementation of my action works:

name: Smoke Test
on:
  push:
    branches:
      - main

permissions:
  contents: read
  issues: write

jobs:
  smoking:
    runs-on: [ubuntu-latest]
    steps:
      - name: Run smoke tests
        if: ${{ !contains(github.event.head_commit.message, 'smoke_test') }}
        run: |
          echo 'Smoke Test not requested'
          exit 1
  stuff:
    needs: smoking
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: JasonEtco/create-an-issue@v2
        env:
          GITHUB_TOKEN: ${{ secrets.TOKEN }}
        with:
          filename: .github/ISSUE_TEMPLATE/smoke-test.md
        id: create-issue
      - run: 'echo Created issue number ${{ steps.create-issue.outputs.number }}'
      - run: 'echo Created ${{ steps.create-issue.outputs.url }}'

but with the implementation of:

exit 1

causes the action to indicate it error'ed out in the action panel and while that works that isn't technically accurate because I don't need it to error I just don't want the remaining steps to run.

I've tried setting a variable:

if: ${{ contains(github.event.head_commit.message, 'smoke_test') }}
with:
  run-smoke-test: true
run: |
  echo 'Smoke Test requested'

but it's not passing to the next step.

Research

Without relying on another GitHub action is there a way in step smoking to set an env variable that step stuff would need to validate for before running the step?

Edit

After reading the answer and implementing job outputs I've written:

name: Smoke Test
on:
  push:
    branches:
      - main

permissions:
  contents: read
  issues: write

jobs:
  commitMessage:
    runs-on: ubuntu-latest
    outputs:
      output1: ${{ steps.isSmoke.outputs.test }}
    steps:
      - id: isSmoke
        if: ${{ contains(github.event.head_commit.message, 'smoke_test') }}
        run: echo "::set-output name=test::true"

  smokeTest:
    runs-on: ubuntu-latest
    needs: commitMessage
    steps:
      - uses: actions/checkout@v2
      - uses: JasonEtco/create-an-issue@v2
        if: steps.isSmoke.output.test == true
        env:
          GITHUB_TOKEN: ${{ secrets.DEV_TOKEN }}
        with:
          filename: .github/ISSUE_TEMPLATE/smoke-test.md

but when the commit message of smoke_test is used it bypasses create-an-issue:

enter image description here

and I'm basing my condition after reading "Run github actions step based on output condition" and reading:

Can a condition come before a step and/or what is the correct way to run a step based off the previous step?

Marketing answered 3/5, 2022 at 15:3 Comment(0)
L
4

You are looking for job outputs, which allow you to send data to the following jobs.

Loveridge answered 4/5, 2022 at 22:57 Comment(0)
M
0

In the example you presented it looks like you want to condititionally execute a job based on the output of a step in a previous job.

This should work for you:

name: Smoke Test
on:
  push:
    branches:
      - main

permissions:
  contents: read
  issues: write

jobs:
  commitMessage:
    runs-on: ubuntu-latest
    outputs:
      output1: ${{ steps.isSmoke.outputs.test }}
    steps:
      - id: isSmoke
        if: ${{ contains(github.event.head_commit.message, 'smoke_test') }}
        run: echo "test=true" >> $GITHUB_OUTPUT

  smokeTest:
    runs-on: ubuntu-latest
    needs: commitMessage
    steps:
      - uses: actions/checkout@v2
      - uses: JasonEtco/create-an-issue@v2
        if: "${{ needs.commitMessage.outputs.output1 == 'true' }}"
        env:
          GITHUB_TOKEN: ${{ secrets.DEV_TOKEN }}
        with:
          filename: .github/ISSUE_TEMPLATE/smoke-test.md

note that set-output is deprecated and you should now just append a key value pair to $GITHUB_OUTPUT.


If on the other hand you wanted to conditionally execute another step in the first job then you would do something like this:

name: Smoke Test
on:
  push:
    branches:
      - main

permissions:
  contents: read
  issues: write

jobs:
  commitMessage:
    runs-on: ubuntu-latest
    outputs:
      output1: ${{ steps.isSmoke.outputs.test }}
    steps:
      - id: isSmoke
        if: ${{ contains(github.event.head_commit.message, 'smoke_test') }}
        run: echo "test=true" >> $GITHUB_OUTPUT
      
      - id: optionalStep
        if: ${{ steps.isSmoke.outputs.test  == 'true' }}
        run: ...
Maugre answered 20/6 at 15:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.