In my previous years as an engineer, I always looked at Continuous Integration/Continuous Delivery (CI/CD) with a smug face. It has never been anything but a rough experience (thank you, Jenkins š¤¢). However, it is my firm belief that in 2019 we will be seeing an uptick in conversation about how to easily manage and deploy your apps; considering developing the app is only a single step, right?
For this article, I am going to assume that you have already created a Vue.js application and are now ready to set up with GitLabās continuous deployment.
Although this article shows how to deploy a Vue.js app, it is the basic structure for using GitLabās CI/CD to deploy anything to S3.
First things first, letās set up an AWS account and create a new S3 bucket! Any new AWS accounts go under their free tier which will allow us to deploy to S3 for free (for the first year, under certain request constraints).
S3:
Once signed up, head over to the S3 console and click āCreate Bucketā
Create bucketĀ modal
You will be shown this modal. Enter in a bucket name (remember this will be required to be unique across AWS). On the āSet Permissionsā tab, ensure you uncheck āBlock new public bucket policiesā and āBlock public and cross-account access if bucket has public policiesā:
After the bucket has been created, click into the bucket and click the second tab called āPropertiesā. You will see a card called āStatic website hostingā. Click that and enable āuse this bucket to host a websiteā and fill in index.html
for the index document as so:
Note: The endpoint at the top of the modal will be the URL you can use to access your website!
Now for the final part, allow read permissions to your S3 bucket (so your users can see your awesome website!). Navigate to the third tab that says āPermissionsā and then click āBucket Policyā. You will need to add the following policy in the editor:
{"Version": "2012-10-17","Statement": [{"Effect": "Allow","Principal": "*","Action": "s3:GetObject","Resource": "arn:aws:s3:::YOUR_BUCKET_NAME/*"}]}
Donāt forget to replace _YOUR_BUCKET_NAME_
with the bucket name that you used when creating. This allows any user to _GET_
from your bucket.
Head back over to the āPublic Access Settingsā tab and re-enable blocking new bucket policies:
This ensures the bucket policy cannot beĀ updated
Now if we go to the URL that Amazon assigned this bucket, you should see aā¦ 404! Thatās because we havenāt pushed your project from GitLab!
The final thing we must do in the AWS dashboard is to create an IAM user so that we can safely allow GitLab to access and upload data to this bucket. This allows us to revoke access if we ever need to.
IAM User:
Navigate to the IAM Management Console and click the blue āAdd userā button at the top. Give a descriptive username such as gitlabci
and select āProgrammatic accessā
You will next create a group if you havenāt already done so and attach a policy. For the means of this demo, we will use AmazonS3FullAccess
āāāhowever, you may want to change policies based on your security needs.
After the final step when you click ācreate userā you will be shown a success screen, which will house two very important pieces of information: Access Key and Secret Key. Note: Once you leave this screen, you will no longer have access to the Secret Key. You can either write it down or download theĀ .csv and then ensure you delete later. Whatever you do, make certain that no one gains access to the keys.
Either download theĀ .csv or click show secret key and copy and paste somewhere
Weāre almost there! Now we just need to setup GitLab to push to our S3 bucket.
GitLab:
Welcome, GitLab CI/CD. Prior to testing out GitLab CI/CD I always hosted my code on GitHub. GitHub did an amazing job at keeping my source code secure and widely available, however, I noticed that without some elbow grease, it did pretty much just that. I told myself that I wanted to be able to make a code change, commit, push, and then see my new code deployed on the internet.
Head over to GitLab, signup for an account, and create a new project. GitLab acts just as GitHub in the sense that you will need to add its remote origin to your local project. Once the project is setup you will rename/add this as a new remote to your localĀ .git
Ā :
git remote rename origin old-origingit remote add origin https://gitlab.com/croossin/vues3example.gitgit push -u origin --all
Now for the key file that you need in order to get CI/CD setup with GitLab:
Funny enough, using GitHub Gistās for a GitLab article š¤£
Go to the root directory of your app and add this fileĀ .gitlab-ci.yml
and copy the content of the above Gist; once pushed, GitLab will automatically recognize and begin itās ādev-opsā process.
So what exactly does this file do?
stages:
This tells GitLab what āstepsā to run during your CI/CD process. You can easily forsee adding more steps such as test
, etc. Letās take a look at the first stageāāābuild:
build prod:image: node:10.15.0-stretchstage: buildonly:- tagsscript:# Install vuecli- npm install -g @vue/cli@latest# Install dependencies- npm install# Build App- npm run buildartifacts:paths:# Build folder- dist/expire_in: 1 hour
stages
that we described at the very top of the file. This letās GitLab know how stages are associated.package.json
āāāwhich for me looks something like: vue-cli-service build
dist/
Ā . With artifacts, we can set a path to āstoreā for other stages to reference. Thus, I set my path for the dist/
folder and set an expiration for 1 hour.Now for the final stage in the pipelineāāādeploy:
deploy prod:image: python:lateststage: deployonly:- tagsscript:- pip install awscli- aws s3 sync ./dist s3://YOUR_BUCKET_NAME
Python is the supported CLI for AWS, so we set our image to python:latest
. When this runs, it will install the latest AWS CLI and sync the dist folder with the bucket we created in the beginning. Ensure YOUR_BUCKET_NAME
is the same as the bucket we created in the S3 console.
Note: If you want both stage and production environments, you will need to create a second bucket in S3 (with the same configuration as the first) and use that as the staging bucket.
Last Step:
How does AWS know that it should allow this user to sync to the S3 bucket? From the IAM user we created in the beginning half of the article! Since I mentioned that it is an extremely bad technique to expose/commit your keys we will add them to GitLabās environment variables. Navigate to the CI/CD in the Settings tab and expand āEnvironment Variablesā
GitLab AWS Environment Variables
You will add two variables: AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
Ā . Fill in the values with the associated keys that you downloaded/copied when created the user in the IAM management console.
Weāre all set to test it out!
Push some code to master and watch GitLab automatically kick off the CI/CD process. You can watch the stages execute and see your completed pipelines:
Take a look at GitLabs CI/CD documentation to see how you can extend your jobs to accomplish anything you may need.
Summed up:
After completing these steps, you should in essence have:
.gitlab-ci.yml
and fill with our CI/CD stagesFeel free to comment, clap, or reach out if you have any questions or concern. I am planning to write a follow-up article in which I will show you how to deploy a CloudFront distribution in front of your S3 bucket to ensure fast and reliable distribution of your site as well as adding SSL certs from AWSās Certificate Manager!