Bootstrap FreeKB - GitHub Actions - Passing output between jobs
GitHub Actions - Passing output between jobs

Updated:   |  GitHub Actions articles

GitHub Actions can be used to do something whenever something happens in one of your GitHub repositories. If you are not familiar with GitHub Actions, check out my article Getting Started with GitHub Actions.

This also assumes you are familiar with Output. If not, check out my article GitHub Actions - Getting Started with Output.

You can

Here is an example of how you can create a variable named foo that contains a value of Hello World. 

name: GitHub Action (POC)
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  workflow_dispatch:
  
jobs:  
  poc:
    runs-on: ubuntu-latest
    steps:
      - name: Checking out the repository code
        uses: actions/checkout@v4

      - name: echo Hello World
        id: greeting
        run: echo "foo=Hello World" >> $GITHUB_OUTPUT

      - name: echo foo
        run: |
          echo ${{ steps.greeting.outputs.foo }}

 

And here is an example of how you could store the output of a curl command in GITHUB_OUTPUT and then use the output in another task in the job. In this example, the curl command should return "200" and then the "echo response" task should echo 200.

name: GitHub Action (POC)
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  workflow_dispatch:
  
jobs:  
  poc:
    runs-on: ubuntu-latest
    steps:
      - name: Checking out the repository code
        uses: actions/checkout@v4

      - name: curl https://www.example.com/
        id: code
        run: response=$(curl --silent --output /dev/null --write-out "%{http_code}" https://www.example.com/); echo "response=$response" >> $GITHUB_OUTPUT

      - name: echo response
        run: |
          echo ${{ steps.code.outputs.response }}

 

More realistically, you probably are going to be getting the response code from a GET request in job1 and then only execute job2 if the response code is 200 OK, like this.

name: GitHub Action (POC)
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  workflow_dispatch:
  
jobs:  
  job1:
    runs-on: ubuntu-latest
    steps:
      - name: Checking out the repository code
        uses: actions/checkout@v4

      - name: get http://www.example.com/ response code
        id: code
        run: response=$(curl --silent --output /dev/null --write-out "%{http_code}" http://www.example.com/); echo "response=$response" >> $GITHUB_OUTPUT

      - name: echo response
        run: |
          echo ${{ steps.code.outputs.response }}

    outputs:
      curl_response: ${{ steps.code.outputs.response }}          


  job2:
    runs-on: ubuntu-latest
    needs: job1
    if: needs.job1.outputs.curl_response == 200    
    steps:
      - name: Checking out the repository code
        uses: actions/checkout@v4

 

In this example, the response code in job1 was 200 OK.

 

Since job1 response code was 200 OK and job2 has "needs: job1" and "if: needs.job1.outputs.curl_response == 200" this evaluated to true thus job2 was also run.

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter efdcf6 in the box below so that we can be sure you are a human.