How to NOT run a GitHub Action when a specific label is set? - label

I have a GitHub Action workflow that runs to deploy a preview of a react-native expo app always when a Pull Request is opened. However, I do not want it to run when the dependabot opens a Pull Request.
How can I filter the dependabot Pull Requests? I saw there is a label dependencies attached, but I could not make the label to be filtered.
A few attempts I tried:
name: Preview
on:
pull_request:
types: [opened, synchronize]
jobs:
preview:
if: ${{ !contains(github.event.pull_request.labels.*.name, '0 diff dependencies') }}
name: Preview
on:
pull_request:
types: [opened, synchronize]
jobs:
preview:
if: "!contains(github.event.pull_request.labels.*.name, '0 diff dependencies')"
name: Preview
on:
pull_request:
types: [opened, synchronize]
jobs:
preview:
if: "!contains(github.event.pull_request.labels.*.name, '0 dependencies')"
name: Preview
on:
pull_request:
types: [opened, synchronize]
jobs:
preview:
if: github.event.label.name != 'dependencies'
name: Preview
on:
pull_request:
types: [opened, synchronize]
jobs:
preview:
if: ${{ github.event.label.name != 'dependencies' }}
If you want, you can find here the repository.

github.event.pull_request.labels is an array, so you index its first element. Assuming dependabot will only assign one label, this should be OK:
name: Preview
on:
pull_request:
types: [opened, synchronize]
jobs:
preview:
if: github.event.pull_request.labels[0].name != 'dependencies'
I just tested it on a dummy repo and it skipped my action when the PR label matched.

Related

Parametrise uses parameter in GitHub Actions

How I can pass parameter inputs.custom to this actions code:
jobs:
test-custom:
name: Test Custom
uses: ./.github/workflows/work4-${{ inputs.custom }}.yml
Fully working example:
First workflow work1-build.yml:
name: Start workflow fail
on: [push]
jobs:
build-fail:
name: Build with other workflow
uses: ./.github/workflows/work3-build-fail.yml
with:
custom: custom-name1
Second workflow work3-build-fail.yml:
name: Build fail with input test
on:
workflow_call:
inputs:
custom:
description: Some custom string
required: true
type: string
jobs:
test-custom:
name: Test Custom
uses: ./.github/workflows/work4-${{ inputs.custom }}.yml
Third workflow work4-custom-name1.yml
name: Custom 1
on:
workflow_call
jobs:
Explore-GitHub-Actions:
runs-on: ubuntu-latest
steps:
- run: echo "IN CUSTOM 1"
Example above make GitHub respond with an error:
Invalid workflow file
error parsing called workflow
".github/workflows/work1-build.yml"
-> "./.github/workflows/work3-build-fail.yml" (source branch with sha:720087c8794e76f52277f9b1229b44ea65ab89d5)
--> "./.github/workflows/work4-${{ inputs.custom }}.yml"
: failed to fetch workflow: workflow was not found.
I can successfully add ${{ inputs.custom }} to:
test-print:
runs-on: ubuntu-latest
name: Print input
steps:
- name: Step print input
run: echo ${{ inputs.custom }}
Docs doesn't contain any examples with uses parametrisation:
https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsuses
I found documentation that proves this isn't possible. There is no supporting info stating that the uses key has access to any contexts.
See: https://docs.github.com/en/actions/learn-github-actions/contexts#context-availability
I believe this is an architectural limitation of GitHub Actions, it appears they want to resolve all workflows/actions at the start of all jobs and thus dynamic resolution isn't possible.

GitHub Actions Matrix sharing the Same Code CheckOut

I tried to perform step actions/checkout#v3 once on chained jobs, but it seems like the "build" job does not get the code. I'm getting an error "can't find the project".
Can I call actions/checkout # v3 once for two jobs?
It works when I call the code checkout twice.
name: publish-nuget
on:
push:
branches:
- main
jobs:
prepare:
runs-on: ubuntu-latest
- name: Checkout code
uses: actions/checkout#v3
- name: Get package version
id: get_package_version
uses: kzrnm/get-net-sdk-project-versions-action#v1.3.0
with:
proj-path: ProjectOne.csproj
build:
needs: prepare
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout#v3
# Add the projects path below
strategy:
matrix:
projects: [
'ProjectOne.csproj',
'ProjectTwo.csproj',
]
steps:
- name: Pack NuGet
run: dotnet pack ${{ matrix.projects }} -p:PackageVersion=${{ env.PACKAGE_VERSION }} --configuration Release
It does not work when I call the code checkout once (on the 'prepare' job).
name: publish-nuget
on:
push:
branches:
- main
jobs:
prepare:
runs-on: ubuntu-latest
- name: Checkout code
uses: actions/checkout#v3
- name: Get package version
id: get_package_version
uses: kzrnm/get-net-sdk-project-versions-action#v1.3.0
with:
proj-path: ProjectOne.csproj
build:
needs: prepare
runs-on: ubuntu-latest
steps:
# Add the projects path below
strategy:
matrix:
projects: [
'ProjectOne.csproj',
'ProjectTwo.csproj',
]
steps:
- name: Pack NuGet
run: dotnet pack ${{ matrix.projects }} -p:PackageVersion=${{ env.PACKAGE_VERSION }} --configuration Release
Having a job being dependent on another job, is just for logical purposes and not state or artifact dependency sharing. You are actually runing the 2 jobs on 2 different agents. If you want to share something from the prepare job, you can use the cache or artifact API. E.g. using the cache API to cache the path 'somePath'. Same step for downloading the cache.
- name: Cached build artifacts
uses: actions/cache#v2
id: artifactcache
with:
path: somePath
key: buildArtifacts${{ github.run_number}}
As you are not gaining anything form splitting this up into 2 jobs, I would run everything in a single job instead.

Triggering yaml file when a certain label is applied to the pull request

I am working on a project where I need to create a tweet from an account whenever a certain issue or pull request has a label issue/tweet
I am able to make tweet when label is applied to issue but unable to do so when the same lable is applied to a pr
the .yml file I am working on
name: Send a Tweet
on:
issues:
-label: issue/tweet
pull_request:
types: [labeled]
jobs:
tweet:
if: ${{github.event.label.name == 'issue/tweet'}}
runs-on: ubuntu-latest
steps:
- uses: ethomson/send-tweet-action#v1
with:
status: ${{github.event.issue.html_url}} "#opensource"
consumer-key: ${{ secrets.TWITTER_CONSUMER_API_KEY }}
consumer-secret: ${{ secrets.TWITTER_CONSUMER_API_SECRET }}
access-token: ${{ secrets.TWITTER_ACCESS_TOKEN }}
access-token-secret: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }}
expected to make a tweet when pr is labeled with `issue/tweet'
Let's dissect the important parts...
Workflow sections
on events
You want issues and pull_request to trigger the workflow and I think both of them should have their types set to labeled.
if job condition
The condition needs to be applied to the correct object within github.event. For each of these events, the content is different. It's defined in Webhook events and payloads.
Both issue and pull_request should have github.event.label so I'm not sure why it wouldn't work. It might be a good idea to dump the entire context at the beginning of your job in order to debug it.
However, both events also have github.event.TYPE.labels, an array of label objects. Therefore, it might be a better option to use that and apply contains expression on it:
contains(github.event.TYPE.labels.*.name, 'issue/tweet')
Result
name: Send a Tweet
on:
issues:
types: [labeled]
pull_request:
types: [labeled]
jobs:
tweet:
if: >-
(
contains(github.event.pull_request.labels.*.name, 'issue/tweet') ||
contains(github.event.issue.labels.*.name, 'issue/tweet')
)
runs-on: ubuntu-latest
steps:
- uses: crazy-max/ghaction-dump-context#v1
- uses: ethomson/send-tweet-action#v1
with:
status: ${{github.event.issue.html_url}} "#opensource"
consumer-key: ${{ secrets.TWITTER_CONSUMER_API_KEY }}
consumer-secret: ${{ secrets.TWITTER_CONSUMER_API_SECRET }}
access-token: ${{ secrets.TWITTER_ACCESS_TOKEN }}
access-token-secret: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }}

Run command taking output from previous step on GitHub actions

I'm trying to make a GitHub action that builds a Hugo website, deploys it on Pinata and saves the output hash of this last step to a txt file. I managed to achieve the first and second steps. And, for the third one, I've been trying to do it by running an "echo" command. However, I get this message: "You have an error in your yaml syntax on line 36"
How do I run the script taking the output from the step identified as "ipfs-pin"?
Here's my code:
name: deploy
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#master
- uses: jakejarvis/hugo-build-action#master
with:
args: --minify --buildDrafts
- uses: anantaramdas/ipfs-pinata-deploy-action#v1.6.4
id: ipfs-pin
with:
pin-name: '[my-pin-name]'
path: './public'
pinata-api-key: [API Key]
pinata-secret-api-key: [secret API Key]
verbose: true
remove-old: true
saves-hash-on-file:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v2
with:
node-version: '14'
- run: echo ${{steps.build.ipfs-pin.hash}} > /.github/ipfs-hash.txt
First
It seems your indentation has a problem, I reproduced the workflow to correct it without returning error when pushing the workflow on the repository:
name: Deploy
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
outputs:
hash: ${{ steps.ipfs-pin.outputs.hash }}
steps:
- uses: actions/checkout#master
- uses: jakejarvis/hugo-build-action#master
with:
args: --minify --buildDrafts
- uses: anantaramdas/ipfs-pinata-deploy-action#v1.6.4
id: ipfs-pin
with:
pin-name: '[my-pin-name]'
path: './public'
pinata-api-key: '[API Key]'
pinata-secret-api-key: '[secret API Key]'
verbose: true
remove-old: true
saves-hash-on-file:
runs-on: ubuntu-latest
needs: [build]
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v2
with:
node-version: '14'
- run: echo ${{steps.build.outputs.hash}} > /.github/ipfs-hash.txt
Second
As you can see on the workflow above, I added the outputs field at the job1 (build) level, without this you can't share the output on other jobs.
Reference about outputs
Moreover, to share outputs between jobs, you will have to add the needs: [build] line at the job2 (saves-hash-on-file) level.
Note: I couldn't run it successfully as I don't have any credential to test, but it should work if you copy/paste the workflow I shared using your credentials.

Secret interpolation is giving syntax error in caller workflow when calling a resusable workflow in GitHub Action

I am using reusable workflow and when passing a secrets from caller workflow to reusable workflow, I am getting following syntax error:
The workflow is not valid. .github/workflows/caller_workflow.yml (Line: 28, Col: 28): Unrecognized named-value: 'secrets'. Located at position 1 within expression: secrets.SECRET_1 .github/workflows/caller_workflow.yml (Line: 29, Col: 22): Unrecognized named-value: 'secrets'. Located at position 1 within expression: secrets.SECRET_2
Not sure why the interpolation is not working.
This is my caller workflow caller_workflow.yml(giving above error):
name: Build workflow
on:
push:
branches:
- dev
- main
pull_request:
types:
- opened
- edited
- reopened
branches:
- main
- dev
jobs:
# reference: https://docs.github.com/en/actions/learn-github-actions/reusing-workflows#example-caller-workflow
org-checks:
uses: repo/.github/workflows/main_workflow.yml#main
with:
SECRET_1: ${{ secrets.SECRET_1 }}
SECRET_2: ${{ secrets.SECRET_2 }}
This is my reusable workflow:
name: CI workflow
on:
workflow_call: # enables this workflow to be reusable for other repo
secrets:
SECRET_1:
description: 'secret 1'
SECRET_2:
description: 'secret 2'
push:
branches:
- main
pull_request:
types:
- opened
- edited
- reopened
branches:
- main
jobs:
job-name-to-run:
...... ......
secrets in other flow are working all fine with the same syntax.
I was passing a secret in the wrong way. In my workflow, the secrets were passed using the with input parameter hence the error. with will work fine while passing the input to the called (reusable) workflow but not for secrets.
For passing the secrets use secrets parameter.
Here is updated caller_workflow.yaml :
name: Build workflow
on:
push:
branches:
- dev
- main
pull_request:
types:
- opened
- edited
- reopened
branches:
- main
- dev
jobs:
# reference: https://docs.github.com/en/actions/learn-github-actions/reusing-workflows#example-caller-workflow
org-checks:
uses: repo/.github/workflows/main_workflow.yml#main
secrets:
SECRET_1: ${{ secrets.SECRET_1 }}
SECRET_2: ${{ secrets.SECRET_2 }}
(removed with and added secrets)
Reference: Reusing workflows - example-caller-workflow

Resources