1

Is there any way to switch a job to manual mode (and vice-versa) dynamically?

So, instead of having when: manual in gitlab-ci.yml file, it would be switched to manual (or the opposite) according to any dynamic checking.

Example: Lacking of configuration from environment variables that prevents it to run, but the user could run it later if they set the variables at the launch time.

Just playing with the current syntax, it could be like:

myjob:
   stage: deploy
   environment: proj-shared-env-qa
   script:
      - echo "Deploying $my_var..."
   when: manual && [[ ! $my_var ]] # Using shell syntax just as an example of the condition

Instead of failing:

myjob:
   stage: deploy
   environment: proj-shared-env-qa
   script:
      - [[ ! $my_var ]] && echo "my_var is undefined" && exit 1
      - echo "Deploying $my_var..."
Luciano
  • 2,695
  • 6
  • 38
  • 53

2 Answers2

1

You can try using rules to set a condition based on a variable you set in the script of the job.

myjob:
   stage: deploy
   environment: proj-shared-env-qa
   script:
      - export my_var
   rules:
     - if: $my_var
       when: manual

https://docs.gitlab.com/ee/ci/yaml/#rules-attributes

amBear
  • 900
  • 6
  • 7
1

You can make use of rules if with below example

myjob:
   stage: deploy
   environment: proj-shared-env-qa
   script:
      - echo "Deploying $my_var..."
   rules:
     - if: $my_var
       when: on_success
     - if: !$my_var
       when: manual

So if var is set then it will automatically run. if it is not set then it'll become a manual process

Ref: Gitlab Rules

Prashanna
  • 889
  • 1
  • 8
  • 13