Learning Outcomes
- define continuous integration (CI)
- define continuous delivery (CD)
- define continuous deployment(CD)
- distinguish between development, testing (or staging), and production environments
- use webhooks to deploy a simple web application
Resources
- Continuous Integration & Continuous Deployment: CI/CD (video)
- What is Continuous Integration
- slides
- narated slides (video)
Lab
Video walkthrough of this lab.
Set up a SSH Key for GitLab
Start the Google Cloud Shell.
Generate a SSH key:
Display the public key:
Copy the output into your clipboard. Log in to GitLab. Go to your settings and then to the “SSH Keys” page. Paste your public key into the “Key” field. Click on the “Add key” button.
Create a Deployment Script for an Application
Create myapp/deploy.sh
with the following contents:
Make sure myapp/deploy.sh
has the execution bit set:
Create a View for the Webhook
Create myapp/views/webhook.ejs
with the following contents:
This page will never be seen, but Gitlab still needs a valid response to know that the webhook succeeded.
Create a Route for the Webhook
Create myapp/routes/webhook.js
with the following contents:
Link the Webhook Route in the application
Require the webhook route in myapp/app.js
:
Link the webhook route to the webhook path:
Create a .gitignore File
Create myapp/.gitignore
with the following contents:
Set Up a GitLab Project
In GitLab, go to “Projects | Your projects”. Click on the “New project” button. Choose “Create blank project”. For “Project name”, enter “myapp”. Click on the “Create project” button. Follow the instructions to “Push an existing folder”.
Go to the project settings page. Go to the “Webhooks” page. For the URL, enter http://NNN.NNN.NNN.NNN:8080/webhook
. For “Secret Token”, enter a random sequence of letters and numbers. For the list of triggers, make sure only “Push events” is checked. Make sure “Enable SSL verification” is unchecked.
Go to your GitLab profile settings page. Go to the “Access Tokens” page. For “Name”, enter “deploy”. For “Scopes”, make sure “read_api” and “read_repository” are checked. Click on the “Create personal access token” button. Record the generated token value. THIS IS THE ONLY TIME YOU CAN SEE IT.
Deploy Application by Hand to Bootstrap
SSH into the virtual machine:
Install wget:
Make a note of project ID for your GitLab project. (It is located just below the project name on the project overview page). Retrieve the latest project archive (fill in your actual values for YOUR_PROJECT_ID
and YOUR_ACCESS_TOKEN
):
Unpack the archive:
We are going to need to re-start the application with the correct environment variables set so we will stop and delete the current application:
Copy the latest version of the application into the myapp
directory:
Remove the archive and the unpacked directory:
Change to application bin
directory:
Re-start the application with the correct environment variables (substituting your actual values for YOUR_ACCESS_TOKEN
and YOUR_WEBHOOK_SECRET
):
View the streaming logs for the application:
Verify that the Webhook Works
In a new tab, browse to the application using the external IP address for the virtual machine (http://NNN.NNN.NNN.NNN:8080
).
Back in the Google Cloud Shell environment, change the title
property in myapp\routes\index.js
. Stage, commit, and push the changes to the repository. Verify that the change has be automatically deployed to http://NNN.NNN.NNN.NNN:8080
by refreshing the page.
Assignment
- Add a new route to the application,
/guestbook
. - In the router, create a global array variable that maintains the list of vistors.
- For a
GET
request, display the contents of the array, one per line. Also, display a form where the user can enter their name and submit the form (as aPOST
request). - For a
POST
request, add the new name to the array and re-display the array. - Stage, commit, and push your changes.
- Confirm that the changes are properly deployed to the virtual machine.