continuous-integrationgitlabyamlgitlab-ci

Gitlab-ci: extend script section


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?


Solution

  • 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