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.


The first step is to setup the repository. The web site shall be in the master branch of a repository called <your username> 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> && cd <your username>
git init
git commit --allow-empty -m "Initializing master branch"
git remote add origin<your username>/<your username>
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.


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:

      - step:
            - echo "Starting Hugo site generation"
            - apt-get update -y && apt-get install wget
            - apt-get -y install git
            - wget
            - dpkg -i hugo*.deb
            - git clone -b master ssh://<your username>/<your username> public
            - git clone themes/cocoa
            - hugo -t cocoa -v
            - git config --global "<your email>"
            - git config --global "<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