Github Actions / workflow_run / outputs - github

I have a workflow build whose output is store in a docker registry,
depending on its outcome, then, I would like to run a e2e test.
I know I can use workflow_run but it's not clear how to pass outputs to the dependant workflow.
on:
workflow_run:
workflows: ["Build"]
types: [completed]
I should be able to grab the IMAGE_URL output and run tests on that specific artefact.
how can I set a workflow output
how can I read a workflow output
Current workaround is using workflow_dispatch, but it has the drawback of not being listed as PR check.

You can write your variables and values, which you want to pass, to a file and upload it as an artifact in the triggering workflow.
In the triggered workflow, download the artifact of the triggering workflow run. Then parse the file to get your variables.
Triggering workflow
[...]
name: Build
jobs:
aJob:
name: A job
runs-on: ubuntu-latest
steps:
- run: echo "aVariable,aValue" > vars.csv
- uses: actions/upload-artifact#v2
with:
name: variables
path: vars.csv
Triggered workflow
(artifacts from other workflows can't be downloaded with the action download-artifact)
on:
workflow_run:
workflows: ["Build"]
types: [completed]
jobs:
aJob:
name: A job
runs-on: ubuntu-latest
steps:
- uses: actions/github-script#v4
id: get-artifact-id
with:
result-encoding: string
script: |
const result = await octokit.request('GET /repos/{owner}/{repo}/actions/runs/{run_id}/artifacts', {
owner: '${{github.repository_owner}}',
repo: '${{github.event.repository.name}}',
run_id: ${{github.event.workflow_run.id}}
})
# assumes the variables artifact is the only one in this workflow
return result.data.artifacts[0].artifact_id
- name: Get result
run: |
echo "${{steps.get-artifact-id.outputs.result}}"
curl -L -H "Authorization: token ${{github.token}}" \
-H "Accept: application/vnd.github.v3+json" \
-O variables.zip \
https://api.github.com/repos/${{github.repository}}/actions/artifacts/${{steps.get-artifact-id.outputs.result}}/zip
unzip variables.zip
# parse variables from variables.csv and set them

You can declaratively access artifacts from the workflow_run event this way:
- name: Download BuildMetadata
if: github.event_name == 'workflow_run'
uses: dawidd6/action-download-artifact#v2
with:
workflow: 'Build'
workflow_conclusion: success
github_token: ${{ secrets.GITHUB_TOKEN }}
run_id: ${{ github.event.workflow_run.id }}
run_number: ${{ github.event.workflow_run.run_number }}
name: build.meta.json
Also checkout github.event.workflow_run.artifacts_url
https://github.com/dawidd6/action-download-artifact

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.

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.

How to use github-secret in github action workflow?

so I have this code:
name: run-script
on: push
jobs:
run_tests:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout#v2
- name: Run script file
run: |
echo {here should be the secret} > ~/id_rsa
shell: bash
On my git action, where {here should be the secret} I want to put the variable, which is a secret token saved as a repo secret.
How can this be done?
Thank you for your help.
Assuming you have a secret named TOKEN, you can use it like so:
name: run-script
on: push
jobs:
run_tests:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout#v3
- name: Run script file
run: |
echo ${{ secrets.TOKEN }} > ~/id_rsa
shell: bash
Unrelated to how to use secrets, please note that > will override the contents of ~/id_rsa.
Secondly, if you want to do something with your private key (which is my guess based on the filename), the correct file would be in ~/.ssh/id_rsa.
And lastly, note that I have changed the checkout action to v3 as that's the latest available version.

GitHub Actions runing build and unit test coverage report

I'm trying to set up a GitHub Action to run build and coverage report using Jest but the workflow that I have set up seems wrong:
# Configure integration.
name: 👷 Build
# Triggers the workflow on push or pull request events
on: [push, pull_request]
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out the repository under $GITHUB_WORKSPACE
- uses: actions/checkout#v2
# Install dependencies
- name: Install
run: yarn install
# Build the app
- name: Build
run: yarn build
# Get test coverage
- uses: ziishaned/jest-reporter-action#v0.0.1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
test-command: 'yarn test:unit:coverage'
GitHub Action that I used
it seems the with have two space more
- uses: ziishaned/jest-reporter-action#v0.0.1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
test-command: 'yarn test:unit:coverage'

git actions save workflow trigger to variable

I wrote a Github action workflow of which will be triggered only when some specific files were updated:
name: CI
on:
push:
paths:
### If a push was applied on one of these files, the CI workflow is triggered.###
### I want to know which file triggered the CI workflow and save it to a variable s I can use later in the CI steps ###
- 'dwh/helm/values-versions.yaml'
- 'ai/helm/values-versions.yaml'
- 'platform/helm/values-versions.yaml'
jobs:
copy-values-template-to-fluent-bit:
runs-on: self-hosted
container:
image: ghcr.io/***/myImage
credentials:
username: ${{ secrets.GHCR_USER }}
password: ${{ secrets.GHCR_PASS }}
steps:
- uses: actions/checkout#v2
- name: show repo files
run: |
pwd
ls -l
I need a way to figure out which file triggered the CI workflow and save it to a variable.
You can use this action Get All Changed Files:
- id: files
uses: jitterbit/get-changed-files#v1
- run: |
for changed_file in ${{ steps.files.outputs.all }}; do
echo "Do something with this ${changed_file} to check if this is you file and set variable."
done