Github/Actions comment on PR from repository_dispatch - github-actions

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'

Related

GitHub action workflow not triggering

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"]
})

how to run Github Actions Jobs in parallel using matrix?

I've really struggled here doing this for the first time and having no background in development.
We have an action that checks the status of several services running on different envs (DEV, TEST, PROD) and sends notifications to Microsoft Teams Channel.
At the moment there is a dedicated action for each env and the goal is to combine them in one.
the action itself:
name: Services Health Check
on:
workflow_dispatch:
schedule:
- cron: '*/30 * * * *'
env:
DEV: https://app.dev.contoso.com
TEST: https://app.test.contoso.com
PROD: https://app.contoso.com
TEAMS_TOKEN_DEV: ${{ secrets.HEALTH_CHECK_TEAMS_WEB_HOOK_URL_DEV }}
TEAMS_TOKEN_TEST: ${{ secrets.HEALTH_CHECK_TEAMS_WEB_HOOK_URL_TEST }}
TEAMS_TOKEN_PROD: ${{ secrets.HEALTH_CHECK_TEAMS_WEB_HOOK_URL_PROD }}
jobs:
#here I want to create a matrix as a JSON array to look like this, but Im not sure if I do it right (I am also not sure if I correctly escape the characters and which one should I escape):
#[
# { dev : https://app.dev.contoso.com, webhook : ${{ secrets.WEB_HOOK_URL_DEV }} },
# {test : https://app.test.contoso.com, webhook : ${{ secrets.WEB_HOOK_URL_TEST }} },
# {prod : https://app.contoso.com, webhook : ${{ secrets.WEB_HOOK_URL_TEST }} }
#]
env-matrix:
name: Setup ENV Matrix
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.matrix.outputs.env }}
steps:
- id: matrix-env
run: |
echo '::set-output name=env::[\{\"env\"\:\"$DEV\", \"webhook\"\:\"$TEAMS_TOKEN_DEV\"\}, \{\"env\"\:\"$DEMO\", \"webhook\"\:\"$TEAMS_TOKEN_DEMO\"\}, \{\"env\"\:\"$TEST\", \"webhook\"\:\"$TEAMS_TOKEN_TEST\"\}, \{\"env\"\:\"$POC\", \"webhook\"\:\"$TEAMS_TOKEN_POC\"\}, \{\"env\"\:\"$PRE\", \"webhook\"\:\"$TEAMS_TOKEN_PRE\"\}, \{\"env\"\:\"$PROD\", \"webhook\"\:\"$TEAMS_TOKEN_PROD\"\}]'
#and the healthcheck job itself
healthcheck:
needs: env-matrix
name: Health Check
runs-on: ubuntu-18.04
strategy:
matrix:
value: ${{ fromJson(needs.env-matrix.outputs.matrix-env)}}
steps:
- name: service1
uses: repo/action
continue-on-error: true
with:
url: '${{ matrix.value.env }}/service1/q/health/ready'
teamsWebHookURL: '${{ matrix.value.webhook }}'
- name: service2
uses: repo/action
continue-on-error: true
with:
url: '${{ matrix.value.env }}/service2/q/health/ready'
teamsWebHookURL: '${{ matrix.value.webhook }}'
so the job must run on DEV with TEAMS_TOKEN_DEV, on TEST with TEAMS_TOKEN_TEST, but I don't know the way to access an array item, so the steps are incorrect.
Any help will be appreciated. If you know a simpler solution pls share.
Thanks for your time and help
Another way of rewriting your workflow is to define the name of the secrets in the matrix and then using Array notation to fetch the actual value of the secrets. Below is a way of doing this and it is not a clone of your workflow. But this should give you an idea.
name: Services Health Check
on:
workflow_dispatch:
jobs:
healthcheck:
name: Health Check
runs-on: ubuntu-18.04
strategy:
matrix:
environment: [dev, test, prod]
include:
- environment: dev
url: https://app.dev.contoso.com
webhook: HEALTH_CHECK_TEAMS_WEB_HOOK_URL_DEV
- environment: test
url: https://app.test.contoso.com
webhook: HEALTH_CHECK_TEAMS_WEB_HOOK_URL_TEST
- environment: prod
url: https://app.prod.contoso.com
webhook: HEALTH_CHECK_TEAMS_WEB_HOOK_URL_PROD
steps:
- name: test_1
run: |
echo ${{ format('{0}{1}', matrix.url, '/service1/q/health/ready') }}
echo ${{secrets[matrix.webhook]}}
- name: test_2
run: |
echo ${{ format('{0}{1}', matrix.url, '/service2/q/health/ready') }}
echo ${{secrets[matrix.webhook]}}

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})"

github action fails with error could not read Username for 'https://github.com/'

Im using branch protection on master, but allowing bypass to a user with a PAT set to ${{ secrets.BYPASS }}
The below pipeline fails on the release step with this error.
I believe its the checkout#2 setting persist-credentials: false thats causing the error, but without it i cant get the Automated Version Bump step to work.
Is there a work around?
Run fregante/release-with-changelog#v3
Error: Command failed: git fetch origin +refs/tags/*:refs/tags/*
fatal: could not read Username for 'https://github.com/': No such device or address
Pipeline.yml
name: Push
on:
push:
branches: [ master ]
workflow_dispatch:
jobs:
push:
runs-on: ubuntu-latest
steps:
- name: Checkout source code
uses: 'actions/checkout#v2'
with:
ref: ${{ github.ref }}
persist-credentials: false
fetch-depth: 50
- name: Automated Version Bump
id: version-bump
uses: 'phips28/gh-action-bump-version#master'
env:
GITHUB_TOKEN: ${{ secrets.BYPASS }}
with:
skip-tag: 'false'
- name: Tag Changelog
uses: fregante/release-with-changelog#v3
with:
token: ${{ secrets.BYPASS }}

How can I use a Github action's output in a workflow?

Let's take this example composite action found on Github's documentation:
name: 'Hello World'
description: 'Greet someone'
inputs:
who-to-greet: # id of input
description: 'Who to greet'
required: true
default: 'World'
outputs:
random-number:
description: "Random number"
value: ${{ steps.random-number-generator.outputs.random-id }}
runs:
using: "composite"
steps:
- run: echo Hello ${{ inputs.who-to-greet }}.
shell: bash
- id: random-number-generator
run: echo "::set-output name=random-id::$(echo $RANDOM)"
shell: bash
- run: ${{ github.action_path }}/goodbye.sh
shell: bash
How can we use that specific output random-number in an external workflow that calls this action? I tried the following snippet but currently it seems the workflow cannot read the output variable from the action as it just comes out empty - 'Output - '
jobs:
test-job:
runs-on: self-hosted
steps:
- name: Call Hello World
id: hello-world
uses: actions/hello-world-action#v1
- name: Comment
if: ${{ github.event_name == 'pull_request' }}
uses: actions/github-script#v3
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Output - ${{ steps.hello-world.outputs.random-number.value }}'
})
It seems my attempt was correct with the exception of one detail:
Instead of:
${{ steps.hello-world.outputs.random-number.value }}
It should be referenced without the .value:
${{ steps.hello-world.outputs.random-number}}
Now it works.