1

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}/
sd1517
  • 607
  • 4
  • 7
  • 23
  • Could you please show the sanitized code of `exists.txt` – Sourav Nov 05 '20 at 14:45
  • @SouravAtta Good notice. Well, i ran this in a separate script file and it was working. Do you think it may be wrong? I have the same hunch. – sd1517 Nov 05 '20 at 14:52
  • Yes, may be. You can look into what is causing the problem or share the error if any. – Sourav Nov 05 '20 at 16:28
  • I think its not gonna work like this because stages get selected initially with rules whose variables or exists has value. In the absence if is taken as always to be true. So to say, the exists.txt will not exist when buildAndCreateApk is chosen to be staged or not. So it will always commutes to being chosen, irrespective of file is present or not. Stages gets chosen before jobs ran. – sd1517 Nov 05 '20 at 18:35

0 Answers0