GitHub action workflow not triggering - github-actions

I have created a new YAML file and that flow not triggering. Whereas I have other workflows that are working as expected.
I want to assign a label on the project card moved from to-do to in progress.
Not Working flow:
name: Update Label on Project Card Move
on:
project_card:
types: [moved]
jobs:
update_label:
if: ${{ github.event.project_card.column_id == '192285912' }}
runs-on: ubuntu-latest
permissions:
issues: write
steps:
- uses: actions/github-script#v6
with:
script: |
// this gets the number at the end of the content URL, which should be the issue/PR number
const issue_num = context.payload.project_card.content_url.split('/').pop()
github.rest.issues.addLabels({
issue_number: issue_num,
owner: context.repo.owner,
repo: context.repo.repo,
name: ["in progress"]
})

Related

How to separate conditions for different event types in GitHub Actions

We have a workflow file:
---
name: 'Deploy Test Env'
on:
pull_request:
types:
- edited
- opened
- synchronize
branches:
- develop
paths:
- '**.js'
jobs:
deploy:
# yamllint disable rule:line-length
name: '[DEV] DEPLOY'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Deploy
run: |
echo 'Deploy Dev Env by ${{ github.event.action }} event type' >> "${GITHUB_STEP_SUMMARY}"
When new Pull Request (feature_branch → develop) is created or new commit inside feature_branch is occured, pull_request's opened (or synchronize) event is triggering Job.
Here is a paths condition: if none of JavaScript files are changed, application source code is the same and we don't need to deploy new test environment. That is correct.
But, here is third action type: edited. It is used because we have environment parameters passed inside Pull Request message. And if message is changed (edited), it means that parameters possibly changed too, and we have to re-deploy test environment even if **.js files are not changed. But because of paths condition edited event will not be triggered too.
In other words, description should be looks like:
---
name: 'Deploy Test Env'
on:
# will be triggered only if *.js files changed
pull_request:
types:
- opened
- synchronize
branches:
- develop
paths:
- '**.js'
# will be triggered anytime when PR contents are updated
pull_request:
types:
- edited
branches:
- develop
But YAML doesn't support duplicated keys and this format is wrong.
OR:
on:
pull_request:
types:
# paths are set only for `opened` and `synchronize` types
- type: edited
- type: opened
paths:
- '**.js'
- type: synchronize
paths:
- '**.js'
branches:
- develop
But types should be a list...
The question is: Is it possible to describe desired behavior? Maybe pull_request may be passed twice as array or paths may be set under the edited type (something like my second example)
You can use reusable workflows to achieve this.
Divide your workflow into three (3) workflows:
ci.yml: reusable workflow (workflow that performs stuff)
ci-pr-opened-synchronize.yml: reusable workflow caller (for PR opened/synchronize for .js files)
ci-pr-edited.yml: reusable workflow caller (for PR edited)
The above reusable workflow callers will call the ci.yml workflow.
Here's a complete working example with .md files filter and PRs to the main branch (https://github.com/iamazeem/github-actions-reusable-workflow-test):
ci.yml
name: CI
on:
workflow_call:
inputs:
message:
type: string
description: custom message
required: true
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Print message
if: ${{ inputs.message }}
env:
MESSAGE: ${{ inputs.message }}
run: |
echo "message: $MESSAGE"
ci-pr-opened-synchronize.yml
name: PR opened/synchronize
on:
pull_request:
types:
- opened
- synchronize
branches:
- main
paths:
- '**.md'
jobs:
pr-open-sync:
uses: ./.github/workflows/ci.yml
with:
message: 'PR opened/synchronized'
ci-pr-edited.yml
name: PR edited
on:
pull_request:
types:
- edited
branches:
- main
jobs:
pr-edited:
uses: ./.github/workflows/ci.yml
with:
message: 'PR edited'
You may check this PR and its respective actions for this sample:
PR: https://github.com/iamazeem/github-actions-reusable-workflow-test/pull/1
Actions: https://github.com/iamazeem/github-actions-reusable-workflow-test/actions
Here is one more example of reusable workflows:
.github/workflows/reuser_on_edited.yml
the workflow will reuse to_reuse.yml jobs when PR contents are edited
---
name: 'Reuser on Edited'
on:
pull_request:
types:
- edited
branches:
- 'develop'
jobs:
reuse:
uses: ./.github/workflows/to_reuse.yml
with:
original_github: ${{ toJSON(github) }}
other_input: 'BOOM! edited'
.github/workflows/reuser_on_pr_changed.yml
the workflow will reuse to_reuse.yml jobs when some of **.js files is changed.
---
name: 'Reuser on PR changed'
on:
pull_request:
types:
- opened
- synchronize
branches:
- 'develop'
paths:
- '**.js'
jobs:
reuse:
uses: ./.github/workflows/to_reuse.yml
with:
original_github: ${{ toJSON(github) }}
.github/workflows/to_reuse.yml
the file to reuse jobs inside it
on:
workflow_call:
inputs:
original_github:
type: string
required: true
description: "github context that passed from original workflow (JSON)"
other_input:
type: string
default: 'default'
required: false
description: "just for LOLs"
jobs:
deploy_job:
runs-on: ubuntu-latest
steps:
- name: Checkout v2
uses: actions/checkout#v2
with:
ref: ${{ fromJSON(inputs.original_github).event.pull_request.head.sha }}
- name: Deploy
run: |
{
echo 'Deploy Dev Env by `${{ fromJSON(inputs.original_github).event.action }}` event type';
echo '';
echo 'Also `${{ inputs.other_input }}` input is passed';
} >> "${GITHUB_STEP_SUMMARY}"
Original github context may be passed as JSON string and reused inside different workflow.
Also, different conditions (paths, etc.) may be set for different pull_request action types.

How to use a json as a parameter for workflow_dispatch

I am trying to automate some tests but I would need to pass some particular parameters to the final test scripts that would fit perfectly as a json file. The issue now is to make github action able to handle json data as a parameter.
The constraint is that the json file as to be local as the workflow has to be triggered from the command gh workflow run ...
So far I tried create my first yml file as such :
name: setup
on:
workflow_dispatch :
inputs:
config_file:
description: 'json file containing the configuration for the runners'
required: true
type: string
...
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jobs:
setup-auth:
name: setup-authentication
uses: ./.github/workflows/single-device-authentication.yml
with:
devices: mlops
config_file: ${{ inputs.config_file }}
secrets: inherit
single-device-authentication.yml looks like this, I commented where it fails :
name: single-device-authentication
on:
workflow_call:
inputs:
devices:
required: true
type: string
config_file:
description: 'json file containing the configuration for jetson runners'
required: true
type: string
jobs:
device-authentication:
name: device-authentication
runs-on: ${{ inputs.devices }}
steps:
- uses: PATH/TO/gh_auth#main
with:
app_id: 7
private_key: ${{ secrets.MLOPS_BOT_PRIVATE_KEY }}
json-parser:
name: parser
runs-on: ${{inputs.devices}}
needs: device-authentication
steps:
- name: parser script
run: |
echo ${{ inputs.config_file }}" # This fails
Also, to trigger the workflow, I tried that way :
gh workflow run setup.yml -f config_file="$(cat ${PATH_TO_CONFIG_FILE})"

How do I specify a specific commit when manually running git action?

How do I create a workflow that can only be started manually, while it will need to specify a specific commit with which it will work?
You can manually run a workflow, provided it is configured to run on the workflow_dispatch event.
Add inputs to define your parameter
on:
workflow_dispatch:
inputs:
myCommit:
description: 'Commit SHA1'
required: true
default: 'undefined'
type: string
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Do something
run: your_command ${{ inputs.myCommit }}
...
Here's an example how to check out the specific commit for build:
on:
workflow_dispatch:
inputs:
refToBuild:
description: 'Branch, tag or commit SHA1 to build'
required: true
type: string
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
with:
ref: ${{ inputs.refToBuild }}
- name: Build
run: <command for build>

How to pre-fill an input field in a workflow_dispatch Github action with GITHUB-SHA

I'm trying to build a workflow_dispatch (ie manual) Github action that pre-fills the input fields with the SHA of the branch.
ie
name: Manually tag a release
on:
workflow_dispatch:
inputs:
git-sha:
description: Release SHA
default: <prefill with GITHUB_SHA>
required: true
I've tried default: ${{ github.sha }} but that throws an error.
Is this possible and what is the syntax?
I just solved this problem a couple of days ago, but in a different way.
I set the required to false and then coalesced the input with the GITHUB_SHA
name: Deploy To PROD
on:
workflow_dispatch:
inputs:
sha:
description: 'Git SHA to Deploy'
jobs:
deploy_prod:
runs-on: ubuntu-latest
env:
SHA_TO_DEPLOY: ${{ github.event.inputs.sha || github.sha }}
Then ${{ env.SHA_TO_DEPLOY }} references the SHA that was passed in, or the default if none was passed in.

Github/Actions comment on PR from repository_dispatch

Is there a way to find a PR (based on it's branch name) and post a comment to it; when triggered from a repository_dispatch?
I'm assuming from your question that you are sending the ref of the pull request to the repository_dispatch event. I don't know how you are doing that, but here is one way for reference.
- name: Repository Dispatch
uses: peter-evans/repository-dispatch#v1
with:
token: ${{ secrets.REPO_ACCESS_TOKEN }}
repository: username/my-repo
event-type: my-event
client-payload: '{"ref": "${{ github.ref }}"}'
That makes the ref available at github.event.client_payload.ref in the repository_dispatch event context.
on: repository_dispatch
jobs:
commentOnPr:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script#v2
id: get-pr-number
with:
script: |
const {data: pulls} = await github.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
head: '${{ github.event.client_payload.ref }}'
})
return pulls[0].number
result-encoding: string
- name: Create comment
uses: peter-evans/create-or-update-comment#v1
with:
issue-number: ${{ steps.get-pr-number.outputs.result }}
body: |
This is a multi-line test comment
- With GitHub **Markdown** :sparkles:
- Created by [create-or-update-comment][1]
[1]: https://github.com/peter-evans/create-or-update-comment
reactions: '+1'