How to run Job when Pipeline was triggered manually

Viewed 569

I setup jobs to run only when pushing/merging to branch "dev", but I also want it so I'm able to run them if I trigger that pipeline manually. Something like this:

test:
  stage: test
  <this step should be run always>

build:
  stage: build
  rules:
    - if: $CI_COMMIT_REF_NAME == "dev"
    - if: <also run if the pipeline was run manually, but skip if it was triggered by something else>

This job is defined in a child "trigger" pipeline. This is how the parent looks like:

include:
  - template: 'Workflows/MergeRequest-Pipelines.gitlab-ci.yml'

stages:
  - triggers

microservice_a:
  stage: triggers
  trigger:
    include: microservice_a/.gitlab-ci.microservice_a.yml
    strategy: depend
  rules:
    - changes:
        - microservice_a/*

The effect I want to achieve is:

  • Run test in all cases
  • Run build in the child pipeline only when pushing/merging to "dev"
  • Also run the build job when the pipeline is run maually
  • Do not run the build job on any other cases (like a MR)
1 Answers

The rules examples showcase:

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

The when:manual should be enough in your case: it does require that a job doesn’t run unless a user starts it.


Bonus question: This job is defined in a child "trigger" pipeline

Then it is related to gitlab-org/gitlab issue 201938, which is supposed to be fixed with GitLab 13.5 (Oct. 2020), but that only allow manual actions for parent-child pipeline (illustrated by this thread)

Double-check the environment variables as set in your child job

echo $CI_JOB_MANUAL

If true, that would indicate a job part of a manual triggered job.

While issue 22448 ("$CI_JOB_MANUAL should be set in all dependent jobs") points to this option not working, it includes a workaround.

Related