49

I have an unity ci-project. .gitlab-ci.yml contains base .build job with one script command. Also I have multiple specified jobs for build each platform which extended base .build. I want to execute some platform-specific commands for android, so I have created separated job generate-android-apk. But if it's failing the pipeline will be failed too.(I know about allow_failure). Is it possible to extend script section between jobs without copy-pasting?

derHugo
  • 83,094
  • 9
  • 75
  • 115
orion_tvv
  • 1,681
  • 4
  • 17
  • 29

1 Answers1

72

UPDATE:

since gitlab 13.9 it is possible to use !reference tags from other jobs or "templates" (which are commented jobs - using dot as prefix)

actual_job:
  script:
    - echo doing something

.template_job:
  after_script:
    - echo done with something

job_using_references_from_other_jobs:
  script:
    - !reference [actual_job, script]
  after_script:
    - !reference [.template_job, after_script]

Thanks to @amine-zaine for the update


FIRST APPROACH:

You can achieve modular script sections by utilizing 'literal blocks' (using |) like so:

.template1: &template1 |
  echo install

.template2: &template2 |
  echo bundle

testJob:
  script:
  - *template1
  - *template2

See Source


ANOTHER SOLUTION:

Since GitLab 11.3 it is possible to use extend which could also work for you.

.template:
  script: echo test template
  stage: testStage
  only:
    refs:
      - branches

rspec:
  extends: .template1
  after_script:
    - echo test job
  only:
    variables:
      - $TestVar

See Docs More Examples

Soroosh Khodami
  • 1,185
  • 9
  • 17
fuma
  • 5,067
  • 4
  • 35
  • 39
  • 1
    The result of the second example will be `echo test`. To achieve the same result as the first example will require adding a `before_script` or `after_script`. – Luke Vincent Aug 20 '19 at 12:10
  • @LukeVincent You are right here, It's not equivalent. The script of the template will not be edited but probably overwritten. I'll edit the answer. – fuma Sep 02 '19 at 10:25
  • may i ask if extended template name must start with .(dot)? didn't see any explaination in docs. – Lei Yang Dec 18 '19 at 03:22
  • 1
    @LeiYang a "job" that is prefixed with a dot is in fact a hidden-key (kind of commented out) and will not be executed in a pipeline. See: https://docs.gitlab.com/ee/ci/yaml/#hidden-keys-jobs You don't want to run the template itself, but the job that extends the template. – fuma Dec 19 '19 at 11:30
  • 1
    Since gitlab 13.9, you can use !reference instead of YAML anchors. More: https://docs.gitlab.com/ee/ci/yaml/README.html#reference-tags – Amine Zaine Mar 31 '21 at 11:04
  • 1
    Regarding YAML-anchors, there is now a documentation section available here https://docs.gitlab.com/ee/ci/yaml/#anchors – lony Aug 19 '21 at 07:37
  • It is important to know any error in the `after_script` does not fail the job, but is ignored. This makes it unsuitable to put part of the job script in. – Ferdy Pruis Nov 04 '21 at 08:05
  • 4
    Upvoted. Can't edit due to minimum length requirements, but according to docs and my own experiment, the reference should include the leading period, this way: `- !reference [.template_job, after_script]` – autonomy Jul 25 '22 at 20:05