So, I have a GitHub workflow that runs on push and checks for changes in a specific directory and runs a script if there are changes. However, with the current implementation (git diff HEAD^ HEAD), it only checks the diff between the last 2 commits. It is entirely possible that more than 1 commit was pushed so I am looking for a way through which I can compare the last 2 pushed commits.
Would be awesome if anyone can help me with this!
You can use the following:
on:
push:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
# checkout full tree
fetch-depth: 0
- run: |
git diff ${{github.event.before}} ${{github.sha}}
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.
Related
I have GitHub repository with 2 branches: "master" & "develop".
The workflow for us is that any code should be committed to the "develop" branch then pushed to GitHub, then a Pull Request should be created to merge the commits into the "master" branch.
I am trying to write an Action that will create a Pull Request once a developer pushes commits to the branch "develop" and had the following script:
name: Create pull request
on:
push:
branches:
- develop
jobs:
prForMasterBranch:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: master
- name: Create Pull Request
uses: peter-evans/create-pull-request#v2
with:
commit-message: update master branch
title: Update master branch
branch: develop
I can see that this action has executed successfully on "Push" event of the "develop" branch, but I can't see any new Pull Requests!
I checked the logs for the action and found these lines at the end of pull request creation:
Pushing pull request branch to 'origin/develop'
Branch 'develop' no longer differs from base branch 'master'
Closing pull request and deleting branch 'develop'
It seems I am missing something, but couldn't figure it out.
Any help is appreciated.
If you look at the documentation of the create-pull-request action, it mentions that
Create Pull Request action will:
Check for repository changes in the Actions workspace. This includes:
untracked (new) files
- tracked (modified) files
- commits made during the workflow that have not been pushed
Commit all changes to a new branch,
or update an existing pull request branch.
Create a pull request to
merge the new branch into the base—the branch checked out in the
workflow.
It would always need an intermediary branch where it can commit the changes.
So if you modify your workflow config as below, adding the Reset master branch step to get the latest changes from the remote develop branch and reset the master branch, and specify branch: temp for the action, the workflow would create a temp branch with the same commits that you have pushed to develop branch and open a PR from temp to master branch. In subsequent commits to develop, it would keep on making the same changes to temp branch and open a PR similarly or update the existing PR.
name: Create pull request
on:
push:
branches:
- develop
jobs:
prForMasterBranch:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: master
- name: Reset master branch
run: |
git fetch origin develop:develop
git reset --hard develop
- name: Create Pull Request
uses: peter-evans/create-pull-request#v4
with:
commit-message: update master branch
title: Update master branch
branch: temp
delete-branch: true
assignees: user-you-want-to
reviewers: user-you-want-to
Note that the temp branch will have the exact commits that are pushed to the develop branch.
PR without intermediate branch
Replace
team-you-want-to
or
user-you-want-to
with team or user you want assign if needed, if not comment them.
name: Create pull request
on:
push:
branches:
- develop
jobs:
reatePullRequest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: master
- name: Get latest changes
run: |
git fetch origin develop:develop
git reset --hard develop
- name: Create Pull Request
id: cpr
uses: peter-evans/create-pull-request#v4
with:
commit-message: Update master
committer: GitHub <noreply#github.com>
author: ${{ github.actor }} <${{ github.actor }}#users.noreply.github.com>
signoff: false
branch: develop
title: 'Updating master'
labels: |
update
reviewers: user-you-want-to
team-reviewers: |
team-you-want-to
draft: false
I host my git-repo on GitHub.com. Several developers contribute code to either the develop- or feature branch respectively. Once all the tests have passed, the changes are merged into master.
Now, once such a pull-request/merge has been done, the master brach shall be pushed automatically to another GitHub-Repo (where another team picks it up). This is when "Workflows" come into play.
So I created this yaml file to trigger a "git push" after a successfull pull-requrest:
name: push master to official repository
on:
pull_request:
types: [closed]
jobs:
gitHubPush:
runs-on: ubuntu-latest
steps:
- run: "git push https://github.com/OFFICIAL/MyProject master"
But this doesn't work. I get:
fatal: not a git repository (or any of the parent directories): .git
Error: Process completed with exit code 128.
The GitHub Account is registered as "Contributor" on OFFICIAL.
Can you help me out ?
You have to checkout your repository first:
- name: Checkout
uses: actions/checkout#v2
More information about checkout options to find the best fitting your needs here: https://github.com/actions/checkout/
Currently, I'm trying to automate the bump versions for the applications as part of the PR Merge using the GitHub Actions. There is a custom script that would identify the current version of the application and the label attached with the PR and bump the major|minor|patch version accordingly on the file where the version numbers are stored. It is important that the version bump happens only at the time when PR is merged and as part of GitHub Actions because it helps in avoiding the merge conflict in the version file and takes away the manual way of bumping the version.
The GitHub Actions code snippet is given below.
jobs:
release:
# Skip on Pull Request Close event.
if: "!(github.event_name == 'pull_request' && !github.event.pull_request.merged)"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: ${{ github.ref }}
- id: bumpversion
if: github.event.pull_request.merged
env:
PR_LABELS: ${{ toJson(github.event.pull_request.labels) }}
run: bash .github/scripts/bump_version.sh -l "${PR_LABELS}"
The bump_version.sh script has a function given below that makes the version changes and then pushes them to the main branch.
git config user.email "${GITHUB_ACTOR}#users.noreply.github.com"
echo "commiting the version bump changes..."
git add .
git commit -m "Bump Version v${current_version} -> v${incremented_version}"
echo "pushing the version bump changes..."
git push origin dev
This runs fine but the problem is that it makes 2 commits on the main branch when a PR is merged to it. There is a CI/CD pipeline that listens to this main branch for changes and it gets triggered when there is a new commit to it. Since this PR merge makes 2 commits, it is triggered twice.
2 commits for single PR merge image
The question: Is it possible to update files as part of a merge via GitHub Actions without creating a second commit? Is there any other way to achieve a solution to this problem that will help me to bump the version as part of the PR Merge?
My immediate thought is that you could alter the trigger ("on:") in CI/CD to ignore the first of the 2 commits, and only run CI/CD on the second one.
There's a number of ways you could do that, utilizing any of the metadata that comes with a commit. Here's an idea that utilizes the tag. You'd have to refactor the code that defines incremented_version probably, but shouldn't be hard to do in bash.
Here's a CI/CD that would ignore something tagged *.0.0, but would trigger on anything like *.1 or *.0.1:
on:
push:
# Sequence of patterns matched against refs/tags
tags:
- !v*.0.0
- !v*.0 # Do not push events to tags v1.0.0, v1.0, v2.0.0, and v2.0, etc.
Personally I like the idea of making the 1st of the 2 commits have an extra .0 and ignoring those. It seems cleaner. After configuring the bash script to do that,:
on:
push:
# Sequence of patterns matched against refs/tags
tags:
- !v*.0.0 # Do not push events to tags v1.0.0, v2.0.0, etc.
I have GitHub repository with 2 branches: "master" & "develop".
The workflow for us is that any code should be committed to the "develop" branch then pushed to GitHub, then a Pull Request should be created to merge the commits into the "master" branch.
I am trying to write an Action that will create a Pull Request once a developer pushes commits to the branch "develop" and had the following script:
name: Create pull request
on:
push:
branches:
- develop
jobs:
prForMasterBranch:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: master
- name: Create Pull Request
uses: peter-evans/create-pull-request#v2
with:
commit-message: update master branch
title: Update master branch
branch: develop
I can see that this action has executed successfully on "Push" event of the "develop" branch, but I can't see any new Pull Requests!
I checked the logs for the action and found these lines at the end of pull request creation:
Pushing pull request branch to 'origin/develop'
Branch 'develop' no longer differs from base branch 'master'
Closing pull request and deleting branch 'develop'
It seems I am missing something, but couldn't figure it out.
Any help is appreciated.
If you look at the documentation of the create-pull-request action, it mentions that
Create Pull Request action will:
Check for repository changes in the Actions workspace. This includes:
untracked (new) files
- tracked (modified) files
- commits made during the workflow that have not been pushed
Commit all changes to a new branch,
or update an existing pull request branch.
Create a pull request to
merge the new branch into the base—the branch checked out in the
workflow.
It would always need an intermediary branch where it can commit the changes.
So if you modify your workflow config as below, adding the Reset master branch step to get the latest changes from the remote develop branch and reset the master branch, and specify branch: temp for the action, the workflow would create a temp branch with the same commits that you have pushed to develop branch and open a PR from temp to master branch. In subsequent commits to develop, it would keep on making the same changes to temp branch and open a PR similarly or update the existing PR.
name: Create pull request
on:
push:
branches:
- develop
jobs:
prForMasterBranch:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: master
- name: Reset master branch
run: |
git fetch origin develop:develop
git reset --hard develop
- name: Create Pull Request
uses: peter-evans/create-pull-request#v4
with:
commit-message: update master branch
title: Update master branch
branch: temp
delete-branch: true
assignees: user-you-want-to
reviewers: user-you-want-to
Note that the temp branch will have the exact commits that are pushed to the develop branch.
PR without intermediate branch
Replace
team-you-want-to
or
user-you-want-to
with team or user you want assign if needed, if not comment them.
name: Create pull request
on:
push:
branches:
- develop
jobs:
reatePullRequest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: master
- name: Get latest changes
run: |
git fetch origin develop:develop
git reset --hard develop
- name: Create Pull Request
id: cpr
uses: peter-evans/create-pull-request#v4
with:
commit-message: Update master
committer: GitHub <noreply#github.com>
author: ${{ github.actor }} <${{ github.actor }}#users.noreply.github.com>
signoff: false
branch: develop
title: 'Updating master'
labels: |
update
reviewers: user-you-want-to
team-reviewers: |
team-you-want-to
draft: false
I have GitHub repository with 2 branches: "master" & "develop".
The workflow for us is that any code should be committed to the "develop" branch then pushed to GitHub, then a Pull Request should be created to merge the commits into the "master" branch.
I am trying to write an Action that will create a Pull Request once a developer pushes commits to the branch "develop" and had the following script:
name: Create pull request
on:
push:
branches:
- develop
jobs:
prForMasterBranch:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: master
- name: Create Pull Request
uses: peter-evans/create-pull-request#v2
with:
commit-message: update master branch
title: Update master branch
branch: develop
I can see that this action has executed successfully on "Push" event of the "develop" branch, but I can't see any new Pull Requests!
I checked the logs for the action and found these lines at the end of pull request creation:
Pushing pull request branch to 'origin/develop'
Branch 'develop' no longer differs from base branch 'master'
Closing pull request and deleting branch 'develop'
It seems I am missing something, but couldn't figure it out.
Any help is appreciated.
If you look at the documentation of the create-pull-request action, it mentions that
Create Pull Request action will:
Check for repository changes in the Actions workspace. This includes:
untracked (new) files
- tracked (modified) files
- commits made during the workflow that have not been pushed
Commit all changes to a new branch,
or update an existing pull request branch.
Create a pull request to
merge the new branch into the base—the branch checked out in the
workflow.
It would always need an intermediary branch where it can commit the changes.
So if you modify your workflow config as below, adding the Reset master branch step to get the latest changes from the remote develop branch and reset the master branch, and specify branch: temp for the action, the workflow would create a temp branch with the same commits that you have pushed to develop branch and open a PR from temp to master branch. In subsequent commits to develop, it would keep on making the same changes to temp branch and open a PR similarly or update the existing PR.
name: Create pull request
on:
push:
branches:
- develop
jobs:
prForMasterBranch:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: master
- name: Reset master branch
run: |
git fetch origin develop:develop
git reset --hard develop
- name: Create Pull Request
uses: peter-evans/create-pull-request#v4
with:
commit-message: update master branch
title: Update master branch
branch: temp
delete-branch: true
assignees: user-you-want-to
reviewers: user-you-want-to
Note that the temp branch will have the exact commits that are pushed to the develop branch.
PR without intermediate branch
Replace
team-you-want-to
or
user-you-want-to
with team or user you want assign if needed, if not comment them.
name: Create pull request
on:
push:
branches:
- develop
jobs:
reatePullRequest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: master
- name: Get latest changes
run: |
git fetch origin develop:develop
git reset --hard develop
- name: Create Pull Request
id: cpr
uses: peter-evans/create-pull-request#v4
with:
commit-message: Update master
committer: GitHub <noreply#github.com>
author: ${{ github.actor }} <${{ github.actor }}#users.noreply.github.com>
signoff: false
branch: develop
title: 'Updating master'
labels: |
update
reviewers: user-you-want-to
team-reviewers: |
team-you-want-to
draft: false