GitLab CI - Use specific keyword in jobs

Viewed 124

I have a problem with GitLab CI (maybe I try to use it in the wrong way). I have templates with artifacts and jobs that are used templates with artifacts but the value expire_in should be used only on a specific branch e.g.

template:

.build_template: &artifactsTMP
    artifacts:
        paths:
            - test.py
        expire_in: 3 minutes

jobs

firstJob:
  stage: build
  <<: *artifactsTMP
  script:
      - echo "sth"
  rules:
    - If: $CI_COMMIT_BRANCH == "dev"

secondJob:
  stage: build
  <<: *artifactsTMP
  script:
      - echo "sth"

What I want to do is that expire_in should be run only on branch "dev". I tried to use the IF statement inside artifacts and rules but it was not working. Is there any option to add the expire_in during job execution?

For example:

template:

.build_template: &artifactsTMP
    artifacts:
        paths:
            - test.py

jobs:

firstJob:
  stage: build
  <<: *artifactsTMP
  script:
      - echo "sth"
  rules:
    - If: $CI_COMMIT_BRANCH == "dev"

secondJob:
  stage: build
  <<: *artifactsTMP
  >>ADD HERE expire_in: 5 minutes<<
  script:
      - echo "sth"

Is there any change to do that or just simply I should create two templates, one without expire_in second with expire_in

1 Answers

One way of doing this could be variablizing the expire_in value and pass it accordingly for your respective branches like below,

template:

.build_template: &artifactsTMP
    artifacts:
        paths:
            - test.py
        expire_in: $EXPIRY_TIME

and you can create 2 jobs for dev and non dev, where the EXPIRY_TIME value can be passed as a variable,


firstJobDev:
  stage: build
  <<: *artifactsTMP
  variables:
     EXPIRY_TIME: 0 minutes
  script:
      - echo "sth"
  only:
    - yourBranchName

Create similar job as shown above for non dev branches

Related