How to add labels to merged PR automatically using github Actions? - github

Need to setup github action for a repository which automatically adds "hacktoberfest-accepted" label once the PR is merged.
Tried with just pull_request_target but the action did not got invoked so added pull_request too but still even on Merging the action is not getting invoked.
Confused about what is causing this issue or is there any other method to achieve the same thing?
Workflow code
on:
pull_request_target:
types:
- closed
pull_request:
types:
- closed
jobs:
label:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
# https://github.com/marketplace/actions/pr-labeler-based-on-multiple-rules
- uses: srvaroa/labeler#v0.8
with:
config_path: .github/labeler_config.yml
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
Config file
version: 1
labels:
- label: "hacktoberfest-accepted"
size-above: 1
Took help from here.
P.S. : Rest of the workflow files are working as expected.

Related

Run Github action when pull request origin branch has certain prefix

I am trying to create actions which behave differently depending on the prefix of the origin (head?) branch.
Current use case is sending a Slack message when a branch named bug/<anything> is getting merged into main.
I've tried setting a script up like the following.
report-bugfix.yml
name: Report Bugfix
on:
push:
branches:
- main
jobs:
run_if:
if: startsWith(github.ref_name, 'bug/')
runs-on: self-hosted
steps:
- uses: 8BitJonny/gh-get-current-pr#2.1.0
id: PR
- name: Slack Notify
uses: rtCamp/action-slack-notify#v2.2.0
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_CHANGELOG }}
SLACK_USERNAME: BugBuster Bot
SLACK_COLOR: ${{ job.status }}
SLACK_TITLE: 'A new bug has been fixed! :beetle:'
SLACK_MESSAGE: ${{ steps.PR.outputs.pr_body }}
My action currently gets skipped as the if statement returns false, so I assume my syntax or variable checking is wrong.
Any action sharks who are aware of a elegant solution to my use case?
Edit: Changed following line if: startsWith(github.ref_name, 'bug/**') with if: startsWith(github.ref_name, 'bug/')
However testing with the branchname bug/action-testing the issue still persists
The double asterisk after the bug/ is unnecessary. Unless you only want names like bug/**abcd to pass, the ** will get treated as characters. With the startsWith function, you only need to pass the actual characters to match, and don't need to worry about wildcards.
More info on Github actions expressions: https://docs.github.com/en/actions/learn-github-actions/expressions
So the issue was related to looking for the push event. The event doesn't seem to know about where a push came from, so it was essentially impossible to locate that information for me.
Instead I can use the pull_request event, which sets the github.head_ref variable to the branch which I am creating the request from.
To ensure the action only triggered when the pull request was closed and merged, I used the type closed and github.event.pull_request.merged == true in an if statement.
My final script is as follows:
name: Bug Changelog
on:
pull_request:
types:
- closed
branches:
- 'main'
jobs:
if_merged:
if: github.event.pull_request.merged == true && startsWith(github.head_ref, 'bug/')
runs-on: self-hosted
steps:
- uses: 8BitJonny/gh-get-current-pr#2.1.0
id: PR
- name: Slack Notify
uses: rtCamp/action-slack-notify#v2.2.0
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_CHANGELOG }}
SLACK_USERNAME: BugBuster Bot
SLACK_COLOR: ${{ job.status }}
SLACK_TITLE: 'A new bug has been fixed! :beetle:'
SLACK_MESSAGE: ${{ steps.PR.outputs.pr_body }}
SLACK_FOOTER: ''
MSG_MINIMAL: true

Github Workflow that forces developers to check box on PR before merge (passing status checks based on issue_comment events)

I want to create a workflow that makes a comment with a checklist on a newly opened PR reminding developers to go and update documentation and fail status checks until the developer acknowledges the checklist. Here is my workflow:
name: 'PR Tasks Completed Check'
on:
pull_request:
types: [opened, edited, synchronize]
issues:
types: [opened, edited, deleted]
issue_comment:
types: [created, edited, deleted]
jobs:
review-checklist:
name: Make documentation comment on PR
runs-on: ubuntu-latest
steps:
- name: "Make comment (if necessary)"
uses: actions/github-script#v4
with:
script: |
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `### Review Checklist
- [ ] Documentation updated`,
})
require_checklist_c:
name: "Require Checklist"
runs-on: ubuntu-latest
needs: [review-checklist]
steps:
- uses: mheap/require-checklist-action#v1
with:
requireChecklist: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
The problem I'm having is that even though require-checklist-action runs fine and fails the status check appropriately after the PR is opened and the documentation comment is made, when it runs as the result of issue_comment being run after the person checks the box, I can see the workflow running again on the Actions tab but the status check on the PR itself remains in the failed state. Am I not able to pass status checks based on issue_comment events? Or is there some other problem i'm fundamentally misunderstanding?

Automating a PR merge followed by opening a new PR

I have a branch called stage and currently have a workflow that runs when a PR is labeled (to merge into the stage branch), and I'm currently trying to create a workflow that happens if the branch is successfully automerge.
For example -- here's a working example of the workflow to the stage branch:
name: PR opened to stage
on:
pull_request:
types: [labeled]
branches: [ stage ]
jobs:
audit-checks:
runs-on: self-hosted
steps:
- uses: actions/checkout#v2
if: contains(github.event.pull_request.labels.*.name, 'ready')
- name: Enable Auto Merge
uses: peter-evans/enable-pull-request-automerge#v1
if: contains(github.event.pull_request.labels.*.name, 'ready')
with:
token: ${{ secrets.GITHUB_TOKEN }}
pull-request-number: ${{ github.event.pull_request.number }}
merge-method: merge
And here's another workflow that, when a PR to stage is closed, is supposed to run. I have a step as well that checks to see if the label includes ready and is supposed to create another PR to another branch:
name: ready for prod test
on:
pull_request:
types: [closed]
branches: [ stage ]
jobs:
audit-checks:
runs-on: self-hosted
steps:
- uses: actions/checkout#v2
- name: automate PR creation for prod
if: |
${{ github.event.pull_request.merged }} && contains(github.event.pull_request.labels.*.name, 'ready')
uses: peter-evans/create-pull-request#v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
branch: stage
base: prod
labels: ready
title: Automated PR opened
It seems that when the PR is initially opened and labeled ready, the first workflow runs just fine. However, once that workflow enables auto merge and it actually auto-merges (upon completion of the workflow), the second workflow doesn't ever get triggered.
Is it possible that the second workflow is somehow running before the first one completes and doesn't actually do anything because of it? I'm not sure it's anything in my 2nd job (at least not yet) since it hasn't even run (or run and fail).
Just found my answer here: https://docs.github.com/en/actions/security-guides/automatic-token-authentication
When you use the repository's GITHUB_TOKEN to perform tasks on behalf
of the GitHub Actions app, events triggered by the GITHUB_TOKEN will
not create a new workflow run. This prevents you from accidentally
creating recursive workflow runs. For example, if a workflow run
pushes code using the repository's GITHUB_TOKEN, a new workflow will
not run even when the repository contains a workflow configured to run
when push events occur.

GitHub Actions on release created workflow trigger not working

I have a GitHub Actions workflow implemented on the main branch of my repository which creates a new release of my package in GitHub. Then I have another workflow implemented which should be triggered on the creation of a release. This trigger, however, is not working.
Please note that GitHub abandoned their own actions/create-release#v1 project and advises to use the softprops release action.
My workflow template is as follows:
name: Main release
on:
push:
branches:
- main
jobs:
release:
name: 'Release main'
runs-on: ubuntu-latest
steps:
- name: 'Checkout source code'
uses: 'actions/checkout#v2'
with:
ref: ${{ github.ref }
- name: Release
uses: softprops/action-gh-release#v1
with:
draft: false
body_path: CHANGELOG.md
name: ${{ steps.version.outputs.version }}
tag_name: ${{ github.ref }}
token: ${{ github.token }}
My on:release:created trigger workflow is as follows:
name: Act on release created
on:
release:
types: [created]
jobs:
build:
name: Build
environment: dev_environment
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Set env
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
- name: Test
run: |
echo $RELEASE_VERSION
echo ${{ env.RELEASE_VERSION }}
The release and tags are correctly added in GitHub, so everything looks to work fine except that the workflow that should be triggered on the release is not executed.
How do I solve this?
The GitHub Actions documentation on performing tasks in a workflow states the following:
When you use the repository's GITHUB_TOKEN to perform tasks on behalf of the GitHub Actions app, events triggered by the GITHUB_TOKEN will not create a new workflow run. This prevents you from accidentally creating recursive workflow runs.
This means that you will have to create a personal access token and add this token to you repository secrets.
To generate a new personal access token go to your personal developer settings and generate a new token. Then go to your repository settings and add a new secret containing the personal access token, name it i.e. PAT.
In your release workflow template, replace:
token: ${{ github.token }}
With:
token: ${{ secrets.PAT }}
Now the on release created event the workflow will be triggered!
Note: This approach seems is a bit hacky, but is currently the only known workaround for this issue and can be considered a major design flaw of workflow integrations.
As an addendum to the answer given above, I found the workflow_run event trigger to work well for this use case:
on:
workflow_run:
workflows: ["Main release"]
types: [completed]
You can add conditions for various release tags and all if required apart from this.

Create pull request with github action

I'm trying to make it work this action, but I'm confused also whats it's missing in between, before triggering the peter-evans PR.
The scenario is pretty simple, I like on push, on any feature/* branch, to create automatically PR, but instead I'm getting weird scenario, where develop changes are applied on top of the feature/* branch. Can someone give me hints on this?
name: Pull Request Action
on:
push:
branches:
- feature/*
jobs:
create-pull-request:
runs-on: ubuntu-latest
steps:
- name: Check out repository code
uses: actions/checkout#v2
with:
fetch-depth: 0
ref: develop
- name: Create Pull Request
uses: peter-evans/create-pull-request#v3.10.0
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: Simple demo
title: '[Example] Simple demo'
body: >
This PR is auto-generated by
[create-pull-request](https://github.com/peter-evans/create-pull-request).
labels: feature, automated pr
branch: feature/workflow-demo
Just posting this as an alternative solution. If you don't want to use any 3rd party actions you can achieve this with actions/github-script, it will just require a bit more coding.
As this stands, the action will error if there is already an open PR for the feature branch. If this is an issue you could check of an existing PR with the github.rest.pulls.list method, filtering by both head and base so it will only return one or no PRs.
name: Pull Request Action
on:
push:
branches:
- feature/*
jobs:
create-pull-request:
runs-on: ubuntu-latest
steps:
- name: Create Pull Request
uses: actions/github-script#v6
with:
script: |
const { repo, owner } = context.repo;
const result = await github.rest.pulls.create({
title: '[Example] Simple demo',
owner,
repo,
head: '${{ github.ref_name }}',
base: 'develop',
body: [
'This PR is auto-generated by',
'[actions/github-script](https://github.com/actions/github-script).'
].join('\n')
});
github.rest.issues.addLabels({
owner,
repo,
issue_number: result.data.number,
labels: ['feature', 'automated pr']
});
I know this question is a year old now and asking about the create-pull-request action, but for those that would rather not use third-party actions, Github actions now support Github command line natively, if you use Github hosted runners. See: Using Github CLI in Workflows
This makes it super easy to create a pull request using the gh pr create command
Something like this:
steps:
- name: create pull request
run: gh pr create -B base_branch -H branch_to_merge --title 'Merge branch_to_merge into base_branch' --body 'Created by Github action'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Reading through the readme, the action by Peter Evans doesn't fit what you're trying to achieve. But you can use repo-sync's pull-request action:
name: Pull Request Action
on:
push:
branches:
- feature/*
jobs:
create-pull-request:
runs-on: ubuntu-latest
steps:
- name: Check out repository code
uses: actions/checkout#v2
- name: pull-request
uses: repo-sync/pull-request#v2
with:
destination_branch: "develop"
github_token: ${{ secrets.GITHUB_TOKEN }}
pr_label: "feature, automated pr"
pr_title: "[Example] Simple demo"
You might need to specify the base branch there:
- name: Create Pull Request
uses: peter-evans/create-pull-request#v3.10.1
with:
token: ${{ secrets.GH_TOKEN }}
commit-message: Auto Pull Request
title: Your desired title
body: Auto-created Pull Request
branch: ${{ github.ref }} # The branch where you commit
base: develop # Don't forget to specify the right base branch here
I have this and it creates the PR when it does not exist. I remember it didn't work exactly right at the beginning until I specified myself base and branch values, which are not very clear in the docs.