Playing around with .gitlab-ci.yml I found few nice things I'd like to share. Let's start with an example:
.frontend_template: &frontend_template
stage: deploy
script:
- npm install netlify-cli -g
- ember deploy $ENVIRONMENT --verbose
- netlify deploy -e $ENVIRONMENT -p ./dist -s $NETLIFY_SITE_ID -t $NETLIFY_KEY
.frontend_variables: &frontend_variables
API_SERVER: "http://${ENVIRONMENT}-api.example.com"
NETLIFY_SITE_ID: "${ENVIRONMENT}-example-com"
development_frontend:
<<: *frontend_template
variables:
ENVIRONMENT: "development"
<<: *frontend_variables
only:
- master
production_frontend:
<<: *frontend_template
variables:
ENVIRONMENT: "production"
<<: *frontend_variables
only:
- production
There's quite a lot going on, so let's try to decompose it piece by piece:
- Each job definition that starts with a dot is ignored. So
.frontend_templateand.frontend_variablesdo not define active, runnable jobs. This feature is called hidden keys. -
YAML allows for referencing other parts of itself to prevent copy&pasting. The feature is called anchors. Simply speaking:
-
&frontend_templateis copy -
<<: *frontend_templateis paste
-
- It might seem weird to have separate
frontend_templateandfrontend_variables, but YAML does not allow complicated merging when using anchors, so thatvariableskey cannot be simply put intofrontend_template, because it would be overwritten with the key of the same name atdevelopment_frontend/production_frontendlevel. - It is important to import
frontend_variablesafter the definition ofENVIRONMENT, because then we can use that variable inAPI_SERVER/NETLIFY_SITE_ID.
Bonus: If you ever wonder how does your YAML look like when expanded, you can simply convert your source to something that does not support anchors. Like JSON for example. Try it with the example above.
Top comments (3)
There is an other gitlab-ci keywords that can help us such:
Good point. I should've mention that. IIRC I did not chose that one because at the time of writting the article include and/or extends were restricted only to paid versions of GitLab.
I already knew about the anchors, but here's a ❤️ for the hint with viewing the expanded yaml as JSON.