github twitter linkedin rss
Bitbucket and continuous integration of a Hugo site
May 15, 2017
3 minutes read

This blog is hosted in Bitbucket personal pages. One interesting feature of Bitbucket are the Pipelines, which allows to define an automatic build and test process in your repository. I used a pipeline for generating this blog automatically from a commit of the source, as I explain in this post.

Repository

The first step is to setup the repository. The web site shall be in the master branch of a repository called <your username>.bitbucket.io and I used a branch called hugo` for the source code, as I use Hugo to generate this blog. The way of doing this is:

mkdir <your username>.bitbucket.io && cd <your username>.bitbucket.io
git init
git commit --allow-empty -m "Initializing master branch"
git remote add origin https://github.com/<your username>/<your username>.github.io.git
git push -u origin master
git checkout --orphan hugo
git reset --hard
git commit --allow-empty -m "Initializing hugo branch"
git push origin hugo

and now you can copy in this directory the source of your website.

It is possible to have both branches in your local disk by using git worktree (it is explained here), just in case you want to generate the web manually.

Pipeline

Once the web source is in the hugo branch, you can use a pipeline for deploying it into master branch. Pipelines are executed in each commit (or even in each PR) and they creates a container that can execute your script.

In our case, we want to execute hugo for generating the web (and for that we need to install it) and we want to upload the public content into the master branch. For we can include this pipeline into our repo:

pipelines:
  branches:
    hugo:
      - step:
          script:
            - echo "Starting Hugo site generation"
            - apt-get update -y && apt-get install wget
            - apt-get -y install git
            - wget https://github.com/spf13/hugo/releases/download/v0.20.7/hugo_0.20.7_Linux-64bit.deb
            - dpkg -i hugo*.deb
            - git clone -b master ssh://git@bitbucket.org/<your username>/<your username>.bitbucket.io public
            - git clone https://github.com/nishanths/cocoa-hugo-theme.git themes/cocoa
            - hugo -t cocoa -v
            - git config --global user.email "<your email>"
            - git config --global user.name "<your name>"
            - cd public && git status && git add . && git commit -m "generated from $BITBUCKET_COMMIT" && git push

As you can see, for each commit in hugo branch the pipeline generates a new commit in master branch with the content of public, where is the web site.

We only have to do one more thing: give read/write permissions to the pipeline in our repository. The way of doing this is to create a new SSH pair-key (Settings -> Pipelines -> SSH keys) and to add the public key in your user settings (Bitbucket settings -> Security -> SSH keys)

And that’s all! Now, your website or blog is automatically generated each time you push a new commit into the repository. And this allows you to create new entries or updates using your web browser (in your repo page) or using any kind of git client with a text editor.


Back to posts


comments powered by Disqus