I have a GitHub Action workflow that can be triggered in two ways, via a pull_request and via a workflow dispatch.
During a pull_request, one of the steps generates an output that will be used in a later step.
During a workflow dispatch, the user manually inputs a custom output that will be used in a later step.
How do I use that output without running into null values? I can only use one of those outputs to assign to a variable, how can I make it accept both?
How about utilizing github.event_name
context to conditionally set a variable that you can use in a later step? It requires an extra step, but is relatively straightforward.
my-workflow.yml
:
name: Use input or job output
on:
workflow_dispatch:
inputs:
my_input:
description: Some workflow input
type: string
pull_request:
jobs:
job1:
runs-on: ubuntu-latest
steps:
- id: set_output
run: echo "my_output=helloworld" >> $GITHUB_OUTPUT
- name: Save job output OR workflow dispatch to Env
run: |
if [[ ${{ github.event_name == 'workflow_dispatch' }} == true ]]; then
my_variable=${{ inputs.my_input }}
else
my_variable=${{ steps.set_output.outputs.my_output }}
fi
echo "my_variable=$my_variable" >> $GITHUB_ENV
- name: Use the variable from previous step
run: echo ${{ env.my_variable }}
If you run this manually from a workflow_dispatch
, env.my_variable
will be whatever string you input. If not (triggered by a pull request), env.my_variable
will be my_output
from the set_output
step.