Please help with gitlab-ci.yml the goal is to do not run pipeline when creating a copy branch from product named with auto-* and do not run pipeline when merging back from auto-* to product branch. BUT run pipeline when just commit to auto-* or to product and run pipeline for product when merging to it from non auto-* branches . Tried bellow workflow but does not work... pipeline runs all the time(
In short I do not want to run pipeline when creating for example auto-testing branch from product and do not want to run it when merging auto-testing back to product
By the way pipeline uploads to the same folder for both product and auto-*
workflow:
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME =~ /^auto-(.*)$/'
when: never
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_COMMIT_BRANCH =~ /^auto-(.*)$/'
when: never
- if: '$CI_COMMIT_BRANCH == "product" || $CI_COMMIT_BRANCH =~ /^auto-(.*)$/'
Your problem is, that '$CI_PIPELINE_SOURCE == "merge_request_event" only covers pipelines that run within merge requests, not those that run after you merge it. GitLab does not differentiate Merge-Request results from normal pushes, so for those the variable will hold CI_PIPELINE_SOURCE=push.
In fact, there is no built-in way in GitLab to determine where a MR came frome that triggered a pipeline. As explained here you can use a workaround to get this information using the API.
Maybe a solution for you could be to built a pipeline that runs, requests the source branch using the API and immediately stops if the source branch starts with auto-*.
Related
I am running a Jira Cloud instance that is integrated with a Gitlab Cloud (SaaS, no paid subscription) repo using the official tools. I have successfully created several automation rules in Jira, so both apps are well configured and connected, but there is one rule that is not working, it is not being triggered
My goal is to detect in Jira when the pipeline fails in order to change the ticket status
My desired flow is
User creates Merge Request
1.1 Jira detects this event and changes ticket status
1.2 Pipeline is run for $CI_PIPELINE_SOURCE == 'merge_request_event' jobs
1.3 Pipeline finishes (uses feature branch)
1.4 Jira detects failed status and changes ticket status
User merges the MR
2.1 Jira detects this event and changes ticket status
2-2 Pipeline is run for $CI_COMMIT_BRANCH == 'main' && $CI_PIPELINE_SOURCE == 'push' jobs
2.3 Pipeline finishes (uses main branch after the merge)
2.4 Jira detects failed status and changes ticket status
This flow is working except for the last step. I have created the Jira rule for it but the rule is not being triggered. Since the rule trigger is cloned from the 1.4 rule, I am assuming that the problem is that the integration is failing, Gitlab is not publishing the event
This is my .gitlab-ci.yml file
stages: # List of stages for jobs, and their order of execution
- build
- deploy
build-job: # This job runs the build stage, which runs on Merge Request creation.
stage: build
rules:
- if: $CI_PIPELINE_SOURCE == 'merge_request_event'
script:
- echo "Compiling the code..."
- echo "Compile complete."
deploy-job: # This job runs the deploy stage, which runs on Merge into Main branch.
stage: deploy
rules:
- if: $CI_COMMIT_BRANCH == 'main' && $CI_PIPELINE_SOURCE == 'push'
script:
- echo "Deploying the code..."
- echo "Deploy complete."
- fakecommand
That fakecommand is on purpose for forcing the pipeline to fail
I created a rule for logging all build state changes.
You can see in the screenshot that all status changed are being detected but the last one (successful to failed)
Should I modified my pipeline? Should I use another rule trigger in Jira? As you can guess, my goal is to have feedback on the main branch performance after the merge is completed.
I'm trying to modify an existing gitlab CI pipeline so that the "deploy" stage will not fire if the merge request's list of changed files is unrelated to the actual code being deployed.
deploy:
extends: .standard_template
only:
- master
- main
Right now deploy only occurs only on master or main branches and I'd like to continue that same logic, but I would also like to introduce the additional logic that if the ONLY thing changed is readme.md then don't do the deploy.
I've seen gitlabs support for the changes: rule, that appears to expect a matching subset of the listed files. What I want is to match a full set of readme.md and abort/not start the deploy.
Is this possible with gitlab's syntax? I know I could write additional "jobs" to do my own script: rules do stuff with the git-tree to inspect changes, but then I have to exit 1 to basically cause the pipeline to fail, which leads to a bunch of red failures or incomplete jobs in the build, when what I really want is just to omit this portion.
You can do this with a couple of rules:
deploy:
extends: .standard_template
rules:
- if: $CI_COMMIT_BRANCH != "main" && $CI_COMMIT_BRANCH != "master"
when: never
- changes:
- readme.md
when: never
- when: always
The first rule is the same as your only: main or master clause. If $CI_COMMIT_BRANCH is anything other than main or master, it won't add the job to the pipeline.
The second rule looks to see if there are changes to the readme.md file, but explicitly adds a when condition: never.
The third rule is our default case if the first two don't match.
All together this is, don't run if the branch isn't main/master, don't run if there are changes to the readme, otherwise always run.
Note: You'll likely have to extend this a bit since this doesn't account for Tag pipelines, merge request pipelines, etc., but that's simple to do with some additional rules.
I want GitLab CI to run a job after a merge request is merged. I don't want it to be run on CREATING a new merge request and also I don't want it to be run whenever target branch is updated. (Since it's possible to commit directly to target branch and the job should not be run in that situation.)
Is that possible?
If yes, I also want to know informations about the merge request which triggered the job.
(Actually I want to update my project management system, when a merge request is merged. Thus I need to know which merge request is merged (or approved).)
Thanks in advance.
I want GitLab CI to run a job after a merge request is merged.
Unfortunately, GitLab does not offer a "merge request merged" trigger.
What you can do, is to make the pipeline run for any push in a certain branch and use branch-protection to make sure pushs can only come from merge requests. To do that:
Set your pipeline to only run e.g. for branch main:
# if you want to use "only":
my_job:
only:
- main
# or alternatively if you want to use rules you can do the same with:
my_job:
rules:
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_REF_NAME == "master"'
Enable Branch protection to disable direct pushes to main and only allow MRs:
You can run the pipeline after merge by using Gitlab ci predefined variable $CI_MERGE_REQUEST_APPROVED this will return true after merge has been done and available from gitlab v14.1.
you can add the rule like this in your job.
rules:
- if: $CI_MERGE_REQUEST_APPROVED
I want a step in the build process only to run on the master branch, if there where changes to the src folder.
My .gitlab-ci.yml file thus contains:
build:php:
stage: build
image: alpine
interruptible: true
needs: [ "test:php" ]
script:
- do stuff // abreviated for simplicity
rules:
- if: $LANGUAGE_RELEASE
when: never
- if: '$CI_COMMIT_REF_SLUG == "master"' # run for production test branch
- changes:
- src/*
However, the issue here is, that it also runs on the dev branch, when I change anything.
Question: Is the a way to have this step only run, both conditions (the branch and the changes) are met?
When using the rules keyword, the rules:if clause may be used, with the variable $CI_COMMIT_BRANCH.
Thus, something like below to specify master as the only branch to run the job:
build:php:
stage: build
# ...
rules:
- if: '$CI_COMMIT_BRANCH == "master"'
# ...
(Rules are applied in order)
The documentation reference for common if clauses available is here.
Now to combine an if and changes rule, you'll need to use
build:php:
stage: build
# ...
rules:
- if: '$CI_COMMIT_BRANCH == "master"'
changes:
- file1 # single file
- folder/**/* # folder including all files and subfolders
# ...
You can read more about changes used in rules here and read the full changes specification here.
With the initial setup there where two issues:
the changes where seen as a new rule (since they had a - in front)
src/* only takes the src folder in consideration, not sub folders, for that you'll need src/**/*
It is also possible to use the only keyword to specify master as the only branch to run the job. Despite being simple, this is no longer encouraged and it cannot be used together with rules (only/except reference).
Example:
build:php:
stage: build
# ...
only:
- master
# ...
There is no way to run rules:changes on the master branch.
Gitlab docs says:
You should use rules: changes only with branch pipelines or merge
request pipelines. You can use rules: changes with other pipeline
types, but rules: changes always evaluates to true when there is no
Git push event. Tag pipelines, scheduled pipelines, manual pipelines,
and so on do not have a Git push event associated with them.
It means changes gives always true if there is different type than branches or merge_requests
It seems rules replaces only/except functionality in the latests GitLab versions.
Before, specifying that a job had to be executed only for master branch, for example, was very straightforward.
How would that be done with rules?
I'm guessing GitLab provides some variable that specifies the current branch's name, but I cannot find that. The only examples I see are regarding merge requests.
In other words, if I have the following job, how to restrict it to run only in potato branch?
unit_tests:
stage: test
script: dotnet vstest test/*UnitTests/bin/Release/**/*UnitTests.dll --Blame
rules:
- exists:
- test/*UnitTests/*UnitTests.csproj
I guess this would be it:
unit_tests:
stage: test
script: dotnet vstest test/*UnitTests/bin/Release/**/*UnitTests.dll --Blame
rules:
- if: $CI_COMMIT_BRANCH == "potato"
Here are the variable references:
https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
Here is an example from gitlab-runner project source code itself
https://gitlab.com/gitlab-org/gitlab-runner/-/blob/main/.gitlab/ci/test.gitlab-ci.yml
job-name:
script:
- echo "i am potato"
rules:
- if: '$CI_COMMIT_BRANCH == "potato"'