Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reuse job in .gitlab-ci.yml

I currently have two jobs in my CI file which are nearly identical.

The first is for manually compiling a release build from any git branch.

deploy_internal:
  stage: deploy
  script: ....<deploy code>
  when: manual

The second is to be used by the scheduler to release a daily build from develop branch.

scheduled_deploy_internal:
  stage: deploy
  script: ....<deploy code from deploy_internal copy/pasted>
  only: 
      variables:
          - $MY_DEPLOY_INTERNAL != null

This feels wrong to have all that deploy code repeated in two places. It gets worse. There are also deploy_external, deploy_release, and scheduled variants.

My question: Is there a way that I can combine deploy_internal and scheduled_deploy_internal such that the manual/scheduled behaviour is retained (DRY basically)?

Alternatively: Is there is a better way that I should structure my jobs?

Edit:
Original title: Deploy job. Execute manually except when scheduled

like image 532
VaporwareWolf Avatar asked Apr 22 '20 05:04

VaporwareWolf


People also ask

Can GitLab runner run multiple jobs?

One way to allow more jobs to run simultaneously is to simply register more runners. Each installation of GitLab Runner can register multiple distinct runner instances. They operate independently of each other and don't all need to refer to the same coordinating server.

How long GitLab keep artifacts?

Pipeline artifacts from: The latest pipeline are kept forever. Pipelines superseded by a newer pipeline are deleted seven days after their creation date.

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.

Do GitLab jobs run in parallel?

When there are many team members waiting on a running pipeline to finish to be able to make a contribution to the project, the productivity of the team takes a hit. GitLab provides a method to make clones of a job and run them in parallel for faster execution using the parallel: keyword.


1 Answers

You can use YAML anchors and aliases to reuse the script.

deploy_internal:
  stage: deploy
  script:
    - &deployment_scripts |
      echo "Deployment Started"
      bash command 1
      bash command 2
  when: manual

scheduled_deploy_internal:
  stage: deploy
  script:
    - *deployment_scripts
  only:
    variables:
      - $MY_DEPLOY_INTERNAL != null

Or you can use extends keyword.

.deployment_script:
  script:
    - echo "Deployment started"
    - bash command 1
    - bash command 2

deploy_internal:
  extends: .deployment_script
  stage: deploy
  when: manual

scheduled_deploy_internal:
  extends: .deployment_script
  stage: deploy
  only:
    variables:
      - $MY_DEPLOY_INTERNAL != null
like image 58
Murli Prajapati Avatar answered Oct 17 '22 02:10

Murli Prajapati