admin管理员组文章数量:1357307
I want to reduce the number of tests performed when executing a pipeline. I want to do it this way:
- Get a list of changed files for the current branch
- Run tests only for services that were affected
I can't get information about what files were changed while working on a branch after checking it out. I can only operate with the following constants: CI_COMMIT_BEFORE_SHA
and CI_COMMIT_SHA
.
I get a list of all commits for the current branch, look for the very first one, get its hash and compare it with the current one. The following command works locally:
git diff --name-only $(git log --walk-reflogs $(git branch --show-current) | grep "commit " | awk '{print $2}' | awk 'END{print}') $CI_COMMIT_SHA
Example:
$ git log --walk-reflogs $(git branch --show-current)
commit 9281739270df6c0cd297954172fe740d8b6 (HEAD -> exp1, origin/exp1
Reflog: exp1@{0} ()
Reflog message: commit: msg
Author:
Date: Fri Mar 28 17:18:34 2025 +0400
msg1
commit 4abb16bf1d0357c66e4b37b72b7e3bf2f (origin/master, master)
Reflog: exp1@{1} ()
Reflog message: branch: Created from refs/heads/master
Merge: 2fae6b22b c39f93a12
Author:
Date: Fri Mar 28 15:28:19 2025 +0300
msg
Commit with hash 4abb16bf1d0357c66e4b37b72b7e3bf2f
is the commit after the command git checkout -b exp1
(initial state of the branch). I need to compare all the following commits with it. Locally this command shows all the commits in the branch and I can find it using awk. In the pipeline this command gives me only the latest and previous commit.
How can I get which files were changed since the checkout command?
I want to reduce the number of tests performed when executing a pipeline. I want to do it this way:
- Get a list of changed files for the current branch
- Run tests only for services that were affected
I can't get information about what files were changed while working on a branch after checking it out. I can only operate with the following constants: CI_COMMIT_BEFORE_SHA
and CI_COMMIT_SHA
.
I get a list of all commits for the current branch, look for the very first one, get its hash and compare it with the current one. The following command works locally:
git diff --name-only $(git log --walk-reflogs $(git branch --show-current) | grep "commit " | awk '{print $2}' | awk 'END{print}') $CI_COMMIT_SHA
Example:
$ git log --walk-reflogs $(git branch --show-current)
commit 9281739270df6c0cd297954172fe740d8b6 (HEAD -> exp1, origin/exp1
Reflog: exp1@{0} ()
Reflog message: commit: msg
Author:
Date: Fri Mar 28 17:18:34 2025 +0400
msg1
commit 4abb16bf1d0357c66e4b37b72b7e3bf2f (origin/master, master)
Reflog: exp1@{1} ()
Reflog message: branch: Created from refs/heads/master
Merge: 2fae6b22b c39f93a12
Author:
Date: Fri Mar 28 15:28:19 2025 +0300
msg
Commit with hash 4abb16bf1d0357c66e4b37b72b7e3bf2f
is the commit after the command git checkout -b exp1
(initial state of the branch). I need to compare all the following commits with it. Locally this command shows all the commits in the branch and I can find it using awk. In the pipeline this command gives me only the latest and previous commit.
How can I get which files were changed since the checkout command?
Share Improve this question edited Mar 28 at 14:08 Gleb Kalaychev asked Mar 28 at 8:48 Gleb KalaychevGleb Kalaychev 112 bronze badges 3 |1 Answer
Reset to default 0The reason, why your command only sees the last commit in your pipeline is that only one commit is checked out in your pipeline. Try calling git fetch
before your command. If you want to reduce the output you can also use git fetch --quiet
.
I still suggest you try to use CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
and CI_MERGE_REQUEST_TARGET_BRANCH_NAME
in your pipeline like this:
git fetch --quiet
git diff --name-only ${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}...${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}
this should get you all changed files between your branch and the branch you want to merge on.
本文标签: How can I get list of changes for current branch in GitLab CIStack Overflow
版权声明:本文标题:How can I get list of changes for current branch in GitLab CI? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744047693a2581835.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
I can't get information about what files were changed while working on a branch after checking it out
why? Also, why not use rules: from gitlab-ci?How can I get which files were changed since the checkout on it?
"since the checkout" - what does it mean? You want list of changed files in current commit vs previous commit? vs different branch? Usually I get all files in current branch vs master branch. – KamilCuk Commented Mar 28 at 9:33CI_COMMIT_BEFORE_SHA
andCI_COMMIT_SHA
? UsingCI_MERGE_REQUEST_SOURCE_BRANCH_NAME
andCI_MERGE_REQUEST_TARGET_BRANCH_NAME
would make this super easy – Michael Kotzjan Commented Mar 28 at 10:05