Gitlab CI/CD: conditionally run downstream multistage pipeline from upstream pipeline

1k Views Asked by At

I am trying to trigger a downstream pipeline only when the artifact in the upstream pipeline is not available. I am using Gitlab CE v13.5.1.

The idea is:

  1. Prebuild stage: curl from nexus and check if the artifact is available. If yes, do not run the downstream trigger otherwise run it.

    • to achieve this, when curl fails, I am creating a dummy exists.txt file which is then used in exists condition in later stages.
  2. Run trigger only when exists.txt is present. But, problem is the trigger runs all the time irrespective of the exists.txt is present or not. So as to say, the exists condition in the trigger stage is not functioning.

Following is the gitlab-ci file:

image: ${NEXUS_REPO_DOCKER_URL}/<<image>>

  variables:
    CUSTOMER_NAME: "ABC"
    CONFIG_VERSION: "1.2.7"
    PICKER_TEMPLATE_TAG: "6.9.15"

  stages:
    - prebuild
    - trigger
    - build
    - release

  checkArtifactAvailability:
     stage: prebuild
     only:
      - master
     tags:
      - docker
      -  lxc
    script:
      - set +e
      - curl -s -S -f $NEXUS_RAW_PICKER_REPOSITORY/${PRODUCT_FLAVOR}/${PRODUCT_FLAVOR}-${BUILD_TYPE}v${PICKER_TEMPLATE_TAG}.apk --output ${PRODUCT_FLAVOR}-${BUILD_TYPE}v${PICKER_TEMPLATE_TAG}.apk
      - "if [ '$?' -gt '0' ]; then touch exists.txt; fi"
      -set -e
    artifacts:
      expire_in: 1 hour
      paths:
       - ${PRODUCT_FLAVOR}-${BUILD_TYPE}v${PICKER_TEMPLATE_TAG}.apk
       - exists.txt

  createApk:
    stage: trigger
    rules:
      - exists:
          - exists.txt
    trigger:
      project: dev/<<project_name>>
      strategy: depend
    when: on_failure
  
  buildZip:
    stage: build
    tags:
      - docker
      - lxc
    rules:
      - exists:
          - exists.txt
    script:
        - curl -s -S -f $NEXUS_RAW_PICKER_REPOSITORY/${PRODUCT_FLAVOR}/${PRODUCT_FLAVOR}-${BUILD_TYPE}v${PICKER_TEMPLATE_TAG}.apk --output ${PRODUCT_FLAVOR}-${BUILD_TYPE}v${PICKER_TEMPLATE_TAG}.apk
        - zip -r v${CONFIG_VERSION}.zip <<files>> ${PRODUCT_FLAVOR}-${BUILD_TYPE}v${PICKER_TEMPLATE_TAG}.apk
    artifacts:
      expire_in: 1 hour
      paths:
          - v${CONFIG_VERSION}.zip

  releaseToNexus:
    stage: release
    tags:
        - docker
        - lxc
    rules:
      - exists:
          - exists.txt
    needs:
     - job: buildZip
       artifacts: true
    script:
        - curl -u $NEXUS_USER:$NEXUS_PASSWORD --upload-file ./v${CONFIG_VERSION}.zip $NEXUS_RAW_QA_REPOSITORY/Picker/${CUSTOMER_NAME}/
0

There are 0 best solutions below