I want github action to rebase my branch onto main as soon as a PR is created or updated from feature branch onto main branch.
But this rebase workflow ends up running twice.
Following is the code:
name: Rebase on main branch
on:
pull_request:
branches: [ main ]
jobs:
rebase:
name: Rebase on main branch
runs-on: ubuntu-latest
steps:
- name: git checkout
uses: actions/checkout#v2
with:
token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
fetch-depth: 0
- name: automatic rebase
uses: cirrus-actions/rebase#1.5
env:
GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
I have another test workflow that runs on successful rebase. Since rebase runs twice, test also runs twice which is not the intended behavior.
How do I make rebase run only once?
Or is there some trigger that can tell me if PR(create or update) + rebase happened?
Related
Currently, my GitHub workflow looks as follows:
name: learn-github-actions
run-name: ${{ github.actor }} is learning GitHub Actions
on:
push:
branches:
- main
jobs:
update-x:
runs-on: ubuntu-latest
steps:
- name: Git checkout
uses: actions/checkout#v2
with:
# fetch depth is needed since we will be taking git diff with HEAD^1
fetch-depth: 2
- name: Run script
run: scripts/x/x-version-bump.sh
Instead of using git diff in my x-version-bump.sh script I would like to get the changes of the commit pushed via a GitHub action and pass it on to the script. I cannot find a way currently in Github actions to do the same.
I have a GitHub action workflow for a swift package that runs whenever something is merged to the main branch.
My goal is to check the for a source branch and depending on its prefix (i.e. patch/something) determine wow to tag and release it.
This is how I have it configured so far:
name: Release
on:
push:
branches:
- main
jobs:
build:
runs-on: macos-latest
patch-release-on-push:
needs: build
if: contains(github.head_ref, 'patch')
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- id: release
uses: rymndhng/release-on-push-action#master
with:
bump_version_scheme: patch
The problem with this is that github.head_ref is null. I only get a value on it if I switch the workflow to be triggered on a pull_request instead of push which wouldn’t really work given that the release would be out before the PR was merged.
What’s the right approach for achieving this?
I would suggest you to use an existing action like EthanSK/git-branch-name-action, as example:
on: [push, pull_request]
jobs:
main_job:
runs-on: ubuntu-latest
steps:
- name: Git branch name
id: git-branch-name
uses: EthanSK/git-branch-name-action#v1
- name: Echo the branch name
run: echo "Branch name ${GIT_BRANCH_NAME}"
and use also like
if: ${{ contains(env.GIT_BRANCH_NAME, 'patch') }}
Link to the marketplace here
I have a CI workflow that runs on PR and PUSH to main branch.
---
name: CI
on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened]
I have another workflow I'd like to only run after CI is complete and conclusion is success but only when it's pushed to main branch.
---
name: Build
on:
workflow_run:
workflows: ["CI"]
types:
- completed
jobs:
build:
name: Build
runs-on: self-hosted
if: ${{ github.event.workflow_run.conclusion == 'success' }}
It runs on both PR and push to main. How do I get the Build workflow to only run on push to main?
It looks like you can just filter on the branch in the Build workflow (see https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#limiting-your-workflow-to-run-based-on-branches):
on:
workflow_run:
workflows: ["CI"]
types:
- completed
branches:
- main
I have some tests that I would like to run on every commit of my repository. I have the following script in my repo:
name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- run: echo "my tests"
Unfortunately, if I push some new commits to my repository, the tests are only run against the latest commit. Is there a way to test all commits?
It is possible to to this by checking out individual commits and building each one in a single run: step.
In order to do this, the fetch-depth option for the checkout action needs to be 0 to checkout the full git tree.
I did something like this using GitPython to iterate and checkout each commit.
Using just the git command line tool, the rev-list command could be used to create a list of commits.
The tricky part is figuring out the commit range. For pull requests, GitHub actions provides github.head_ref and github.base_ref properties (docs) that could be used to create the commit range. However, these properties are not available for other events, like push (in that case, github.ref could be used with a fixed branch name like origin/main).
Here is a simple example. It may need more a advanced query for rev-list to handle cases where base_ref is not an ancestor of head_ref, but I will leave that for other SO questions to answer.
name: CI
on: [pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
fetch-depth: 0
- run: |
for commit in $(git rev-list ${{ github.base_ref }}..${{ github.head_ref }}); do
git checkout $commit
echo "run test"
done
Building on David Lechner's answer:
name: CI
on:
push:
# only trigger on branches, not on tags
branches: '**'
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
# checkout full tree
fetch-depth: 0
- run: |
for commit in $(git rev-list ${{ github.event.before}}..${{ github.sha}}); do
git checkout $commit
echo "run test"
done
As per the docs on the github context and the docs on the push webhook event data {{github.event.before}} is replaced by the commit sha before the push. {{github.sha}} or {{github.event.after}} is replaced by the sha of the latest commit that was pushed:
Push event payload (for a pushed tag; docs)
{
"ref": "refs/tags/simple-tag",
"before": "6113728f27ae82c7b1a177c8d03f9e96e0adf246",
"after": "0000000000000000000000000000000000000000",
"created": false,
"deleted": true,
"forced": false,
"base_ref": null,
"compare": "https://github.com/Codertocat/Hello-World/compare/6113728f27ae...000000000000",
"commits": [],
"head_commit": null,
[...]
}
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?