How to associate status of a Github action workflow with a commit or PR - github

I have a Github action workflow which is set to trigger on issue_comment, for example the following
name: Testing actions
on:
issue_comment:
types: [created, edited]
jobs:
testing-actions:
if: github.event.issue.pull_request && contains(github.event.comment.body, '#test')
name: Just testing
runs-on: ubuntu-latest
steps:
- uses: xt0rted/pull-request-comment-branch#v1
id: comment-branch
- uses: actions/checkout#v3
with:
ref: ${{ steps.comment-branch.outputs.head_ref }}
- name: Print stuff
run: echo "Hello dudes!"
The workflow is working, it is getting triggered when I comment "#test" on a PR. But I have to check it from the actions tab, the workflow doesn't get associated with any commit or PR, so it doesn't get shown in the checks tab in the PR, or the commits don't show any commit status.
How do I get the workflow status and details in the PR page, so that I don't have to go to actions tab manually to see the results?

I solved this by manually setting the commit status using #myrotvorets/set-commit-status-action. The workflow file ended up being like the following
name: Testing actions
on:
issue_comment:
types: [created, edited]
jobs:
testing-actions:
if: github.event.issue.pull_request && contains(github.event.comment.body, '#test')
name: Just testing
runs-on: ubuntu-latest
steps:
- name: Get PR details
uses: xt0rted/pull-request-comment-branch#v1
id: comment-branch
- name: Set commit status as pending
uses: myrotvorets/set-commit-status-action#master
with:
sha: ${{ steps.comment-branch.outputs.head_sha }}
token: ${{ secrets.GITHUB_TOKEN }}
status: pending
- uses: actions/checkout#v3
with:
ref: ${{ steps.comment-branch.outputs.head_ref }}
- name: Print stuff
run: echo "Hello dudes!"
- name: Set final commit status
uses: myrotvorets/set-commit-status-action#master
if: always()
with:
sha: ${{ steps.comment-branch.outputs.head_sha }}
token: ${{ secrets.GITHUB_TOKEN }}
status: ${{ job.status }}
This works pretty well, the workflow runs get associated with the PR's head commit, and thus it shows up on the PR page as a check too. Thanks #rethab for the alpha.

Related

git steps command to get output from action

I would like to get the success/failure output of this action aws-actions/configure-aws-credentials#v1 in my next job. As per the action-output the output is aws-account-id.
hence I created below workflow, doesn't matter what I do, I don't seems to catch the output. My question is how to catch the error/success message for this action.
Right now I'm getting empty even if the job success/failed. Is there any way I can get the output of the steps?
jobs:
deploy_infra:
name: Deploy
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
outputs:
output1: ${{ steps.cac.outputs.aws-account-id }}
steps:
- name: Git Checkout
uses: actions/checkout#v3
- name: AWS Role
id: cac
uses: aws-actions/configure-aws-credentials#v1
with:
role-to-assume: arn:aws:iam::${{ github.event.inputs.account_id }}:role/assume-role
aws-region: ${{ github.event.inputs.aws_region }}
mask-aws-account-id: false
alert_job:
if: always()
needs: deploy_infra
name: Testing the status
runs-on: ubuntu-latest
steps:
- run: echo ${{needs.job1.outputs.output1}}

Github Actions workflow not triggered although schedule should trigger the job

I'm trying to auto-assign issues and PRs in Github from a Github Actions workflow. The respective steps work fine when an issue / a PR is opened. So this trigger is fine.
Recently I added Dependabot to my repo. Since Dependabot cannot access my secrets I cannot assign any issue in a Dependabot-triggeres pipeline. So I just thought I run this pipeline with a scheduler one per day to "clean up" every issue and PR which is unassigned. But this schedule config does not trigger the pipeline. It simply does nothing, not even show as a pipeline run which does nothing (with all jobs skipped). Seems like the trigger is completely ignored.
This is my workflow file.
---
name: "Organize: Assign Issues + Pull Requests"
on:
issues:
types:
- opened
pull_request:
types:
- opened
schedule:
- cron: '0 9 * * *' # https://crontab.guru/#0_11_*_*_*
permissions:
contents: read
issues: write
pull-requests: write
jobs:
add-to-project:
name: Add to project
runs-on: ubuntu-latest
steps:
- name: Add to project (issues and PRs)
uses: actions/add-to-project#main
with:
project-url: https://github.com/users/sebastian-sommerfeld-io/projects/1
github-token: ${{ secrets.GH_TOKEN_REPO_AND_PROJECT }}
assign-to-user:
name: Assign to user
runs-on: ubuntu-latest
steps:
- name: Assign issue to user when moved into column
uses: pozil/auto-assign-issue#v1
# https://github.com/marketplace/actions/auto-assign-issue
with:
assignees: ${{ github.actor }}
numOfAssignee: 1
allowSelfAssign: true
abortIfPreviousAssignees: true
new-pull-request-chat-message:
runs-on: ubuntu-latest
needs: ['add-to-project', 'assign-to-user']
if: github.event_name == 'pull_request'
steps:
- name: Send message to Google Chat
uses: Co-qn/google-chat-notification#releases/v1
with:
name: New Pull Request "${{ github.event.pull_request.title }}" (raised by ${{ github.actor }})
url: ${{ secrets.GOOGLE_CHAT_WEBHOOK }}
status: ${{ job.status }}
on-failure:
runs-on: ubuntu-latest
needs: ['add-to-project', 'assign-to-user', 'new-pull-request-chat-message']
if: failure()
steps:
- name: Send Pipeline Status to Google Chat
if: always()
uses: Co-qn/google-chat-notification#releases/v1
with:
name: ${{ github.workflow }}
url: ${{ secrets.GOOGLE_CHAT_WEBHOOK }}
status: failure
What bugs me is that the scheduler setting is copied from another workflow where it works just fine. So I cannot think of a reason why this pipeline is not triggered at 09:00 in the morning.
Found a way :-)
I use the Github CLI to get all PRs with a certain label and assign a user. This is a new dedicated pipeline.
---
name: "Organize: Dependabot Pull Requests"
on:
schedule:
- cron: '30 * * * *' # https://crontab.guru
permissions:
contents: read
issues: write
pull-requests: write
jobs:
assign-user:
name: Aassign PRs with label 'dependencies'
runs-on: ubuntu-latest
steps:
- name: Get PR and assign user (filtered by github cli)
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN_REPO_AND_PROJECT }}
label: dependencies
assignee: sebastian-sommerfeld-io
run: |
OUTPUT=""
pr_ids="$(gh pr list --repo "$GITHUB_REPOSITORY" --label "$label" --json number --jq '.[].number')"
for id in $pr_ids; do
gh pr edit "$id" --repo "$GITHUB_REPOSITORY" --add-assignee "$assignee"
done
Then I updated the triggers of my original pipeline to
on:
issues:
types:
- opened
pull_request:
types:
- opened
- assigned

GitHub Actions re-run and who is the author

I need an additional field with information on who is the author of the trigger re-run button in GitHub actions.Any help + bless you 🙏
Below is an example.
name: CI
#Only for master
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
check_run:
types: [rerequested]
#Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
notify:
runs-on: ubuntu-latest
steps:
# Ms Teams Deploy Cards step linked to repository in a dedicated created Ms teams channel
- name: teams notification
uses: patrickpaulin/ms-teams-deploy-card#master
if: always()
with:
github-token: ${{ github.token }}
webhook-uri: ${{ secrets.MS_TEAMS_WEBHOOK_URI }}
and this part of the code
check_run:
types: [requested]
This step is to identify re-run action but in this example just duplicate MS Deployment Card and provide all information with was when the author did push or pull request.
P.S
Friendly advice: Don't use toko-bifrost repo because do not work
correct, explaining in the link below.
https://github.com/toko-bifrost/ms-teams-deploy-card/issues/44
SOLVED
- name: teams notification
uses: patrickpaulin/ms-teams-deploy-card#master
if: always()
with:
github-token: ${{ github.token }}
webhook-uri: ${{ secrets.MS_TEAMS_WEBHOOK_URI }}
custom-facts: |
- name: GiitHub Action
value: ${{ github.actor }}

how to set GitHub action to comment on new PR?

I want to integrate github-action-comment-pull-request from the official GitHub marketplace.
This will be my yaml:
on: [pull_request]
jobs:
build:
name: Comment a pull_request
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Comment a pull_request
uses: mb2dev/github-action-comment-pull-request#1.0.0
with:
message: "Hello, Thank you for this PR!"
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
My problem is that I want the message to be shown for all new PRs except PRs raised by specific users: userA, userB.
Preferably also to exclude users for a specific GitHub team.
Is this possible?
If you want this workflow to run the job for everyone except for specific users, an option could be to add an if condition to your job to run only if the github.actor from the Github context isn't among a list of users you set.
Example with your workflow:
on: [pull_request]
jobs:
build:
name: Comment a pull_request
runs-on: ubuntu-latest
if: ${{ github.actor != 'Slava' }}
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Comment a pull_request
uses: mb2dev/github-action-comment-pull-request#1.0.0
with:
message: "Hello, Thank you for this PR!"
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
This also work for many users if you add && or || inside the condition.
For example, I use this workflow with some of my repositories to add PR comments:
name: PR Comment
on:
pull_request_target:
types:
- opened
jobs:
PR-Comment:
if: github.actor != 'user1' && github.actor != 'user2' && github.actor != 'user3' && github.actor != 'user4' && github.actor != 'user5'
runs-on: ubuntu-latest
steps:
- name: PR Comment
uses: actions/github-script#v2
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.issues.createComment({
issue_number: ${{ github.event.number }},
owner: context.repo.owner,
repo: context.repo.repo,
body: ':warning: Have you followed the [contributions guidance](<contribution_guidance_url>)? Content PRs should generally be made against the the [source repo](https://github.com/<owner>/<repo>).'
})
A full workflow example can be found in this repo
I don't think it's possible to use or inform the Github Team directly with a condition yet.

GitHub action for issue_comment doesn't shown in checks for PR

I created a GitHub action on:issue_comment, I can see the flow running only in the action tab, but not in the PR, where I made the comment.
I want to comment in a PR and trigger a check on that PR (not on master)
here is my workflow:
name: issue-comment-CI-test
on:
issue_comment:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- name: Run a one-line script
run: echo Hello, world!
- name: Run a multi-line script
run: echo ${{ github.event.comment.body }}
currently, I'm just printing the comment body, But I plan to check the body, and if it is equal to "run integration tests" then I'll run my integration tests (maven)
Basically you need to checkout to the PR origin. For that, first make a API request to the pr url and fetch all ref.
Then do the checkout on the fetched repo and branch.
Step1
- name: Github API Request
id: request
uses: octokit/request-action#v2.0.0
with:
route: ${{ github.event.issue.pull_request.url }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Step 2
- name: Checkout PR Branch
uses: actions/checkout#v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
repository: ${{ fromJson(steps.request.outputs.data).head.repo.full_name }}
ref: ${{ steps.pr_data.outputs.branch }}
You can follow the following example, specially the GitHub API Request part.
I've also implemented it in one of our workflows, you can take reference from that as well.
https://github.com/adrianjost/workflow-trigger-comment-example/blob/master/.github/workflows/demo.yml
https://github.com/TeamAmaze/AmazeFileManager/blob/master/.github/workflows/android-debug-artifact-ondemand.yml
You need to checkout the Pull Request. You can get the PR ID by using {{ GITHUB_REF }}.
You can checkout the PR with:
git fetch origin pull/{{ GITHUB_REF }}/head:PR
git checkout PR
See https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/checking-out-pull-requests-locally, https://help.github.com/en/actions/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#onevent_nametypes, https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables
and https://developer.github.com/v3/pulls/ for reference.
Used built-in gh command to fetch the branch instead of using actions/checkout.
name: issue-comment-CI-test
on:
issue_comment:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- run: |
gh pr checkout ${{ github.event.issue.number }}
# do your job...
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} # required for gh