Trigger another job as a part of job in Gitlab CI Pipeline

Viewed 4476

I'm setting up a gitlab CI pipeline for my project. It has 3 stages - build, build-async and build-all and deployment_mode "dev" as of now. In build stage, creation of folders, deployment zips will take place. In build-async, all the async stuff like copying kits to aws s3 bucket will occur and build-all must essentially consist of build plus build-async stage. Assume that stage and deployment_mode environment variables have been setup in the gitlab environment variables. Here's sample snippet -

stages:   
  -build    
  -build-async  
  -build-all
  
dev-build:  
  image: python:3.7.4-alpine3.9  
  script:   
    - echo "Hello from dev-build. "  
  stage: build  
  tags:   
    - docker  
    - linux  
  only:  
    variables:  
        - $stage =~ /^build$/ &&  $deployment_mode =~ /^dev$/  

dev-build-async:  
  image: python:3.7.4-alpine3.9  
  script:   
    - echo "Hello from dev-build-async. "  
  stage: build-async  
  tags:   
    - docker  
    - linux  
  only:  
    variables:  
        - $stage =~ /^build-async$/ &&  $deployment_mode =~ /^dev$/  

dev-build-all:  
  image: python:3.7.4-alpine3.9  
  script:   
    - echo "Hello from dev-build-all. "  
  stage: build-all  
  tags:   
    - docker  
    - linux  
  needs: ["dev-build", "dev-build-async"]  
  only:  
    variables:  
        - $stage =~ /^build-all$/ &&  $deployment_mode =~ /^dev$/  

I'm not able to trigger the jobs dev-build and dev-build-async as a part of dev-build-all. Does anyone have idea how to trigger them both?

In this case the output expected when I provide stage as build-all and deployment-mode as dev is

Hello from dev-build. 
Hello from dev-build-async. 
Hello from dev-build-all. 
1 Answers

dev-build-all is on the third stage, means it's executed this way:

dev-build -> dev-build-async -> dev-build-all

needs: in that job means that it only runs after both jobs are successful. In this case, this is by default and needs: is not needed unless you want artifacts.

In order to trigger dev-build and dev-build-async from dev-build-all, you should place both jobs in the stages after the third. Optionally, use needs: in both of them. There's no way to call the previous stage.

Example:

stages:   
  - build-all
  - build    
  - build-async  

dev-build-all:  
  image: python:3.7.4-alpine3.9  
  script:   
    - echo "Hello from dev-build-all. "  
  stage: build-all  
  tags:   
    - docker  
    - linux  
  only:  
    variables:  
        - $stage =~ /^build-all$/ &&  $deployment_mode =~ /^dev$/  

dev-build:  
  image: python:3.7.4-alpine3.9  
  script:   
    - echo "Hello from dev-build. "  
  stage: build  
  tags:   
    - docker  
    - linux
  needs:
    - job: dev-build-all
      artifacts: false
  only:  
    variables:  
      - $stage =~ /^build$/ &&  $deployment_mode =~ /^dev$/  

dev-build-async:  
  image: python:3.7.4-alpine3.9  
  script:   
    - echo "Hello from dev-build-async. "  
  stage: build-async  
  tags:   
    - docker  
    - linux  
  needs:
    - job: dev-build
      artifacts: true
  only:  
    variables:  
        - $stage =~ /^build-async$/ &&  $deployment_mode =~ /^dev$/  
Related