I've got a CloudBuild trigger setup listening to a bitbucket repository. Once the build is complete, I've a cloud function which sends a slack message to the author of the commit.
The problem I have, is bitbucket triggers a repo:push
on branch creation (though their UI). This triggers a build, and then notifies the author of the last commit from the source branch that a new build is ready.
To prevent this, I decided to prevent the slack message triggering if the last commit on the triggered branch is also on master (the source branch). I've tried this a number of ways, but it seems like the clone CloudBuild uses when triggered is NOT a full clone. I can't quite work out what state the local repo is in... looks like maybe CloudBuild calls the branch master
locally, no matter what branch triggered the build? For example, commands like this:
- name: 'gcr.io/cloud-builders/git'
entrypoint: /bin/bash
args:
- -c
- |
# Write the author name to the cloud build output
git show -s --format='%ae' $COMMIT_SHA >> /builder/outputs/output
# Write the latest commit to the output.
git rev-list ^master $COMMIT_SHA -1 >> /builder/outputs/output
Gives blank output, even when there are changes on mybranch
that are not reachable by master
. The following similar command has the same result:
git log master..$COMMIT_SHA --oneline -1 >> /builder/outputs/output
Both of these commands correctly return the branch commits when I run locally.
If I add some explicit checkout steps like this (before the rev-list):
git checkout origin master
git checkout origin $BRANCH_NAME
I get this:
From https://source.developers.google.com/p/myproject/r/bitbucket_myproject_myrepo
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
fatal: refusing to merge unrelated histories
From https://source.developers.google.com/p/myproject/r/bitbucket_myproject_myrepo
* branch feature/mybranch -> FETCH_HEAD
* [new branch] feature/mybranch -> origin/feature/mybranch
Already up to date.
Incidentially, this command
git log master..$BRANCH_NAME --oneline -1 >> /builder/outputs/output
throws this error
fatal: ambiguous argument 'master..feature/mybranch': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
however, this command does not (but leaves me with the dreaded blank value):
git log master..$COMMIT_SHA --oneline -1 >> /builder/outputs/output
I'm super confused. Can anyone help me with what exactly CloudBuild does when it does the FETCHSOURCE
step?
FETCHSOURCE
Initialized empty Git repository in /workspace/.git/
From https://source.developers.google.com/p/myproject/r/bitbucket_myproject_myrepo
* branch 1ed4397bd9825b3eb9fe8dca12409b06975a9c41 -> FETCH_HEAD
HEAD is now at 1ed4397 some commit message
and how I can achieve my aim of getting a list of commits that appear only on my branch (not master)?
--- UPDATED ---
After @bhito suggested the shallow fetch, I've almost been able to reproduce locally, but there is still a difference I can't work out. Doing the following locally works:
$ git init
$ git remote add origin myrepo
$ git fetch --depth 1 origin b72b37
$ git checkout FETCH_HEAD
$ git fetch --unshallow
$ git rev-list ^master feature/mybranch -1
fatal: bad revision '^master'
but then if I do this:
$ git checkout master
$ git checkout feature/mybranch
$ git rev-list ^master feature/mybranch -1
it works!! But only locally. My reproduce is not quite right, since in the CloudBuild clone, when I try and checkout master I get:
Step #0 - "Setting deployment environment": Already on 'master'
It seems CloudBuild fetches the commit onto a branch called master... I'm not sure how and so can't figure out how to get back to where I can perform the rev-list