Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 985
orion_tvv Avatar asked Nov 06 '18 15:11

orion_tvv


People also ask

What is extends in GitLab CI?

Here is the definition from GitLab documentation: extends defines entry names that a job that uses extends inherits from. It's an alternative to using YAML anchors and is a little more flexible and readable.

What is Before_script in GitLab CI?

These are scripts that you choose to be run before the job is executed or after the job is executed. These can also be defined at the top level of the YAML file (where jobs are defined) and they'll apply to all jobs in the . gitlab-ci. yml file.

How many ways you can structure your pipeline?

There are three main ways to structure your pipelines, each with their own advantages. These methods can be mixed and matched if needed: Basic: Good for straightforward projects where all the configuration is in one easy to find place.

How do you run a script from file in another project using include in GitLab CI?

If you have access project A and B, you can use multi-project pipelines. You trigger a pipeline in project A from project B. In project A, you clone project B and run your script.


1 Answers

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

like image 61
fuma Avatar answered Sep 28 '22 03:09

fuma