Automatically create new PR from specific branch using GitHub actions? - github-actions

I have 3 branches:
develop
test
master
So what I want to do is to setup an automated process to be triggered on test branch getting updated, it will automatically create PR to master.
ie. I create a PR from develop to test then merge, once the merge is completed, I want to setup a GitHub action to automatically create a new PR from test to master.
Is it possible to do this/is there any marketplace action that meet my requirements? Thanks

You can achieve this with create-pull-request. Specifically, this example should help you.
In your case it will look something like this:
name: Create master promotion pull request
on:
push:
branches:
- test
jobs:
masterPromotion:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: master
- name: Reset master branch
run: |
git fetch origin test:test
git reset --hard test
- name: Create Pull Request
uses: peter-evans/create-pull-request#v3
with:
branch: master-promotion

Related

How can I attach a manual workflow run to a pull request's status checks?

I have a workflow which runs a test job under given circumstances automatically, or I can run it manually:
name: Test
on:
workflow_dispatch:
inputs:
used-branch:
description: Branch the test shall be run on
default: master
required: true
push:
branches: ['main', 'master']
tags: ['!**']
pull_request:
types: [opened, reopened, ready_for_review]
jobs:
test:
steps:
- name: Checkout selected branch
uses: actions/checkout#v3
if: inputs.used-branch != ''
with:
ref: ${{ github.event.inputs.used-branch }}
- name: Checkout current branch
uses: actions/checkout#v3
with:
ref: ${{github.ref}}
if: inputs.used-branch == ''
- name: Run test
...
I want the test be required before merging. So I check Require status check to pass before merging, Require branches to be up to date before merging and specify test as required job in the repo's branch settings.
The problem is: when I run the workflow manually (and therefore inject the branch via a variable), it's not related to the branch and its success won't be "discovered" by the PR checks.
Is there another way to link the run to a branch, or another way to propagate the result to the branch's pull request?
Took me a minute to digest what you were asking, but now I get it. (Updated title to be more precise).
Short answer, it isn't possible out of the box. Once you start to work around GitHub's API design things get complicated.
The only plausible solution I can think of today is via GitHub API.
See:
https://github.com/orgs/community/discussions/24616
https://docs.github.com/en/rest/checks?apiVersion=2022-11-28
https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/collaborating-on-repositories-with-code-quality-features/about-status-checks

Github workflows not triggered by automatically created PRs [duplicate]

This question already has an answer here:
GitHub Actions auto-approve not working on pull request created by GitHub Actions bot
(1 answer)
Closed 7 months ago.
I implemented a workflow that runs once a week and updates all the project dependencies and opens a PR with its changes using the workflow token.
name: Automatic dependency update
"on":
workflow_dispatch: null
schedule:
- cron: 0 0 * * 1
jobs:
update:
name: Update to latest versions
runs-on:
- self-hosted
- default-runner
steps:
- name: Checkout Project
uses: actions/checkout#v2
- name: Install Java
uses: actions/setup-java#v2
- name: Update Versions
run: |
./gradlew useLatestVersions --info
- name: Commit and open PR
uses: peter-evans/create-pull-request#v3
with:
commit-message: Update to latest versions
committer: Update Bot <workflow#xxx.com>
branch: auto-dependency-update
base: dev
delete-branch: true
title: Automatic dependency update
draft: false
team-reviewers: XX/teamname
body: Automated gradle dependency updates
The issue is, that for this PR the normal workflows (that are mandatory for the PR merge are not triggered.
name: Build pipeline
"on":
workflow_dispatch: null
pull_request:
branches:
- dev
push:
branches:
- '!master'
- '**'
defaults:
run:
shell: bash
jobs:
build:
name: Compile
runs-on:
- self-hosted
- default-runner
steps:
- name: Checkout code
uses: actions/checkout#v2
- uses: actions/setup-java#v2
- name: Compile code
run: |
./gradlew classes testClasses --info
# ...
When I manually push something to that branch, the workflows are triggered. Though when I add the following step to the version update workflow, then the workflows aren't triggered either.
So what can I do? I dont want to trigger the workflows explicityl (e.g. using benc-uk/workflow-dispatch#v1) to keep the update mechanism as generic as possible.
According to the official documentation
When you use the repository's GITHUB_TOKEN to perform tasks, 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.
For more information, see "Authenticating with the GITHUB_TOKEN."
The action you're using to open the PR also states in the Inputs section that you can change the GITHUB_TOKEN scope as well, or use a PAT:
GITHUB_TOKEN (permissions contents: write and pull-requests: write) or a repo scoped Personal Access Token (PAT).
Solution
Therefore, you just need to add a token input to the peter-evans/create-pull-request action using a secret allowing you to trigger a workflow from another workflow.

How to get branch name which triggered a github action workflow

Edit: Question makes no sense, the branch which triggered the workflow always gets checked out per default.
Is there a way to get the name of the branch which started the workflow in Github Actions? For example, if I push to test/1, I'd like to use this name in my workflow.
Preferably before checking out the repo since I want to check out the branch which triggered the workflow. And I can't just specify the branch myself since I have multiple branches named test/* and I want my workflow to handle things dynamically in one workflow without creating a seperate workflow for each branch name.
Example:
name: Workflow
on:
push:
branches: [ test/* ]
jobs:
build:
runs-on: [ self-hosted ]
steps:
- name: Checkout the code
uses: actions/checkout#v2
with:
ref: $branch_name <--- this is where I want to use the branch name for the branch which triggered the workflow on push
fetch-depth: 0

Why will this not trigger on a pull request push commit update?

name: blabla bacon n eggs
on:
pull_request:
branches:
- basickarl/gh-actions-pr
defaults:
run:
shell: bash
jobs:
somting:
runs-on: ubuntu-latest
steps:
- run: echo "testy"
I have pushed my code on the branch stated to the origin repo in github. I have created a pull request. I updated some code on the branch and pushed, but the actions is not triggering? How does one trigger a github action workflow when updating a pull request?
The updates to the branch I am making is the workflow file itself.
Here is an example repo: https://github.com/basickarl/github-actions/actions/workflows/test.yaml
For some reason only on: [pull_request] seems to work.
Use types: [synchronize] under on: pull-request:

Trigger GitHub Action Workflow By Pull Request

I'd like to trigger automatically a GitHub Action Workflow for each [assigned, opened, synchronize, reopened] Git Pull Request from side-branch into master.
In addition, Is there a way to checkout the code from the private repository of a PR via branch name?
Below you can find my GitHub Action workflow that I've tried but unfortunately when I open a PR from side-branch into master it didn't trigger at all.
on:
workflow_dispatch:
pull_request:
types: [assigned, opened, synchronize, reopened]
branches:
- master
jobs:
build-image:
name: Build Docker Image
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v2
with:
ref: ${{github.event.pull_request.head.sha}}
Is it possible, how can I do that?