I am trying to create a base image for my repo that is optionally re-built when branches (merge requests) make changes to dependencies.
Let’s say I have this pipeline configuration:
stages:
- Test
- Build
variables:
- image: main
Changes A:
stage: Test
rules:
- if: '$CI_PIPELINE_SOURCE == "push"'
changes:
- path/to/a
script:
- docker build -t a .
- docker push a
- echo 'image=a' > dotenv
artifacts:
reports:
dotenv: dotenv
Build:
stage: Build
image: $image
script:
- echo build from $image
Let’s say I push to a new branch and the first commit changes /path/to/a
, the docker image is build and pushed, the dotenv is updated and the Build
job successfully uses image=a
.
Now, let’s say I push a new commit to the same branch. However, the new commit does not change /path/to/a
so the Changes A
job does not run. Now, the Build
stage pulls the "wrong" default image=main
while I would like it to still pull image=a
since it builds on top of the previous commit.
Any ideas on how to deal with this?
- Is there a way to make
rules.changes
refer toorigin/main
? - Any other ideas on how to achieve what I am trying to do?
2
Answers
There is a project setting, which defines how your MR pipelines are setup. This is only working for Merge requests and can be found in
Settings -> Merge Requests
under the sectionMerge options
each commit individually – nothing checked
this means, each commit is treated on its own, and changes checks are done against the triggering commit on it’s own
Enabled merged results pipeline
This will merge your MR with the target branch before running the CI Jobs. This also will evaluate all your changes, and take a look at all of them within the MR and not commit wise.
Merge trains
This is a whole different chapter, and for this usecase not relevant. But for completeness i have to mention it see https://gitlab.com/help/ci/pipelines/merge_trains.md
What you are looking for is Option 2 –
Merged pipeline results
. but as i said, this will only work in Merge Request pipelines and not general pipelines. So you would also need to adapt your rules to something like:Yes, there is, since GitLab 15.3 (August 2022):
See Documentation and Issue.
Example: