enzo
enzo

Reputation: 11486

How can I skip a job based on the changes made in .gitlab-ci.yml?

I have two jobs in GitLab CI/CD: build-job (which takes ~10 minutes) and deploy-job (which takes ~2 minutes). In a pipeline, build-job succeeded while deploy-job failed because of a typo in the script. How can I fix this typo and run only deploy-job, instead of rerunning build-job?

My current .gitlab-ci.yml is as follows:

stages:
  - build
  - deploy

build-job:
  image: ...
  stage: build
  only:
    refs:
      - main
    changes:
      - .gitlab-ci.yml
      - pubspec.yaml
      - test/**/*
      - lib/**/*
  script:
      - ...
  artifacts:
    paths:
      - ...
    expire_in: 1 hour

deploy-job:
  image: ...
  stage: deploy
  dependencies:
    - build-job
  only:
    refs:
      - main
    changes:
      - .gitlab-ci.yml
      - pubspec.yaml
      - test/**/*
      - lib/**/*

  script:
    - ...

I imagine something like:

  1. the triggerer (me) fixes the typo in deploy-job's script and pushes the changes
  2. the pipeline is triggered
  3. the runner looks at the files described by build-job/only/changes and detects a single change in the .gitlab-ci.yml file
  4. the runner looks at the .gitlab-ci.yml file and detects there IS a change, but since this change does not belong to the build-job section AND the job previously succeeded, this job is skipped
  5. the runner looks at the files described by deploy-job/only/changes and detects a single change in the .gitlab-ci.yml file
  6. the runner looks at the .gitlab-ci.yml file and detects there is a change, and since this change belongs to the deploy-job section, this job is executed

This way, only deploy-job is executed. Can this be done, either using rules or only?

Upvotes: 2

Views: 1842

Answers (1)

Agustin L. Lacuara
Agustin L. Lacuara

Reputation: 339

There is a straight foward way, you just have to have access to gitlab CI interface.

  1. Push and let the pipeline start normally.
  2. Cancel the fist job.
  3. Run the desired one (deploy).

Another way is to create a new job, intended to be runned only when needed, in which you can set a non-dependency deploy. Just copy the current deploy code without the dependencies. Also add the manual mode with

job:
script: echo "Hello, Rules!"
rules:
  - if: $CI_PIPELINE_SOURCE == "merge_request_event"
    when: manual
    allow_failure: true

As docs.

Upvotes: 1

Related Questions