Intro

A year or so ago, after a long time of neglect, I decided to finally put some effort and update my personal website. After reviewing the landscape of available options, I decided to abandon full features content management systems, and look for available solutions to create small sites (possibily single page), easy to maintain with minimal overhead.
I found Jekyll and it seemed perfect for my use case, despite the fact that is written in Ruby which I am not familiar. The decision was made, so I set off and read the documentation. I found a suitable open source theme and I set aside sometime to create my new website, this website.

Article Image

The challenge

Initially I focused mostly on creating content, understanding what I wanted to post, the flow and the design. All the testing and depolyment was done manually from the laptop that I’m writing from right in this moment. After many iterations and new designs, I realized that the deployment was becoming the most time consuming task of the whole process.
Every time it was time for a new release and deployment, I had forgot which commands I needed to use, which version of the site I was deploying, tag correctly the repository and so on. I needed to find a solution to reduce the time spend on the deployment and automate the process once and for all.

The solution

At work, we constantly use CI for deployment and testing, both on GitHub and other platforms. Thanks to the team I work with, I have been learning continuous integration, the tools and how to configure it, specifically on GitHub.
Back to my site, I host my site in a github repo, why not using GitHub actions to implement CI for my website? Levergaging git hut actions, I wanted to test the site, version it correctly and deploy it directly to my hosting provider. I set aside some more time to learn more about GitHub actions. I studied how to configure them to achieve my CI pipeline, and invest some effort to set it up.

My current CI solution relies on a private repository with two main branches:

  • master which contains the latest code
  • release which contains the versions of site that has been deployed including the current one. Additional branches are created as needed when I develop new features or add new content.

The workflow that I decided to use losely follow the original git flow. When I want to add a new feature, fix a bug or remove any content from my site, I create a new branch, a feature branch. In this branch, I do all my work and commit all the code and content changes. When the feature is ready, I create a PR from the feature branch to the master branch. If everything goes according to plan, the PR will be merged to master, so master will contain the latest and greatest version of my site.
When I’m ready to release master on my public site hosted by my provider, I create a new PR from master to release branch. If the PR is successful, it will be merged to release branch and soon deployed to my hosting service.

In the next section, I review the details of the workflow including the github actions configurations, and how they work.

The workflow

Before I did set my hands on the keyboard and click away, I outlined on paper the workflow of the development and deployment my site should follow.

The steps of the workflow are:

  1. Create a feature branch
  2. some work is done
  3. Create a PR to master
  4. Following two actions are run on the PR:
    1. PR Title Check
    2. Test
  5. If #3 is successful, merge the PR to master
  6. Repeate 1 to 5 as many time as needed
  7. We are ready to update the public version of my site, aka deploy the new version in production
  8. Create a PR from master to release
  9. Following two actions are run on the PR (like in the case of a feature branch PR):
    1. PR Title Check
    2. Test
  10. If #9 is successful, merge the PR to release
  11. Actions run upon merging the PR to release:
    1. Build and Deploy

New features, new content and bug fixes

Following the standard git flow, any feature development, adding new content or bug fixes requires a dedicated branch. Once the branch is ready to be merged, I create a PR from the specific feature branch to master. Two actions are run on the PR:

  • PR title check
  • Test

Only when these two actions are successful, the PR can be merged to master and included in the official code base. Figure 1 shows the two actions run on a feature PR showing that they have been successfull and therefore the Pr can be merged.

GitHub Actions Tests Figure 1: Actions’ tests passing for a feature branch on a PR to Master

Action: PR Title Check

The PR Title Check action verify that title of the PR follows the semantic version rules. The title must start with a specific keyword followed by colon, followed by a short description of the title for the PR. This actions requires a configuration which is provided in a file saved under the folder .github.

Action: Test

The Test action simply check if the site can be build and readied for deployment by the Jekyll engine

New release

When I am ready to release and deploy the new version of the site, I create a PR from master to release. By this time, master contains the deployment ready version of the site.
This PR will trigger the same two actions listed for PR to master: PT Title Check and Test. If both of the actions are successful, than the PR can be merged. The merge is performed manually and triggers the Build and Deploy action.

Action: Build and Deploy

This action contains two jobs. The first one creates a new release properly tagged in release. It also inject in the site configuration the correct version string and proceed to commit it back to release.
The second job check out the latest release created by the previous one, build the site with Jekyll and push the resulting files directly on the hosting server.

The details

In the folder .github/workflows, I created three files which contains the definition of the three actions used in my workflow:

  • pr-title-check.yml
  • test.yml
  • release-and-deploy.yml

Figure 2 shows the three files in the github web interface.

GitHub Actions Files
Figure 2: Actions’ files listed in GitHub web interface

In the following paragraphs, I will review the content of such files and discuss the details of each sections.

If any detail is not clear or missing, you are more than welcome to contact me and also check the github actions documentation.

PR Title Check

The file pr-title-check.yml contains the following yaml code:

name: PR Title Checker

on:
  pull_request:
    branches:
      - master
      - release
    types:
      - opened
      - edited
      - synchronize
      - labeled
      - unlabeled

jobs:
  check-pr-title:
    name: Check Pr Title
    runs-on: ubuntu-latest
    steps:
      - uses: thehanimo/pr-title-checker@v1.4.3
        with:
          GITHUB_TOKEN: $
          pass_on_octokit_error: false
          configuration_path: .github/pr-title-checker-config.json

The first line assigns the name of this action, which I will be shown in the list of actions that have been run in the github actions tab of my repository, as shown in figure 3.

GitHub Actions Names Runs List
Figure 3: Actions’ names in the GitHub actions tab

The keyword on indicates when this actions should run. Action Check PR Title will be run when a PR is created or modified and the destination (or base) branch is master or release.

The keyword jobs define individual jobs within this action. The job is labeled check-pr-title. This name is used by the CI workflow machine. To be more clear and user friendly, I explicitly declared the named Check PR Title with the key name. It will run on a runner (virtual machine) with the latest ubuntu OS. The jobs has only one step, using the predefined action thehanimo/pr-title-checker, version v1.4.3, available in the github actions marketplaces. In my use case, I pass the github token for my repo, provide the path to my configuration file, and I disable the parameter pass_on_octokit_path as specified in the action documentation.

The configuration file define the regex to be used to validate the title. The file content is:

{
  "LABEL": {
    "name": ""
  },
  "CHECKS": {
    "NOTE": "You can test the regex here: https://regex101.com/r/nDeps5/1",
    "regexp": "(^(?<type>build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test|BREAKING CHANGE)(?<scope>\\([\\w\\s*-]+\\))?(?<breaking_change>!?): (?<body>([a-z]).+[^.|\\s])$)",
    "regexpFlags": "gm"
  },
  "MESSAGES": {
    "success": "All OK",
    "failure": "PR title not following the semantic-release guidelines (<type>(<scope>): <subject>). Please check https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#-git-commit-guidelines for more information.",
    "notice": ""
  }
}

The PR title prefixes that I mostly use are fix, feat, and BREAKING CHANGE, which trigger respectively a fix, minor and major version change in the resulting release.

Following are a couple of examples of PR title. The first one is an accepted title, while the second one is marked invalid by the action.

  • feat: adding tech posts
  • FIX error on header

Test

The action file test.yml contains the following configuration:

name: Site Building Test

on:
  pull_request: 
    branches:
      - master
      - release

jobs:
  test_build:
    name: Test Build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      
      - name: Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.0'

      - name: Install dependencies
        run: |
          gem install bundler
          bundle install

      - name: Build Jekyll site
        run: bundle exec jekyll build

First line define the name, which is Site Building Test.

The on keyword tells github to run this action when the PR is created toward the base branch of master and release.

Only one job is defined in this action. It is explicitly named Test Build, and consists of 4 sequential steps:

  1. Check out the latest version of the code present in the branch
  2. Set up a ruby environment, using the ready-made action ruby/setup-ruby. We specifically select Ruby version 3.0
  3. Install all the Ruby dependencies needed to run Jekyll
  4. Build the site using the Jekyll command bundle exec jekyll build

If Jekyll engine is able to build the site successfully, the action will be successful.

As I’m not an expert of Ruby and its intricacies, I relied solely on Jekyll documentation to write the commands needed for this steps. I also used ChatGTP to figure out some of the details.

Release and Deploy

The file release-and-deploy.yml contains the following code:

name: Release and Deploy

on:
  push:
    branches:
      - release

jobs:
  create-release:
    name: Create Release
    runs-on: ubuntu-latest
    permissions:
      contents: write
      packages: write

    outputs:
      full_tag: $
      bare_tag: $
      changelog: $

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      ## Commit message examples for Release type (patch|minor|major) can be found:
      ## https://github.com/mathieudutour/github-tag-action
      - name: Bump version and push tag
        id: tag_version
        uses: mathieudutour/github-tag-action@v6.2
        with:
          github_token: ${ secrets.GITHUB_TOKEN }
          release_branches: ${ env.RELEASE_BRANCH }
          dry_run: true

      - name: Strip 'v' from the tag
        id: bare_tag_version
        run: |
          TAG=${ steps.tag_version.outputs.new_tag }
          WITHOUT_V=${TAG#v}
          echo "tag=$WITHOUT_V" >> $GITHUB_OUTPUT

      - name: Version update and tag
        id: version_update_and_tag
        run: |
          pwd
          ls -la
          git config --global user.email "version.update@github.workflow.com"
          git config --global user.name "Version update in release workflow"
          sed -i 's/^site-version.*/site-version: "${ steps.bare_tag_version.outputs.tag }"/' _config.yml
          git add _config.yml
          git commit -m "Updated version"
          ## git tag ${ steps.bare_tag_version.outputs.tag }  # now commits & tags with your identity
          git push --follow-tags
          echo "commit=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
        
      - name: Create a GitHub release
        uses: ncipollo/release-action@v1
        with:
          tag: ${ steps.tag_version.outputs.new_tag }
          name: Release ${ steps.tag_version.outputs.new_tag }
          body: ${ steps.tag_version.outputs.changelog }
          commit: ${ steps.version_update_and_tag.outputs.commit }

  build-and-deploy:
    name: Build and Deploy
    runs-on: ubuntu-latest
    needs: create-release

    steps:
    - name: Deployment
      run: |
        echo "Deploying maxnovelli.info"
        echo "Version: ${ needs.create-release.outputs.full_tag }"
        echo ""
        
    - name: Checkout code
      uses: actions/checkout@v4
      with:
        fetch-depth: 0 
        
    - name: Checkout correct release
      run: |
        git checkout ${ needs.create-release.outputs.full_tag }
        echo "Verifying version"
        grep version _config.yml
        echo ""

    - name: Setup Ruby
      uses: ruby/setup-ruby@v1
      with:
        ruby-version: '3.0'
        bundler-cache: true

    - name: Install dependencies
      run: |
        gem install bundler
        bundle install

    - name: Build site
      run: bundle exec jekyll build

    - name: Upload via SCP
      run: |
        mkdir -p ~/.ssh
        echo "<DEPLOYMENT_SSH_KEY>" > ~/.ssh/id_deployment
        chmod 600 ~/.ssh/id_deployment
        ssh-keyscan <DEPLOYMENT_HOST> >> ~/.ssh/known_hosts
        scp -i ~/.ssh/id_deployment -r _site/* <DEPLOYMENT_USER_NAME>@<DEPLOYMENT_HOST>:<DEPLOYMENT_FOLDER>

As you can see, this action is the most complicated of the three. It is explicitly named Release and Deploy and it runs only when there is a push (aka a merge) on the branch release. This happens only when a release PR is merged, aka a PR from master to release is merged. The actions contains 2 separate jobs.

Job 1: Create Release

The first job is explicitly named Create Release and requires a runner with the latest ubuntu OS. It produces three outputs that can be accessed by the second job. They are:

  • full_tag: the new version string prefixed with v. Example: v1.2.3
  • bare_tag: the new version string without v as prefix. Example: 1.2.3
  • changelog: the list of the commit messages to master since the last release

The job has 5 steps:

  1. Checkout the latest version from release
  2. Create the new release tag using action mathieudutour/github-tag-action on the release branch, running it in dry mode (no changes made). The release tag will contain the prefix v.
  3. Strip the prefixed v from the version string
  4. Manually save the version string in the site configuration, tag the repo and commit all the changes back to release
  5. Create a new release with release tag define in step 1 using the code committed in step 4. This step uses the action _ncipollo/release-action

Job 2: Build and Deploy

The second job of this action builds the site with Jekyll and than pushes the files to the hosting server.

The job is configured to run on the latest version of ubuntu os. It also requires the successful completion of job 1 to be triggered (keyword needs).
It has the following 7 steps:

  1. Logs the version of the site that will be deployed.
  2. Checkout the code from the repo
  3. Manually checkout the repo matching the latest release tag.
    This step is required as each job access by default the commit available at the beginning of the job itself
  4. Install Ruby version 3.0 with action ruby/setup-ruby
  5. Install all the Ruby dependencies to run Jekyll
  6. Build the site with the command bundle exec jekyll build
  7. Push the code to the deployment server using scp with the ssh key saved in the secrets

Conclusions

The total effort to configure this setup in github CI was around one day of work, all done in my personal time.
It was definitely worth it, as now I have the whole deployment process completely automated and I do not need to worry anymore about re-learning all the commands used in the process.
An additional benefit was that I improved my knowledge about github actions, Also I improved the deployment process and I understand it a lot better, Last but not least, I was forced to review my website code base and implement improvements which lead to a cleaner code (although more work is needed).


This content is licensed under a Creative Commons Attribution-ShareAlike 4.0 International license.