how to add a "needs" based on a condition

Viewed 176

Is there a way to add a need in order for a job to run? Or is there a specific way to add a condition if you're on a specific branch, please add this job as a dependency? So far I have a "need"s within my rules section. Review job only runs on feature branches. There is a similar question being asked.

test:sauce:
  ...
  script:
    - export MASTER_URL=https://masterurlexample.io
    - export TEST_PREVIEW_APP=$CI_COMMIT_REF_SLUG
    - cd $MAVEN_DIRECTORY
    - if [ "$CI_COMMIT_BRANCH" == "master" || "$EMULATE_BRANCH" == "master" ]; then
        export TEST_PREVIEW_APP=$MASTER_URL;
        needs:
      fi;
    - echo "Testing on $TEST_PREVIEW_APP"
    - echo "starting test"
    - sleep 30
    - mvn -U $MAVEN_CLI_OPTS ...
  rules:
    - if: "$CI_COMMIT_BRANCH" != "master"
      needs: [ "review "]
1 Answers

I think is not possible.

A workaround could be make a template job (starting the name with .) cointaining your code and extends the job two time, one time for the case "$CI_COMMIT_BRANCH" != "master" with needs: [ "review "] and another time with - if: "$CI_COMMIT_BRANCH" == "master" without the needs section.

EDIT: I try to explain myself better, my idea is to use an hidden job like

.hidden_job:
  script:
    - YOUR CODE HERE

And after that extended this job into others with the right needs setted, like:

job1:
  extends: .hidden_job
  rules:
    - if: "$CI_COMMIT_BRANCH" != "master"
    needs: [ "review "]


job2:
  extends: .hidden_job
  rules:
    - if: "$CI_COMMIT_BRANCH" == "master"

In this way you can define multiple job with different rules and needs for satisfy your requirements without repeating your code everytime. (E.G. use a specific needs only on a specific branch, like job1)

Related