To continue moving toward implementing a CI pipeline. You will now create the file required by GitLab to configure the pipeline.
In your VS Code Terminal, use the code-server
keyword to open a YAML file called .gitlab-ci.yml
.
GitLab looks for this particular hidden file in a repo to leverage its CI/CD pipeline feature.
code-server -r /home/user21/ciscolive/LTROPS-2711/.gitlab-ci.yml
This hidden file defines the pipeline and the stages to run. Your pipeline for this lab will have four stages defined: lint, pre-tests, deploy, and post-tests.
yamllint
to check your repo for correct YAML formatting, and ansible-playbook --syntax-check
to validate
the syntax and structure of your previously created playbooks.
As you populate and review the pipeline below, make note of the only
and except
keys.
These dictate which stages are run against which branch or not run against a branch.
As done in this lab, typically it is the main branch that is used with these keys. For example, when you want the pipeline to run against your staging setup, then you will notice that except
is used so that a particular step is
not run on main. On the contrary there are steps only applicable for prod, thus the only
key is used to specify main.
The last two pieces to this pipeline is the usage of the only
key to also control when the pipeline is run. For example, you could have the pipeline run against staging every single time a commit is pushed to the repo. Depending on your devops process,
that may be required, but for this lab, only
will be used to control the pipeline running when a merge request is created. A merge request will be expanded on in the next section, but in short, this is the process of merging your code from another branch,
such as your staging branch in this case, into main. The other piece is the script
key that defines a list of the commands to execute to run your automation. In this case, you will be using the cxtm-mgr client to instruct CXTM to run the system and routing batches
that you created earlier in the lab.
.gitlab-ci.yml
file in VS Code:
.gitlab-ci.yml
file in VS Code:
---
image: cait:25.2
variables:
ANSIBLE_HOST_KEY_CHECKING: 'false'
ANSIBLE_FORCE_COLOR: 'true'
ANSIBLE_PERSISTENT_COMMAND_TIMEOUT: 100
ANSIBLE_PERSISTENT_CONNECT_TIMEOUT: 100
CXTM:
description: "Cisco CXTM IP or FQDN"
TM2_API_KEY:
description: "Cisco CXTM API Key"
stages:
- lint
- pre-tests
- deploy
- post-tests
yamllint:
except:
- main
only:
- merge_requests
stage: lint
script:
- echo "Checking YAML files"
- yamllint -d relaxed .
ansible-syntax-check:
except:
- main
only:
- merge_requests
stage: lint
script:
- echo "Checking Ansible playbook"
- ansible-playbook --syntax-check -i ansible/hosts.yml ansible/playbook.yml
baseline-pre-tests:
except:
- main
only:
- merge_requests
stage: pre-tests
script:
- echo "Tests before config changes"
- cxtm-mgr batch --host $CXTM --insecure run --output tests/cxtm.log.xml <Batch ID 1>
artifacts:
paths:
- tests/cxtm.log.xml
reports:
junit:
- tests/cxtm.log.xml
deploy:
except:
- main
only:
- merge_requests
stage: deploy
script:
- echo "Deploy config changes to test network"
- ansible-playbook -i ansible/hosts.yml ansible/playbook.yml
post-tests:
except:
- main
only:
- merge_requests
stage: post-tests
script:
- echo "Tests after config changes"
- cxtm-mgr batch --host $CXTM --insecure run --output tests/cxtm.log.xml <Batch ID 1> <Batch ID 2>
artifacts:
paths:
- tests/cxtm.log.xml
reports:
junit:
- tests/cxtm.log.xml
In your .gitlab-ci.yml file in VS Code:
.gitlab-ci.yml
with the above connection information, save your .gitlab-ci.yml
file using Ctrl+s on the Windows keyboard or by clicking File then Save.
Be sure to save your file! Not saving will result in your code not executing.
After initializing your Git repo and creating the files specific to your prod fabric, it is time to add these files with Git in preparation for committing them to the repo.
You perform the action of adding files with git add
. You will add your .gitlab-ci.yml
file and add your ansible
directory.
git add .gitlab-ci.yml
git add ansible/*
With your directories and files added, you can now commit them to your Git repo with git commit
.
The -m
option is for a comment/message for the commit.
git commit -m "Initial config change testing commit"
Finally, with your project directory initialized as a Git repo, your files added and committed, you can push everything to your Git repo on the GitLab instance.
git push -u origin config-changes-testing
Continue to the next section to trigger the pipeline and review the results.