
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.
If using a Linux runner, such as Ubuntu, run can be used to issue the echo command to create a variable (foo in this example) with a value (Hello World in this example) and then redirect the output to $GITHUB_OUTPUT.
If you want to use the output in the same job, you would then use steps.<id>.outputs.<variable name> which would be steps.greeting.outputs.foo in this example.
name: GitHub Action (POC)
run-name: ${{ github.workflow }} run by ${{ github.actor }}
on:
workflow_dispatch:
jobs:
poc:
runs-on: ubuntu-latest
steps:
- name: echo Hello World
id: greeting
run: echo "foo=Hello World" >> $GITHUB_OUTPUT
- name: echo foo
run: echo ${{ steps.greeting.outputs.foo }}
If you want to run a command in the runner VM, you can use the following syntax.
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: response=$(curl --silent --output /dev/null --write-out "%{http_code}" https://www.example.com/); echo "response=$response" >> $GITHUB_OUTPUT
- name: echo command response
run: echo ${{ steps.command.outputs.response}}
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) }}'
You can​ also
Did you find this article helpful?
If so, consider buying me a coffee over at