Bootstrap FreeKB - GitHub Actions - Getting Started with Output
GitHub Actions - Getting Started with Output

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.

It's important to recognize that there is no need to append key/value pairs to the builtin $GITHUB_OUTPUT variable if you want to define and use a variable in a single step. In this example, since the $server variable is defined and used in the same step, there is no need to append the server variable to the builtin $GITHUB_OUTPUT variable.

name: my workflow
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  workflow_dispatch:
    inputs:  
      environment:
        type: choice
        description: environment
        required: true        
        options: 
        - development
        - production  
jobs:  
  my-job:
    runs-on: ubuntu-latest
    steps:
      - name: run example.py
        id: greeting
        run: |
          if [ ${{ inputs.environment}} == "development" ] ]; then
          	server="server1.example.com"
          else
            server="server2.example.com"
          fi
          ssh -o StrictHostKeyChecking=no john.doe@$server "/path/to/example.py"

 

On the other hand, if you want a variable defined in a step to be usable in a subsequent step, then you will want to append the variable to the builtin $GITHUB_OUTPUT variable.

name: my workflow
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  workflow_dispatch:
jobs:  
  my-job:
    runs-on: ubuntu-latest
    steps:
      - name: define my_variable
        id: my-step
        run: echo "my_variable=my_value" >> $GITHUB_OUTPUT

      - name: Use output variable
        run: echo "The value is ${{ steps.my-step.outputs.my_variable }}"

 

Here is a bit of a more practical example.

name: my workflow
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  workflow_dispatch:
    inputs:  
      environment:
        type: choice
        description: environment
        required: true        
        options: 
        - development
        - production  
jobs:  
  my-job:
    runs-on: ubuntu-latest
    steps:
      - name: define server
        id: xserver
        run: |
          if [ ${{ inputs.environment}} == "development" ] ]; then
          	server="server1.example.com"
          else
            server="server2.example.com"
          fi
          echo "server=${server}" >> $GITHUB_OUTPUT  
    
      - name: run example.py
        id: greeting
        run: ssh -o StrictHostKeyChecking=no john.doe@${{ steps.xserver.outputs.server }} "/path/to/example.py"

 

Be aware that if you are running a command that returns lines of output separated by new lines, perhaps something like this.

~]$ gh pr diff 123
foo.txt
bar.txt

 

You will almost always need to replace the newlines with whitespace so that the output is returned as a single line of output.

~]$ gh pr diff 123 | sed ':label; N; $! b label; s|\n| |g'
foo.txt bar.txt

 

Thus your GitHub Action would look something like this.

name: GitHub Action (POC)
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  workflow_dispatch:
  
jobs:  
  poc:
    runs-on: ubuntu-latest
    steps:
      - name: get command response
        id: command
        run: echo "response=$(gh pr diff 123 | sed ':label; N; $! b label; s|\n| |g')" >> $GITHUB_OUTPUT

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

 

Here is an example of how to print the GitHub Event as JSON.

name: GitHub Action (POC)
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  workflow_dispatch:
  
jobs:  
  poc:
    runs-on: ubuntu-latest
    steps:
      - name: Print GitHub Event
        run: echo '${{ toJSON(github.event) }}'

 

Sometimes, it may make more sense to instead use GITHUB_ENV.

name: my workflow
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  workflow_dispatch:
jobs:  
  my-job:
    runs-on: ubuntu-latest
    steps:
      - name: define my_variable
        id: my-step
        run: echo "my_variable=my_value" >> $GITHUB_ENV

      - name: Use output variable
        run: echo "The value is $my_variable"

 

This is a nice approach if you have multiple steps that may update the value of a variable.

name: my workflow
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
  workflow_dispatch:
    inputs:  
      environment:
        type: choice
        description: environment
        required: true        
        options: 
        - development
        - staging
        - production  
jobs:  
  my-job:
    runs-on: ubuntu-latest
    steps:
      - name: define ENVIRONMENTS
        run: echo "ENVIRONMENTS=" >> $GITHUB_ENV

      - name: append dev to ENVIRONMENTS
        if: ${{ inputs.development }}
        run: echo "ENVIRONMENTS=$ENVIRONMENTS,dev" >> $GITHUB_ENV
        
      - name: append stage to ENVIRONMENTS
        if: ${{ inputs.staging}}
        run: echo "ENVIRONMENTS=$ENVIRONMENTS,stage" >> $GITHUB_ENV
        
      - name: append prod to ENVIRONMENTS
        if: ${{ inputs.production }}
        run: echo "ENVIRONMENTS=$ENVIRONMENTS,prod" >> $GITHUB_ENV                                                

 

You can​ also




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 8932e8 in the box below so that we can be sure you are a human.