zephyr
zephyr

Reputation: 2332

Create Manual GitLab CI Job to be Run Before Final Merge

I have a job in GitLab CI and I want to set it up to be run under the following conditions.

  1. It is a manual job that must be triggered by the user.
  2. It must gate a MR being completed in that the user must run it before the MR is allowed to go through.

The reason I'm looking for this type of set up is that the job is extremely long and takes over certain limited resources. Because of that, I don't want the job to run for every commit, only as a last step before a MR is put through. Is it possible to set things up this way?

I'm using GitLab Community Edition 11.7.0.

Upvotes: 3

Views: 1798

Answers (3)

Efren
Efren

Reputation: 4907

Found an option credit to this blog:

Create a job at the last stage of the pipeline (this is only so you get any other relevant job done and reflect a reasonable state)

Prerequisite:

  • Repo/Project settings:
    • General > Merge requests
      • [Enabled] Merge checks: Pipelines must succeed

Use case covers OP scenario:

  • Pipeline jobs only for merge requests
  • Merge is blocked until a job is run and the workflow allows to run manually
  • Pipeline succeeds and allows merge only after all jobs pass and last manual job passed.
  • Pipeline fails if any job fails
  • Pipeline is blocked if all jobs pass except the last manual (gate) job
    • Merge is blocked when pipeline is blocked

Example yml:

stages:
  - first
  - cleanup

first-job-only-mr:
  stage: first
  only:
    refs:
      - merge_requests
  script:
    - echo "First job - on Merge request only"

last-job-manual-block-MR:
  stage: last
  # this is the key config
  allow_failure: false
  when: manual
  only:
    refs:
      - merge_requests
  script:
    - echo "Manual job on Merge request only, blocks pipeline if not run, blocking merge"

Pipeline state

Pipeline blocked at manual gate job

Merge overview

Merge blocked with blocked pipeline state

Upvotes: 0

MrBerta
MrBerta

Reputation: 2758

I don't think that this is possible in GitLab at the moment. They are working on an issue that they call "Prospective merge pipelines" that sounds like what you are searching for:

https://gitlab.com/gitlab-org/gitlab-ee/issues/7380

The work flow they describe is:

  • A user presses merge
  • A pipeline is started on a post-merge codebase
  • Only merge if there are no merge conflicts, all jobs succeed, and the target branch haven't moved forward.

As a workaround, you can maybe use "when:manual":

https://docs.gitlab.com/ee/ci/yaml/#whenmanual

Or some other of GitLab CI's feature to only trigger the pipeline manually, and then have as a rule as the users have to paste a link to the pipeline that succeeded before they are allowed to press the merge button.

Upvotes: 1

King Chung Huang
King Chung Huang

Reputation: 5644

Yup! GitLab 11.6 introduced pipelines for merge requests. For what you described, I suggest creating a job that is only for merge requests, and not allow failure.

gate:
  script: "true"
  only:
    - merge_requests
  allow_failure: false

You'll need to fill in the actual script, job stage, and any other details.

Upvotes: 1

Related Questions