Hi
I'm trying to get the changed folders in bitbucket pipeline, but I'm not able to do that.
I'm using the command
When I put hardcoded commit it is working fine ie.e
git fetch origin $BITBUCKET_PR_DESTINATION_BRANCH
git diff --name-only --diff-filter=d abwjdsefd
I'm trying to store the output in some other variable.
Script
Hello @Shubham and welcome to the community!
The variable $BITBUCKET_COMMIT will contain the commit that triggered the pipeline. Once a pipeline starts, it will clone the repository inside the build container and checkout to that commit, which means the the head of the local repository will be moved to that commit.
Once you execute the command
git diff --name-only --diff-filter=d $BITBUCKET_COMMIT
You are essentially comparing the current HEAD with the provided commit $BITBUCKET_COMMIT. However, since they are the same commit (pipelines resets the HEAD to the commit that triggered it), it will return nothing, as you would be comparing a commit with itself.
If your end goal is to check what folders changed in the commit that triggered the pull request compared the destination branch of the pull request, you should provide the destination branch of the PR in the git diff command instead :
git diff --name-only --diff-filter=d $BITBUCKET_PR_DESTINATION_BRANCH
Could you try with that approach and let us know how it goes ?
Should you have any questions, feel free to ask :)
Thank you, @Shubham !
Patrik S
Hi Patrik
Thanks for the help.
I tried above approach. But it didn't work for my use case.
My pipeline is
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @Shubham ,
The variable $BITBUCKET_PR_DESTINATION_BRANCH will only be available in a pull-request pipeline config.
In the branch pipeline config you have (that runs for the merge commit) this variable will be empty, so this is the reason the diff is not showing anything.
If you want to also diff the files in the branch pipeline, you will need to compare the commit that is running the pipeline (the merge commit in this case), with one of its parents. Since the merge commit has two parents (one for the feature branch and the other for the main branch), you can use the modifier ^ to identify which parent you want.
The first parent of a merge commit (^1) is from the branch you were on when you merged (usually main/master), while the second parent (^2) of a merge commit is from the branch that was merged (feature branch).
So in your case, since you are interested in the diffs introduced by the feature branch (the source branch of the pull request), you would need to use the following command in your branch pipeline config :
branches:
shubham-pipeline-test:
- step:
name: GetModifiedFilesBranchPipeline
runs-on:
- 'self.hosted'
image:
name: 'golang:1.18'
script:
- git diff --name-only --diff-filter=d HEAD^1
This will instruct git to calculate the diff between HEAD (the merge commit), and its first parent (the previous commit in the main branch). This would show all the files introduced by the feature branch into the merge commit.
Hope that helps! Should you have any questions, feel free to ask
Thank you, @Shubham !
Patrik S
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You're ver welcome!
Happy to have been of some help :)
Feel free to reach out to the community if you ever need assistance in the future.
Thank you, @Shubham !
Patrik S
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.