Gatsby is an open-source web framework that lets you build really fast and secure websites. I love because it gives me a lot of control without a lot of complexity. This replaced Wordpress for any of my personal projects.
I've used and deployed Gatsby before using Netlify. It provided a lot of features without a lot of effort such as built-in CI/CD, domain and DNS management, and even automated previews of every commit to your Git repo. The best part: they provide these features on the free tier!
I wanted to replicate some of that here. I will provide a walkthrough of how to deploy your GatsbyJS site to AWS S3 using GitLab CICD pipelines.
The walkthrough is didvided into two parts: the AWS and GitLab setup
Create a new S3 bucket that is publicly accessible.
Attach a new policy to the bucket that allows public access.
Copy and paste the policy below to allow the public to read files from this bucket
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::mydomain.com/*"
}
]
}
At this point, you can host your Gatsby site by copying the contents of the "public" directory to this S3 bucket after you've run npm run build
on your local machine. However, I'm sure you're reading this for the CI/CD part. Keep reading!
Here, you are creating a new policy to use for a new user we will be creating in Step 5. The policy provides seeing what buckets are available as well as read and write permissions on the specified S3 bucket.
Click "Create policy"
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"s3:PutAccountPublicAccessBlock",
"s3:GetAccountPublicAccessBlock",
"s3:ListAllMyBuckets",
"s3:HeadBucket"
],
"Resource": "*"
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::mydomain.com/*",
"arn:aws:s3:::mydomain.com"
]
}
]
}
.gitlab-ci.yml
at the root of your repositoryvariables:
BUCKET_NAME_PRODUCTION: mydomain.com
stages:
- build
- deploy
# This folder is cached between builds
# http://docs.gitlab.com/ce/ci/yaml/README.html#cache
cache:
paths:
- node_modules/
build-assets:
image: node:latest
stage: build
artifacts:
paths:
- public/
script:
- npm install
- npm run build
deploy-s3:
stage: deploy
image: python:3.8
before_script:
- pip3 install awscli --upgrade
script:
- aws s3 sync public s3://$BUCKET_NAME_PRODUCTION/ --delete
environment: production
only:
- master
variables
allows you to define values that you can use throughout your CICD configuration file. Here, we are specifying the bucket name
cache:
paths:
- node_modules/
We will be building our Gatsby site and we will need to grab all of our dependencies. This allows us to specify folders or files to cache in between runs
build-assets:
image: node:latest
stage: build
artifacts:
paths:
- public/
script:
- npm install
- npm run build
build-assets
. This can be named anything you want.stage
image
specifies the docker image to use for the job. In this case, we are using the node:latest
. See https://hub.docker.com/_/node?tab=tagsartifacts
are files and directories that are created by a job once it finishes. Here, we are specifying that we are creating a directory called "public"script
section describes what scripts will run. These are the same scripts that you'd be running locally on your computer.npm install
to install all of our dependenciesnpm run build
which Gatsby will generate the production version of our website. This will output to the "public" directory
.gitlab-ci.yml
file, you should see a pipeline active with two jobs
.gitlab-ci.yml
, that generates GatsbyJS files from your code, then uploads it to your S3 bucketCheckout the source code on GitLab
Stay tuned for Part II where we use a custom domain for your website and setup a staging environment to test prior to going to production