As mentioned in the previous post, I use Hugo and Gitlab to host, build and deploy our site.

In this post, I share the code that uses Gitlab’s Continuous Integration feature that allows us to build and deploy the site using code below. Create .gitlab-ci.yml in the root directory of your Hugo site with the code below.

    image: publysher/hugo

    pages:
      script:
      - hugo -v
      artifacts:
        paths:
        - public
      only:
      - master

What does Gitlab CI do?

Everytime you commit (git push) your code to the master branch of your repository, Gitlab CI checks if .gitlab-ci.yml exists. If it exists, it pulls the Docker image specified in the YAML image variable (i.e. publysher/hugo for above) and runs the script within the script variable. This process and additional information is explained here for further information.

For reference, an output of the Gitlab CI from a recent build stage log using the code above is included below.

Hope this helps you automate your Hugo CI/CD workflow with Gitlab CI. Please leave a comment below for any questions and I will be glad to help.

Running with gitlab-ci-multi-runner 1.9.4 (8ce22bd)
Using Docker executor with image publysher/hugo ...
Pulling docker image publysher/hugo ...
Running on runner-fa6cab46-project-2276918-concurrent-0 via runner-fa6cab46-machine-1485109515-a7cf3d0d-gce...
Cloning repository...
Cloning into '/builds/mistryious/mistryious'...
Checking out 2a728717 as master...
$ hugo -v
INFO: 2017/01/22 18:26:39 hugo.go:349: Using config file: /builds/mistryious/mistryious/config.toml
INFO: 2017/01/22 18:26:39 hugo.go:497: using a UnionFS for static directory comprised of:
INFO: 2017/01/22 18:26:39 hugo.go:498: Base: /builds/mistryious/mistryious/themes/hugo-tranquilpeak-theme/static
INFO: 2017/01/22 18:26:39 hugo.go:499: Overlay: /builds/mistryious/mistryious/static/
INFO: 2017/01/22 18:26:39 hugo.go:532: syncing static files to /builds/mistryious/mistryious/public/
Started building sites ...
INFO: 2017/01/22 18:26:39 site.go:1482: found taxonomies: map[string]string{"tag":"tags", "category":"categories", "archive":"archives"}
INFO: 2017/01/22 18:26:39 htmlredirect.go:114: Alias "/page/1" translated to "page/1/index.html"
INFO: 2017/01/22 18:26:39 htmlredirect.go:114: Alias "/post/page/1" translated to "post/page/1/index.html"
Built site for language en-us:
0 of 1 draft rendered
0 future content
0 expired content
4 regular pages created
2 other pages created
0 non-page files copied
2 paginator pages created
0 tags created
0 categories created
0 archives created
total in 53 ms
Uploading artifacts...
public: found 192 matching files                   
Uploading artifacts to coordinator... ok            id=9083195 responseStatus=201 Created token=UdDyW6fo
Build succeeded