GitLab CI/CD - Hands-On Lab: Investigating Broken Pipelines
Objectives
- Syntax error catching
- Cases where no jobs run due to rules/workflows
In the next section, we are going to take a look at deploying an application using an SSH key. To start this process, we will need to add an SSH key as a variable to our pipeline. This process can sometimes cause errors due to the formatting of the SSH key. To help with implementing this process correctly, as well as learning how to troubleshoot, let’s take a look at a common error.
Task A. Setup the SSH Connection
As a part of this course, you were provided with an SSH key to use for deployments. You will need to add this SSH key to GitLab to use it during your CI/CD process. To do this:
-
Navigate to your project in GitLab.
-
In the left sidebar, go to Settings > CI/CD.
-
Select Expand next to Variables.
-
In Group variables (inherited), you will see a variable named
SSH_PRIVATE_KEY
To demonstrate a common SSH related error, we will copy the SSH key and create a new variable based on its value:
-
Select the group next to the
SSH_PRIVATE_KEY
variable. -
Select Expand next to Variables.
-
Select the Copy icon next to the value of the
SSH_PRIVATE_KEY
variable . -
Select Add variable.
-
Set the Type to file.
-
In the
key
field, enterSSH_INVALID_KEY
. -
In the value, paste your SSH key.
-
Delete the new line at the end of the key value. Doing this will create an error when we try to use the key.
-
Select Add variable.
Your new SSH key variable will now be accessible during any CI/CD jobs you run in your group. Now, let’s create a job to test the SSH connection.
-
Navigate to your CI/CD project.
-
Select your
.gitlab-ci.yml
file. -
Select Edit > Edit in pipeline editor.
-
Add a new stage named deploy:
stages: - test - build - run - release - deploy
-
For this stage, we will have a single job named deploy app.The first thing this job will do is check if there is an SSH agent available, and install one if not.
deploy app: stage: deploy script: - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client git -y )' - eval $(ssh-agent -s)
-
Next, we will setup a pem file from our SSH key variable, setting the required permissions on the file so it can be used for SSH purposes. Note that we are using the
SSH_INVALID_KEY
variable.deploy app: stage: deploy script: - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client git -y )' - eval $(ssh-agent -s) - chmod 400 "$SSH_INVALID_KEY" - ssh-add "$SSH_INVALID_KEY" - mkdir -p ~/.ssh - chmod 700 ~/.ssh
-
Finally, we can add a simple SSH command to test if the connection is working.
deploy app:
stage: deploy
script:
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client git -y )'
- eval $(ssh-agent -s)
- chmod 400 "$SSH_INVALID_KEY"
- ssh-add "$SSH_INVALID_KEY"
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- ssh-keyscan -t rsa,ed25519 $ip >> ~/.ssh/known_hosts
- ssh root@$ip 'ls /'
When you commit these changes, you will see an error in your deploy job. To view this error, navigate to the Build > Pipelines page and view the failed pipeline and job.
$ eval $(ssh-agent -s)
Agent pid 3211
$ chmod 400 "$SSH_PRIVATE_KEY"
$ ssh-add "$SSH_PRIVATE_KEY"
Error loading key "/builds/scottcosentinogitlab/cicd_lab_rewrite.tmp/SSH_PRIVATE_KEY": error in libcrypto
Let’s try to figure out what happened!
Task C.1. Isolate the command that causes the error
The first logical step is to isolate the command that is causing the error. We can see in the logs that the ssh-add "$SSH_INVALID_KEY"
command looks to cause the error.
With the command isolated, we can consider some ways to verify the command. One option would be to try running the command locally if possible. This can help rule out potential issues with the runner. For this example, we can assume the runners are working correctly, meaning the issue lies in the actual command itself.
In these cases, often the variable/input of the command is the main source of the error. From the error message, it looks that the key is not formatted correctly. Let’s consult the documentation to see why.
Task C.2. Search the Documentation
Often, common errors will be present in our documentation with solutions to the problems. To find this error:
-
Try searching a part of it in the documentation: error in libcrypto. The first result you get is an article titled: Using SSH keys with GitLab CI/CD.
-
If you scroll to the Troubleshooting section of this page, you will see a section on the exact error we are facing.
The documentation tells us that the issue is not having a new line at the end of the key. Let’s try adding one and see if it fixes the problem.
-
Return to your variables by selecting Settings > CI/CD > Variables.
-
Select the group next to the
SSH_INVALID_KEY
variable. -
Expand the group variable section and select the Edit icon next to the
SSH_INVALID_KEY
variable. -
Add a new line to the end of the variable value, then select Save Changes.
To test if this fixes the error:
-
Navigate back to your CI/CD project.
-
Select Build > Pipelines from the left sidebar.
-
Select Run pipeline.
-
Leave all values as default and select Run pipeline again. You will now see the job complete successfully!
Lab Guide Complete
You have completed this lab exercise. You can view the other lab guides for this course.
Suggestions?
If you wish to make a change to the Hands-On Guide for GitLab CI/CD, please submit your changes via Merge Request!
b559de89
)