Sequence of jobs in one stage

Hi Guys
I have a question about GitLab CI/CD. In the deploy stage of my code, I have multiple jobs and complex changes and conditions, so when you change some things in code some jobs tigers and relate them with needs. Generally, every job is manual but I want to avoid manually starting each job and I want to just depend on the first job, because of changes and conditions First jobs dynamically change, so I can’t use the on_success condition. Does anyone have any idea?

Hi @mm3906078

Maybe you need to better explain what you mean by “because of changes and conditions First jobs dynamically change”. If the Job name stays the same, you can always use it with needs as dependency.

Thanks @balonik for you’re responce. Pelese check this example.

.rules_template:
  - &staging_role1_ansible
    changes:
      - ansible/roles/role1/**/*

  - &staging_role2_ansible
    changes:
      - ansible/roles/role1/**/*
      - ansible/roles/role2/**/*

  - &staging_role3_ansible
    changes:
      - ansible/roles/role2/**/*
      - ansible/roles/role3/**/*

stages:
  - deploy

role1_staging:
  stage: deploy
  image: kroniak/ssh-client
  rules:
    - <<: *staging_role1_ansible
      when: on_success
  script:
    - echo "role1_staging"

role2_staging:
  stage: deploy
  image: kroniak/ssh-client
  rules:
    - <<: *staging_role2_ansible
      when: on_success
  script:
    - echo "role2_staging"
  needs:
    - job: role1_staging
      optional: true

role3_staging:
  stage: deploy
  image: kroniak/ssh-client
  rules:
    - <<: *staging_role3_ansible
      when: on_success
  script:
    - echo "role3_staging"
  needs:
    - job: role1_staging
      optional: true
    - job: role2_staging
      optional: true

In this example, if I change something in role2 folder jobs, role3_staging & role2_staging will be triggered and because both of them have when: on_success jobs will be automatically run.

My desired state in this case is role2_staging run manually and then role3_staging run automatically.

If I change role2_staging to when: manual, the issue is solved, but if I change the role1 folder, all jobs will be triggered and job role1_staging automatically runs and role2_staging job will be manual.

But my desired state in this case is role1_staging, have manually start and other jobs goes atoumaticly.

So, in general, the first job should always be manual and others should be automatic.

something like this (just so I understand it):

change in role1: role1_staging manual, role2_staging automatic, role3_staging not present
change in role2: role1_staging not present, role2_staging manual, role3_staging automatic
change in role3: role1_staging not present, role2_staging not present, role3_staging manual

That’s interesting use-case.

I’d go for something like this. I have removed the YAML anchors so it’s easier to understand, you can reduce the code later if you need to. I have also removed the when: on_success since that’s the default.

stages:
  - deploy

role1_staging:
  stage: deploy
  image: kroniak/ssh-client
  rules:
    - changes:
      - ansible/roles/role1/**/*
      when: manual
  script:
    - echo "role1_staging"

role2_staging:
  stage: deploy
  image: kroniak/ssh-client
  rules:
    - changes:                          # if change is done to role1, this is a 2nd job in the other, so it's automatic
      - ansible/roles/role1/**/*
    - changes:                          # if change is done to role2, this job is manual, because it's the first job
      - ansible/roles/role2/**/*
      when: manual
  script:
    - echo "role2_staging"
  needs:
    - job: role1_staging
      optional: true

role3_staging:
  stage: deploy
  image: kroniak/ssh-client
  rules:
    - changes:                          # if change is done to role2, this is a 2nd job in the other, so it's automatic
      - ansible/roles/role2/**/*
    - changes:                          # if change is done to role3, this job is manual, because it's the first job
      - ansible/roles/role3/**/*
      when: manual
  script:
    - echo "role3_staging"
  needs:
    - job: role1_staging
      optional: true
    - job: role2_staging
      optional: true

Yes, you understand correctly.

I thought about adding when:manual to each condition. it’s hard for 20 jobs. It looks like I have no other choice.
About the use case, I do this because I am afraid of automatic deployment in prod so, i want to use manual option but not manually start a lot of them if there are trigerd.

Again, thanks for your response @balonik

(I have accidentally edited the wrong post, writing this again, but shorter)

Alternative, but a bit complicated one, is to use Dynamic Child pipelines. You create a script that based on changes creates a pipeline definition.

1 Like

This GitLab CI configuration is invalid: jobs:role3_staging rules should be an array containing hashes and arrays of hashes.

looks like multiple changes doesn’t work :((

Edit: no choice to use if multiple time.

No, I have a wrong copy/paste in the role3_staging job, there is rules twice.