Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a specific job in gitlab CI

We are facing a problem where we need to run one specific job in gitlab CI. We currently not know how to solve this problem. We have multitple jobs defined in our .gitlab-ci.yml but we only need to run a single job within our pipelines. How could we just run one job e.g. job1 or job2? We can't use tags or branches as a software switch in our environment.

.gitlab-ci.yml:

before_script:
  - docker info

job1:
  script:
    - do something

job2:
  script:
    - do something
like image 308
lin Avatar asked Mar 23 '17 20:03

lin


People also ask

How do I run a manual job in GitLab?

In GitLab CI/CD you can easily configure a job to require manual intervention before it runs. The job gets added to the pipeline, but doesn't run until you click the play button on it. Notice that the manual job gets skipped, and the pipeline completes successfully even though the manual job did not get triggered.

Which file is used to specify the jobs within the GitLab CI pipeline?

gitlab-ci. yml file. Jobs are: Defined with constraints stating under what conditions they should be executed.


1 Answers

You can use a gitlab variable expression with only/except like below and then pass the variable into the pipeline execution as needed.

This example defaults to running both jobs, but if passed 'true' for "firstJobOnly" it only runs the first job.

Old Approach -- (still valid as of gitlab 13.8) - only/except

variables:
  firstJobOnly: 'false'

before_script:
  - docker info

job1:
  script:
    - do something

job2:
  script:
    - do something
  except:
    variables:
      - $firstJobOnly =~ /true/i

Updated Approach - rules

While the above still works, the best way to accomplish this now would be using the rules syntax. A simple example similar to my original reply is below.

If you explore the options in the rules syntax, depending on the specific project constraints there are many ways this could be achieved.

variables:
  firstJobOnly: 'false'

job1:
  script: 
    - do something

job2:
  script: 
    - do something
  rules:
    - if: '$firstJobOnly == "true"'
      when: never
    - when: always

like image 200
zm31 Avatar answered Oct 18 '22 22:10

zm31