Sushil.R
Sushil.R

Reputation: 117

How to exclude gitlab-ci.yml changes from triggering a job

I am unable to find a solution for how to ignore changes made in .gitlab-ci.yml to trigger a job. So far I have tried the below options:

except:
  changes:
  - .gitlab-ci.yml

and

only
 - Branch A

but every time i make changes in .gitlab.ci-yml file, jobs for Stage B get added in pipeline and show as skipped.

Below are the jobs defined in .gitlab-ci.yml. Do you have any suggestion here?

I do not want Stage B jobs get added in pipeline when:

i) push made against the .gitlab-ci.yml (either manual changing file or git push command)
ii) any merge request for .gitlab-ci.yml

stages:
 - A
 - B
 
Stage A:
  stage: A
  script:
    - echo "TEST"
  rules:
    - if: '$CI_COMMIT_TAG =~ /^\d+\.\d+\.DEV\d+/'
  tags:
    - runner
    
Stage B:
  stage: B
  script:
    - echo "TEST"
  when: manual
  tags:
    - runner

Upvotes: 2

Views: 7430

Answers (3)

Alex
Alex

Reputation: 3431

If you want to exclude pipelines on certain file changes, but include pipelines on other file changes, use two if statements:

build:
  stage: build
  script:
    - ...
  rules:
    - if: $CI_COMMIT_BRANCH == "master"
      changes:
        - ".gitlab-ci.yml"
        - "mkdocs.yml"
      when: never
    - if: $CI_COMMIT_BRANCH == "master"
      changes:
        - Dockerfile
        - "*.yml"

The second if-statement will not be evaluated if the first one is matched.

Upvotes: 0

Pelayo
Pelayo

Reputation: 114

Could you try using workflow rule? It should determine if the pipeline is created.

P.S: Someone complained a couple of years ago about not being able to activate manual jobs after the exception, but it looks like it was a bug. I can't find the issue mentioned in the post

Edit:

This conf skips any commit with changes README.md or .gitlab-ci.yml:

workflow:
    rules:
        - if:
          changes: 
            - README.md
            - .gitlab-ci.yml
          when: never

Upvotes: 1

Davide Madrisan
Davide Madrisan

Reputation: 2300

With this setup the Stage B is not added if .gitlab-ci.yml is modified:

stages:
 - A
 - B

Stage A:
  stage: A
  script:
    - echo "Stage A"
  tags:
    - runner

Stage B:
  stage: B
  script:
    - echo "Stage B"
  rules:
    - changes:
      - ".gitlab-ci.yml"
      when: never
    - when: manual
  tags:
   - runner

Otherwise Stage B is showed in the pipeline and can be run manually. Tested with GitLab CI CE 14.1.0.

Upvotes: 2

Related Questions