Github action how to access the inputs - github

on:
workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
required: true
default: 'warning'
tags:
description: 'Test scenario tags'
I have the following inputs:
Which branch do I want to use
Input fields
How do I access the information that I enter and then use it later in the github action script?

You now (June 2022) have an alternative:
You could try and take advantage of a new feature (June 2022)
GitHub Actions: Inputs unified across manual and reusable workflows
Workflows triggered by workflow_dispatch and workflow_call can now access their inputs using the inputs context.
Previously workflow_dispatch inputs were in the event payload.
This made it difficult for workflow authors who wanted to have one workflow that was both reusable and manually triggered.
Now a workflow author can write a single workflow triggered by workflow_dispatch and workflow_call and use the inputs context to access the input values.
For workflows triggered by workflow_dispatch, inputs are still available in the github.event.inputs context to maintain compatibility.
Using the inputs context in GitHub Actions.
In your case:
jobs:
printInputs:
runs-on: ubuntu-latest
steps:
- run: |
echo "Log level: ${{ inputs.logLevel }}"
echo "Tags: ${{ inputs.tags }}"

To use an input from the workflow_dispatch trigger on your yaml file, you need to use the following syntax ${{ github.event.inputs.<input_name> }} or ${{ inputs.<input_name> }}.
For example, in your case:
on:
workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
required: true
default: 'warning'
tags:
description: 'Test scenario tags'
jobs:
printInputs:
runs-on: ubuntu-latest
steps:
- run: |
echo "Log level: ${{ github.event.inputs.logLevel }}"
echo "Tags: ${{ github.event.inputs.tags }}"
Here is a simple demo if you want to check:
workflow file
workflow run
You can also check the official documentation about it.

Related

How to pass run number from one workflow to another?

I have 2 workflows
First runs tests and generated allure report and second builds and deploys to github pages and sends a slack notification with the link to gh pages.
I am curious, how can I get run number of the first workflow and pass it to the second workflow as ${{github.run_number}}?
I've tried to pass just ${{github.run_number}}
You can configure the first WF to trigger the second WF and pass some inputs. At first, you need to define an input in the second workflow:
name: Second workflow (deploy)
on:
workflow_dispatch:
inputs:
run_number: # Exposing the input through which we can pass the value
description: 'The run number'
required: true
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Echo the input
run: echo ${{ github.event.inputs.run_number }}
Then, you can trigger this workflow from another workflow in the following way:
name: First Workflow (report)
on:
push:
branches:
- '*'
jobs:
allure-report:
runs-on: ubuntu-latest
steps:
# ... Allure report generation steps, etc.
- name: Invoke the deploy WF
uses: benc-uk/workflow-dispatch#v1
with:
workflow: deploy.yaml
inputs: '{ "run_number": "${{ github.run_number }}" }'
In this example, I've used the Workflow Dispatch GH Action to pass the current run_number to the deploy.yml WF as an input value.
To read more about workflow inputs see the Specifying inputs and workflow_dispatch event.

GitHub Actions - Reuse outputs from other reusable workflows

I'm not sure if it's possible, but I'm attempting to use outputs from one reusable workflow, in another, that's part of the same caller workflow. For reference, please see the configs below:
Caller Workflow:
jobs:
call-workflow-calver:
uses: ./.github/workflows/called-workflow1.yaml
secrets: inherit
call-workflow-echo:
needs: call-workflow-calver
uses: ./.github/workflows/called-workflow2.yaml
secrets: inherit
Job for creating the CalVer tag (it outputs as $VERSION as part of the action)
Called Workflow 1:
...
jobs:
calver:
name: Create CalVer tag
...
steps:
- name: Calver tag
uses: StephaneBour/actions-calver#1.4.4
if: ${{ github.ref == 'refs/heads/main' }}
id: calVer
with:
date_format: "%Y-%m-%d"
release: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
...
Trying to use the CalVer $VERSION output from another caller workflow
Called Workflow 2:
...
- name: Echo
run: |
echo ${{needs.calver.outputs.VERSION}}
...
The basic concept is I'm trying to use outputs from one reusable workflow in another for setting CalVer versions in workflow 1, calling that in workflow 2 so I can set it as an image version. I will eventually use in a 3rd reusable workflow to deploy said image. If this is possible, that would be great!
Hopefully, this all makes a bit of sense, but if anything needs clarifying, please do let me know!
Many thanks in advance!
According to the official documentation, you can now declare outputs to reusable workflows.
These work just like job outputs and are available via needs.<reusable>.outputs.<output> format once you declare the output.
Example
1. Reusable workflow configuration:
name: Reusable workflow
on:
workflow_call:
# Map the workflow outputs to job outputs
outputs:
firstword:
description: "The first output string"
value: ${{ jobs.example_job.outputs.output1 }}
secondword:
description: "The second output string"
value: ${{ jobs.example_job.outputs.output2 }}
jobs:
example_job:
name: Generate output
runs-on: ubuntu-latest
# Map the job outputs to step outputs
outputs:
output1: ${{ steps.step1.outputs.firstword }}
output2: ${{ steps.step2.outputs.secondword }}
steps:
- id: step1
run: echo "::set-output name=firstword::hello"
- id: step2
run: echo "::set-output name=secondword::world"
2. Workflow using the reusable:
name: Call a reusable workflow and use its outputs
on:
workflow_dispatch:
jobs:
job1:
uses: octo-org/example-repo/.github/workflows/called-workflow.yml#v1
job2:
runs-on: ubuntu-latest
needs: job1
steps:
- run: echo ${{ needs.job1.outputs.firstword }} ${{ needs.job1.outputs.secondword }}
Note that if a reusable workflow that sets an output is executed with a matrix strategy, the output will be the output set by the last successful completing reusable workflow of the matrix which actually sets a value. That means if the last successful completing reusable workflow sets an empty string for its output, and the second last successful completing reusable workflow sets an actual value for its output, the output will contain the value of the second last completing reusable workflow.
I used this workflow as example here if you want to check the logs of the workflow run.

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

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
Use environment variable in github action if
How to pass variable between two successive GitHub Actions jobs?
github-action: does the IF have an ELSE?
How to fail a job in GitHub Actions?
GitHub Actions - trigger another action after one action is completed
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:
and I'm basing my condition after reading "Run github actions step based on output condition" and reading:
Contexts
Expressions
Using conditions to control job execution
Can a condition come before a step and/or what is the correct way to run a step based off the previous step?
You are looking for job outputs, which allow you to send data to the following jobs.

GitHub Action workflow_call does not use up to date input values

I am encountering a strange behaviour with GitHub Action workflow_call.
Basically, everything works fine after the initial setup, but when I edit the input parameters of the workflow files it does not use the updated values. Hard to explain in words, so let me show you an example.
Consider this basic setup:
File: start-pipeline.yml
name: Start Pipeline
on:
workflow_dispatch:
inputs:
release_type:
required: true
type: choice
options:
- patch
- minor
- major
jobs:
call-sub-workflow:
uses: codezalot/workflow-call-issue/.github/workflows/sub-workflow.yml#main
with:
release_type: ${{ github.event.inputs.release_type }}
File: sub-workflow.yml
name: Sub Workflow
on:
workflow_call:
inputs:
release_type:
type: string
required: true
jobs:
my-job:
runs-on: ubuntu-latest
steps:
- name: Print Inputs
run: |
echo "Release Type: ${{ github.event.inputs.release_type }}"
I then start the pipeline with the value patch and the sub-workflow prints the input just fine:
I then update the workflow files and add an additional input value like this:
File: start-pipeline.yml
...
jobs:
call-sub-workflow:
uses: codezalot/workflow-call-issue/.github/workflows/sub-workflow.yml#main
with:
release_type: ${{ github.event.inputs.release_type }}
some_other_value: ${{ github.event.inputs.release_type }}
File: sub-workflow.yml
...
inputs:
...
some_other_value:
type: string
required: true
jobs:
...
run: |
echo "Release Type: ${{ github.event.inputs.release_type }}"
echo "Release Type: ${{ github.event.inputs.some_other_value }}"
I then run the pipeline again, once with release_type patch, once with minor.
The value some_other_value is missing however, see:
.
I have been playing around with this issue for hours, but I do not understand what is going wrong. For it does seem to be using the latest SHA versions of the files, just the input parameters cause an issue.
Does anyone know what's going on here?
According to the documentation: "When a workflow is triggered with the workflow_call event, the event payload in the called workflow is the same event payload from the calling workflow." In other words, the event parameter in the github context is the same for the called workflow as for the original workflow.
Consequently, there is no github.event.input.some_other_value parameter since the original workflow did not have this input. For inputs defined in a reusable workflow you can use the inputs context which contains the input properties passed to the reusable workflow.
To sum up, here is a working sub-workflow.yml file:
name: Sub Workflow
on:
workflow_call:
inputs:
release_type:
type: string
required: true
some_other_value:
type: string
required: true
jobs:
my-job:
runs-on: ubuntu-latest
steps:
- name: Print Inputs
run: |
echo "Release Type: ${{ inputs.release_type }}"
echo "Release Type: ${{ inputs.some_other_value }}"
Result:

GitHub Actions: How to view inputs for workflow_dispatch?

My idea here is to write my inputs from workflow_dispatch on each pipeline run. .
For example, in Bitbucket pipelines input parameters shown after custom -
Is there a way to do something similar for GitHub?
Although this does not directly answer your question, I'm adding it here because this is where I landed looking for the answer on how to output all my workflow inputs.
In my case I am using a workflow_dispatch trigger - YMMV if you are using a different trigger, but I suspect it would work the same way.
As with the other answer proposed, you will need to do this as a step within your job:
on:
workflow_dispatch:
inputs:
myInput:
default: "my input value"
jobs:
myJob:
steps:
- name: Output Inputs
run: echo "${{ toJSON(github.event.inputs) }}"
This will result in output you can view in your GitHub action execution output with the inputs serialized as JSON:
{
"myInput": "my input value"
}
If you have only a few simple input values (from workflow_dispatch) then you can include them in the name of the job:
on:
workflow_dispatch:
inputs:
my_value:
description: 'My input value'
required: true
default: 'foo'
type: string
jobs:
my_job:
name: "My job [my_value: ${{ github.event.inputs.my_value }}]"
runs-on: ubuntu-latest
steps:
....
This way you will be able to see the input directly in the GitHub UI.
You cannot really alter how they will be displayed on the list I'm afraid.
All you can do is to log your input variables inside action itself, like this:
jobs:
debugInputs:
runs-on: ubuntu-latest
steps:
- run: |
echo "Var1: ${{ github.event.inputs.var1 }}"
echo "Var2: ${{ github.event.inputs.var2 }}"
If you want to see them in summary, you can use a notice or warning message mark:
I was looking for something similar and landed on logging + writing to the Job Summary.
I created a small action that can easily be used as a first step in your workflow, since I found myself need