Reputation: 10380
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
Upvotes: 23
Views: 23285
Reputation: 13
You can use a hidden job in template file that you can extend after that in multiple jobs. (This solution was already mentioned in this thread)
#template.yml
.wget-job:
stage: download
image: alpine/wget
variables:
URL_TO_GET: ""
WGET_OPTS: ""
script:
- |
#! /bin/sh
wget $WGET_OPTS $URL_TO_GET
#.gitlab-ci.yml
include:
file:
- template.yml
stages:
- download
download-1:
extends: .wget-job
variables:
URL_TO_GET: "https://fake.url.com/file"
download-2:
extends: .wget-job
variables:
URL_TO_GET: "https://fake.url.com/file2"
Upvotes: 0
Reputation: 9473
Use GitLab's default
section containing a before_script
:
default:
before_script:
- ....<deploy code>
job1:
stage: deploy
script: ....<code after than deploy>
job2:
stage: deploy
script: ....<code after than deploy>
Note: the
default
section fails to function as such if you try to execute a job locally with thegitlab-runner exec
command - use YAML anchors instead.
Upvotes: 3
Reputation: 9713
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
Upvotes: 40