2
votes

I need to selectively run stages in a single .gitlab-ci.yml file, while the selection is done by 1st stage for calling gitlab api.

Below is the simplified code. Finally, only jobs 1, 3, 5, 7, 9 should happen in pipeline:-

.gitlab-ci.yml :

stages:
  - select
  - deploy

select-targets:
  stage: select
  script:
    - curl "project_id=$CI_PROJECT_ID" -X POST https://some-api-endpoint
    - return some result e.g. [1, 3, 5, 7, 9]

job1:
  stage: deploy
  script:
    - to deploy job 1
  only:
    - curl return has 1

job2:
  stage: deploy
  script:
    - to deploy job 2
  only:
    - curl return has 2

job3:
  stage: deploy
  script:
    - to deploy job 3
  only:
    - curl return has 3

... repeat for job 4 to job 9, now skipped for simplicity

Is this idea possible?

1

1 Answers

1
votes

You can trigger pipelines using API. So, the idea would be to use the response of your api call to trigger the pipeline passing a variable "jobX". In each numbered job_, the only: allow the job exeuction if $TRIGERRED_JOB has the desired value. The except: keyword for select-targets avoid execution if pipeline is triggered with $TRIGERRED_JOB defined.

I tested this configuration successfully :

stages:
  - select
  - deploy

select-targets:
  stage: select
  script:
    - JOB_ID=$(shuf -i1-3 -n1)
    - echo "$JOB_ID"
    - curl -Ss --request POST --form token=${CI_JOB_TOKEN} --form ref=master --form "variables[TRIGERRED_JOB]=job$JOB_ID" "https://gitlab.com/api/v4/projects/${CI_PROJECT_ID}/trigger/pipeline"
  except:
    variables:
      - $TRIGERRED_JOB

job_1:
  stage: deploy
  script:
    - echo "job 1"
  only:
    variables:
      - $TRIGERRED_JOB == "job1"

job_2:
  stage: deploy
  script:
    - echo "job 2"
  only:
    variables:
      - $TRIGERRED_JOB == "job2"

job_3:
  stage: deploy
  script:
    - echo "job 3"
  only:
    variables:
      - $TRIGERRED_JOB == "job3"

Adapt the script of select-targets job to call correclty your endpoint.