Using GitLab CI/CD to Build your Jekyll Site and Upload it to AWS S3

Using GitLab CI/CD to Build your Jekyll Site and Upload it to AWS S3


Hosting your Jekyll generated static web site or blog on Amazon’s S3 is a no brainer. And with GitLab’s CI/CD you can build and deploy it automatically.

.gitlab-ci.yml

This is my .gitlab-ci.yml for CoffeeShopDeveloper.com:

stages:
  - build
  - deploy
  - cleanup
  
variables:
  S3_BUCKET_NAME: "coffeeshopdeveloper.com"

buildJekyll:
  image: ruby:2.6
  stage: build
  script:
  - apt-get update -y                     # only because of error "Invalid US-ASCII character "\xE2" on line..."
  - apt-get install -y locales            #  "
  - echo "en_US UTF-8" > /etc/locale.gen  #  "
  - locale-gen en_US.UTF-8                #  "
  - export LANG=en_US.UTF-8               #  "
  - export LANGUAGE=en_US:en              #  "
  - export LC_ALL=en_US.UTF-8             #  "
  - bundle install
  - bundle exec jekyll build -d _site
  artifacts:
    paths:
    - _site
  only:
  - master
  
deployS3:
  image: python:latest
  stage: deploy
  dependencies:
  - buildJekyll
  before_script:
  - pip install awscli
  script:
  - aws s3 cp _site s3://$S3_BUCKET_NAME --recursive
  environment:
    name: live
    url: http://${BUCKET_NAME}.s3-website.${AWS_DEFAULT_REGION}.amazonaws.com
    
    
invalidateCloudFront:
  image: python:latest
  stage: cleanup
  dependencies:
  - deployS3
  before_script:
  - pip install awscli
  script:
  - aws cloudfront create-invalidation --distribution-id $CDN_DISTRIBUTION_ID --paths "/*"

See this great blog post by Riccardo Padovani for more details.