I have a workflow which runs a test job under given circumstances automatically, or I can run it manually:
name: Test
on:
workflow_dispatch:
inputs:
used-branch:
description: Branch the test shall be run on
default: master
required: true
push:
branches: ['main', 'master']
tags: ['!**']
pull_request:
types: [opened, reopened, ready_for_review]
jobs:
test:
steps:
- name: Checkout selected branch
uses: actions/checkout@v3
if: inputs.used-branch != ''
with:
ref: ${{ github.event.inputs.used-branch }}
- name: Checkout current branch
uses: actions/checkout@v3
with:
ref: ${{github.ref}}
if: inputs.used-branch == ''
- name: Run test
...
I want the test be required before merging. So I check Require status check to pass before merging, Require branches to be up to date before merging and specify test as required job in the repo's branch settings.
The problem is: when I run the workflow manually (and therefore inject the branch via a variable), it's not related to the branch and its success won't be "discovered" by the PR checks.
Is there another way to link the run to a branch, or another way to propagate the result to the branch's pull request?
There is a way to do this via the api. You can run a step conditionally based on your manual input, and use the github.checks.create action from github-script.
Assuming that you give your step name: Run test and id of tests-step, here is an example:
- name: Report tests check
if: ${{ github.event.inputs.used-branch && steps.tests-step.outcome }}
uses: actions/github-script@v3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
github.checks.create({
name: 'run tests',
head_sha: '${{ github.event.issue.pull_request.head.sha }}',
status: 'completed',
conclusion: '${{ steps.tests-step.outcome }}',
output: {
title: 'Run tests',
summary: 'Results: ${{ steps.tests-step.outcome }}'
},
owner: context.repo.owner,
repo: context.repo.repo
})
Note that GITHUB_TOKEN is automatically built in to your workflows.
The head_sha parameter is tricky and needs to be there for the commit annotation to work. The issue property of github.event.issue in this case refers to the PR (github sees it as an "issue") that will be annotated.
The trouble here is that if you run your workflow using workflow_dispatch, you will not have access to this value, since there is no PR in the context of a manual run.
One solution would be to have an input requiring the PR number, and a script to get the sha. Here is how you can do that:
- name: get sha from PR number and save output
id: get-sha
uses: actions/github-script@v3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
console.log('PR number: ' + ${{ github.event.issue.number }})
const pr = await github.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: ${{ github.event.issue.number }}
})
console.log('PR Head sha: ' + pr.data.head.sha)
core.setOutput('sha', pr.data.head.sha)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With