Search code examples
gitlabgitlab-ci

Gitlab extends stage's script does not trigger


I am currently practicing using GitLab's CD/CI pipeline. To make my pipeline more efficient, I have created nested jobs and tied them to stages using the "extends" keyword. Each of my individual jobs has its own script that I have carefully crafted to execute specific tasks. However, when I run the pipeline, I am unable to see any output from the jobs' scripts. I have double-checked my code and pipeline settings, but I am still unsure what could be causing this issue. Can you please suggest any potential solutions or troubleshooting steps that I could try?

stages:
  - test
  - build-publish
  - qa_release
  - prod_release

.deploy-job:
  script:
    - echo "'Deploy to Environment'"
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: never
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: manual


.download-signin-keystore:
  image: docker:latest
  before_script:
    - apk add --update curl && rm -rf /var/cache/apk/*
    - apk add bash
  variables:
    SECURE_FILES_DOWNLOAD_PATH: ${CI_PROJECT_DIR}/android/app
  script:
    - curl --silent "https://gitlab.com/gitlab-org/incubation-engineering/mobile-devops/download-secure-files/-/raw/main/installer" | bash
    - ls -lah ${CI_PROJECT_DIR}/android/app ##I can't see the output
  after_script:
     - rm -f ${CI_PROJECT_DIR}/android/app/signing_keystore_password.keystore

.gradle-job:
  variables:
    GRADLE_OPTS: '-Dorg.gradle.daemon=false'

.build-publish:android:
  stage: build-publish
  extends:
    - .gradle-job
    - .download-signin-keystore
  script:
    - echo "Build env $SOK_BUILD_ENV" #I can't see the output
    - echo "hello wolrd" #I can't see the output
   
# stages
test:
  stage: test
  script:
    - echo "I can see output"
  

build-publish:android_qa:
  stage: qa_release
  extends:
    - .build-publish:android
    - .deploy-job
  variables:
    SECURE_FILES_DOWNLOAD_PATH: ${CI_PROJECT_DIR}/android/app
    SOK_BUILD_ENV: 'qa'
  script:
    - echo "android_qa"  #I can see the output
  needs:
    - test
  environment:
      name: qa/android
      deployment_tier: staging

build-publish:android_prod:
  stage: prod_release
  variables:
    SOK_BUILD_ENV: 'prod'
  extends:
    - .build-publish:android
    - .deploy-job
  script:
    - echo "prod" #I can see the output
  needs:
    - test
  environment:
      name: prod/android
      deployment_tier: staging

Solution

  • Unfortunately, this is not how the extends: feature works. extends: only merges/overrides keys together. When you have two items in extends: and both have a script: (or other nonhash value) only one of them will be present in the final configuration.

    Additionally, if you also add a script: section after extending, you overwrite the script: section entirely of extended jobs altogether. This is also similar for other keys besides script:.

    .foo:
      script:
        - echo 'foo'
    
    .bar:
      script:
        - echo 'bar'
    
    example1:
      extends:
        - .foo
        - .bar  # only .bar `script` will take effect
    
    example2:
      extends:
        - .foo
        - .bar
      script: # none of the extended jobs will have any practical effect
        - echo "example" # this overrides both extended `script` sections
    

    To get the effect you want, you should use !reference to include additional scripts within your job.

    
    .ensure_curl:
      - which curl || apk update -y && apk add curl
    
    .download_keys:
      - !reference [.ensure_curl]  # you can nest !reference if needed
      - curl --silent "https://gitlab.com/gitlab-org/incubation-engineering/mobile-devops/download-secure-files/-/raw/main/installer" | bash
      - ls -lah ${CI_PROJECT_DIR}/android/app
    
    myjob:
      script:
        - !reference [.download_keys]
        - echo "myjob script"