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
Related
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
I have created a workflow that automatically merges branch A into branch B when branch A is pushed and a workflow that runs when branch B is pushed. However, the workflow does not run when branch B is pushed. Is this a GitHub specification? If so, I would like to know if there is documentation or an issue that clearly states this.
name: CI
on:
push:
branches: [ "A" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Merge branch B
run: |
git fetch
git checkout B
git merge A
git push origin B
name: CI-2
on:
push:
branches: [ "B" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Test
run: echo Hello
According to the official documentation (triggering a workflow from a workflow)
, this issue occurs because:
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.
To make it work:
If you do want to trigger a workflow from within a workflow run, you can use a personal access token instead of GITHUB_TOKEN to trigger events that require a token. You'll need to create a personal access token and store it as a secret. To minimize your GitHub Actions usage costs, ensure that you don't create recursive or unintended workflow runs. For more information about creating a personal access token, see "Creating a personal access token." For more information about storing a personal access token as a secret, see "Creating and storing encrypted secrets."
If you're not familiar with the GITHUB_TOKEN concept and want to get more context about its usage, I suggest to check this section from the official documentation.
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.
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:
Can I trigger a new workflow from another workflow?
I'm trying to run a workflow after the first workflow has pushed a new release and it seems to ignore it.
Found the answer here:
An action in a workflow run can't trigger a new workflow run. For example, if an action 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.
EDIT:
The quote above might be confusing. When I add a Personal Access Token (PAT) to the checkout action with repo permissions granted (and not repository's GITHUB_TOKEN), the following commands DO trigger other workflows:
- name: Checkout Repo
uses: actions/checkout#v2
with:
token: ${{ secrets.PAT_TOKEN }}
(In my case, running semnatic-release after this checkout, which creates a new release with a new tag - did trigger another workflow that runs only if a tag was created)
As described here, you can trigger another workflow using the workflow_run event.
For example we could think of two workflow definitions like this (the only prerequisite is, that both reside in the same repository - but I'am sure, there's also an event for other repos as well):
release.yml
name: CI release
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Release artifact
run: ...
do-something-different.yml
name: Do anything after the release of the first workflow
on:
workflow_run:
workflows: ["CI release"]
types:
- completed
jobs:
notify:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Do something
run: ...
A crucial point here is that the name: CI release definition of the first yaml file must exactly match the workflow_run: workflows: ["CI release"] definition in the second yaml file. Another point is that this approach needs to be done on the default branch (which is mostly main or master) as the docs state:
Note: This event will only trigger a workflow run if the workflow file
is on the default branch.
If you don't want to use a general Personal Access Token (which has access to all of your repos), you can generate a dedicated SSH keypair for this purpose and add it to the repository as a Deploy Key. This is done as follows:
Generate an SSH keypair:
ssh-keygen -N "" -f deploy_key -C "github-actions"
Add the private key (generated file deploy_key) as an encryped secret, e.g. COMMIT_KEY to the GitHub project.
Add the public key (generated file deploy_key.pub) as a deploy key with write access to the GitHub project. Tick the Allow write access checkbox.
When checking out the source code in your workflow, add the SSH key:
- name: Checkout
uses: actions/checkout#v3
with:
ssh-key: "${{secrets.COMMIT_KEY}}"
Subsequent push actions in the same workflow will then trigger any configured GitHub workflow as if they were pushed manually.