Setting up CI with jekyll and gitlab

2024-01-05

Get started with Jekyll here: https://jekyllrb.com/

1. setup the CI config

Example:

stages:          # List of stages for jobs, and their order of execution
  - build
  - deploy

variables:
  S3_BUCKET_NAME: <S3 bucket name>
  CDN_DISTRIBUTION_ID: <CDN distribution ID>

cache:
  paths:
  - vendor/
  - node_modules/

prepare_npm:
  stage: build
  image: node:latest
  script:
    - yarn install
  only:
  - tags

generate_pages:
  stage: build
  image: ruby:2.6
  needs:
    - prepare_npm
  before_script:
    - gem install bundler -v 2.4.15
  script:
    - bundle install --path vendor
    - bundle exec jekyll build -d public
  artifacts:
    paths:
    - public
  only:
  - tags

deploy_s3:
  stage: deploy
  image: python:latest
  dependencies:
    - generate_pages
  before_script:
    - pip install awscli
  script:
    - aws s3 cp public s3://$S3_BUCKET_NAME --recursive
    - aws cloudfront create-invalidation --distribution-id $CDN_DISTRIBUTION_ID --paths "/*"
  only:
  - tags

2. Setup the AWS resources

  • S3 bucket with name <S3 bucket name>
  • Cloudfront

3. Setup gitlab access to AWS with IAM

Create policy with access to gitlab. Example:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "s3:DeleteObject",
                "cloudfront:CreateInvalidation"
            ],
            "Resource": [
                "arn:aws:s3:::<S3 bucket name>/*",
                "arn:aws:cloudfront::<cloudfront distribution ID>"
            ]
        }
    ]
}
  • Create user and attach the above policy
  • Create access key and save both the access key and the secret key

4. Setup AWS keys in gitlab

  • Go to repo > Settings > CI/CD > Variables and enter the necessary keys: AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
  • The above variables are required for gitlab to access the AWS resources.
  • By default these variables are only accessible to pipelines running on protected branches and tags.

5. Setup protected tags and branches

  • Go to repo > Settings > Repository > Protected branches to setup the protected branch
  • Go to repo > Settings > Repository > Protected tags to setup a wildcard for protected tags

6. Done!