Consice, Reusable and Refactored Definitions
30%
Including YAML Files: A Better Way to Define Pipelines
Let's say you have a long definition of a pipeline. This is a common scenario and can make the pipeline definition hard to read and maintain. Let's consider this example even though it is neither long nor complex but it will serve the purpose of this section:
stages:
- build
- test
- deploy
build:
stage: build
script:
- echo "Building the code..."
test:
stage: test
script:
- echo "Running tests..."
deploy:
stage: deploy
script:
- echo "Deploying the code..."
notify:
stage: notify
script:
- echo "Notifying the team..."
Another way to write the same pipeline is to split it into multiple files and include them in the main pipeline definition. Let's see how. Start by creating a new directory for our pipeline definitions:
mkdir -p $HOME/todo/app/.includes
Write the main pipeline definition:
cat <$HOME/todo/app/.gitlab-ci.yml
stages:
- build
- test
- deploy
- notify
include:
- local: '.includes/build.yml'
- local: '.includes/test.yml'
- local: '.includes/deploy.yml'
notify:
stage: notify
script:
- echo "Notifying the team..."
EOF
Create the first pipeline file called build.yml:
cat <$HOME/todo/app/.includes/build.yml
# build.yml
build:
stage: build
script:
- echo "Building the code..."
EOF
Create the second pipeline file called test.yml:
cat <$HOME/todo/app/.includes/test.yml
# test.yml
test:
stage: test
script:
- echo "Running tests..."Cloud Native CI/CD with GitLab
From Commit to Production ReadyEnroll now to unlock all content and receive all future updates for free.
