How to get the GitHub Pull Request ID Number - github-actions

Is there a way to get and set the Github’s pull request number (example PR #323 ) into Github Actions’s Environment Variable (without the hashtag)?
env:
GITHUB_PR_NUMBER: 323

on:
pull_request
env:
GITHUB_PR_NUMBER: ${{github.event.pull_request.number}}
jobs:
prJob:
name: Print event
runs-on: ubuntu-latest
steps:
- run: |
echo "$GITHUB_PR_NUMBER"
For more information, see https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads#pull_request

Related

How to use different runners for a specific value in matrix strategy

Is there a way to use a higher resource runner machine for a specific value in matrix strategy?
this is a sample GitHub action:
test-app:
runs-on: ubuntu-latest
timeout-minutes: 60
strategy:
matrix:
run: ['app1', 'app2', 'app3']
for app3 I need 2x resources on the runner machine. Could someone please help me? I searched a lot but could find the answer here on stackoverflow
You need two changes to make this happen:
Define an object to reference your runs-on values
Reference those in the job definition
I have only been able to do this by referencing outputs from a previous job:
jobs:
test-setup:
runs-on: ubuntu-latest
outputs:
runners: '{"app1":"ubuntu-latest", "app2": "ubuntu-latest", "app3": "ubuntu-22.04-16core"}'
steps:
- run: echo no-op
test-app:
needs: [test-setup]
runs-on: ${{ fromJSON(needs.test-setup.outputs.runners)[matrix.run] }}
timeout-minutes: 60
strategy:
matrix:
run: ['app1', 'app2', 'app3']
steps:
- run: echo ${{ matrix.run }}
This is the runs-on value: ${{ fromJSON(needs.test-setup.outputs.runners)[matrix.run] }}, it just pulls the string from the previous job's output

Github actions: Run step / job in a workflow if changes happen in specific folder

Is there any way for us to control what jobs/steps to run in a workflow based on the changes in a specific folder
Eg:
I have said, following folders in my git repo : a, b, c
On every PR merge to my branch I will trigger a workflow. The workflow will execute jobs say,
A -> B -> C. I want to run job A only if changes are present for folder "a/**", B for "b/**" and so on.
So, If in the PR changes only happen in "a/**"and "b/**" workflow will skip job execution for C, making the workflow run to be A->B
You could use the paths-filter custom action with if conditions at the jobs or step levels, using a setup job as preliminary to check if your specific path has been updated, saving the result as an output.
Here is an example
name: Paths Filter Example
on: [push, workflow_dispatch]
jobs:
paths-filter:
runs-on: ubuntu-latest
outputs:
output1: ${{ steps.filter.outputs.workflows }}
steps:
- uses: actions/checkout#v2
- uses: dorny/paths-filter#v2
id: filter
with:
filters: |
workflows:
- '.github/workflows/**'
# run only if 'workflows' files were changed
- name: workflow tests
if: steps.filter.outputs.workflows == 'true'
run: echo "Workflow file"
# run only if not 'workflows' files were changed
- name: not workflow tests
if: steps.filter.outputs.workflows != 'true'
run: echo "NOT workflow file"
next-job:
runs-on: ubuntu-latest
# Wait from the paths-filter to be completed before starting next-job
needs: paths-filter
if: needs.paths-filter.outputs.output1 == 'true'
steps:
...
That way, you could have something like this in your jobs: A --> B or A --> C depending on the path that has been updated.
Yes: https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows#registry_package
This is the syntax:
on:
push:
paths:
- 'a/**'
- 'b/**'

Get the name of variable from a JSON file in GitHub Actions

I am trying to setup the GitHub actions for deployment to the Azure. What I am trying to do is getting the name of some variables from the armtemplates with the given code.
name: Create Initial Resources
on:
push:
branches:
- CreateResources
jobs:
Read:
runs-on: ubuntu-latest
steps:
- name: Chekout branch
uses: actions/checkout#v2
with:
ref: CreateResources
- name: get storage account
run: |
echo ::set-output name=storage-account-name::$(jq '.parameters.storage_account_name.value' at ./armtemplates/sac/parameters-example.json)
This is the code that I use, first it checks the branch and in the second step it tries to parse the file that is in the branch but the error is like this:
jq: error: Could not open file at: No such file or directory
Issue here is at phrase. Please use this:
echo ::set-output name=storage-account-name::$(jq '.parameters.storage_account_name.value' ./armtemplates/sac/parameters-example.json)

Github Actions Workflow that runs when branch is main AND a matching tag is present [duplicate]

I want to trigger a Github workflow only if a code is pushed to a specific branch and if a tag exists, but my config (github workflow) does not work as expected:
name: Deployment
on:
push:
branches:
- feature/BRANCH-NAME
tags:
- *
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- uses: actions/setup-node#v1
with:
node-version: '10.x'
- name: Install dependencies
run: |
npm install
- name: Lint & build
run: |
npm run build
The workflow is triggered even if a tag does not exist.
How could I fix this?
EDIT: This workaround seemed to have solved my problem at the time of writing but I cannot guarantee that it still works as expected.
Since I couldn't find a way to implement an AND condition (i.e. tagged AND on master), I used the following workaround:
name: Worflow demo
on:
push:
tags:
- v*
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- name: Exit if not on master branch
if: endsWith(github.ref, 'master') == false
run: exit -1
- name: Next job ...
This will trigger if there is a tagged (e.g. tag v1.0.0) commit pushed:
on:
push:
tags:
- v*
The first step ('Exit if not on master branch') then checks if the current branch doesn't end with master and exits the workflow (the subsequent tests will not start):
- name: Exit if not on master branch
if: endsWith(github.ref, 'master') == false
run: exit -1
Hope this helps someone else as well.
can use release event and github.event.release.target_commitish to make only tags on 'my_branch' to trigger the build
name: workflow demo
on:
release:
types:
- published
jobs:
my_job:
runs-on: ubuntu-latest
steps:
- name: build only on my_branch tag
if: ${{ github.event_name == 'release' && github.event.release.target_commitish == 'my_branch'}}
run: "something"
To fix the multiple unintended runs I removed the "branches:" scalar and just include and !exclude tags that I want my workflow to run on.
Following runs on tagged releases, not on release candidates:
name: 'tagged-release'
on:
push:
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'
- '!*-rc[0-9]+'
The accepted answer didn't seem to work for me, as pointed out by dilithiummatrix in the comments.
So I tried outputting the available values of the github object, which you can do by adding this in your workflow file to see what is available:
- name: Dump job github var
env:
GITHUB_VAR: ${{ toJson(github) }}
run: echo "$GITHUB_VAR"
From this I noticed that as Billy Clark also pointed out, that github.event.base_ref contains refs/heads/production. So this worked for me:
# Only release from prod branch
- name: Exit if not on production branch
if: endsWith(github.event.base_ref, 'production') == false
run: exit -1
You can so by writing the following YAML code.
Keep in mind that you have to put the branches-ignore so that the workflow is not activated when you create branches. The part where you check whether the tag is pushed to a specific branch is covered in the second part of the answer.
name: Deployment
on:
push:
tags:
- *
branches-ignore:
- '*'
You can check for the name of the branch with the following code; specifically for every step of the job you are trying to accomplish.
- name: job
env:
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
if: contains(env.BRANCH_NAME, <branch-name>)
then follow up with some more code you want the workflow to do.

Github Action tmate - This check was cancelled

I've already attempted to ask about this on the repo itself with no luck so far.
I've created a fairly simple workflow based on this SO answer to retrieve a forgotten secret:
name: Show Me the S3cr3tz
on: [push]
jobs:
debug:
name: Debug
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout#v2
- name: Set up secret file
env:
MY_SECRET: ${{ secrets.MY_SECRET }}
run: |
echo $MY_SECRET >> secrets.txt
- name: Run tmate
uses: mxschmitt/action-tmate#v3
However, I can never see the tmate link because it is cancelled automatically and immediately after leaving the queue:
What am I doing wrong?
This is probably related to the recent GitHub Actions outage.
The workflow YAML syntax is valid, and confirmed to be working.