Publishing the Static Website Automatically

Updated: Jun 15, 2021

GitLab Pages

Just like GitHub Pages 1, GitLab Pages 2 are also available. GitLab Pages are basically a git repository with special .gitlab-ci.yml which will push the static html content you have in that repository under yourname.gitlab.io/projectname. It is easy to setup 3.

Using Gitlab CI to build and publish

There are two choices for me to publish my website

  • Build html and pdf locally in my laptop and push the resulted content to gitlab. By using their default GitLab Pages specific .gitlab-ci.yml, I can just publish easily

  • Write my own .gitlab-ci.yml and build both html and pdf content using Gitlab’s CI 4 and publish.

I selected the second way and here is my custom .gitlab-ci.yml

.initialize: &initialize
  - useradd -d "${CI_PROJECT_DIR}" -m -U -s /bin/bash user0
  - chown -R user0:user0 "${CI_PROJECT_DIR}"
  - apt update -y -q --force-yes && apt full-upgrade -y -q --force-yes

.html-apt: &html-apt
  - apt install -y -q --force-yes python3-minimal python3-distutils curl patch make git tar

default:
  image: ubuntu

variables:
  DEV: 1
  DEBIAN_FRONTEND: noninteractive

html:
  stage: build
  only:
    - master
  before_script:
    - *initialize
    - *html-apt
  script:
    - su user0 -c 'cd; scripts/bootstrap poetry run bash -c "(scripts/add-timestamp.sh; make -C docs html)"'
    - su user0 -c 'cd; tar cvzf "${CI_PROJECT_NAME}".tar.gz -C docs/_build/html/ .'
  artifacts:
    name: "${CI_PROJECT_NAME}.tar.gz"
    paths:
      - "${CI_PROJECT_NAME}.tar.gz"

pdf:
  stage: build
  only:
    - master
  before_script:
    - *initialize
    - *html-apt
    - apt install -y -q --force-yes texlive-xetex fonts-freefont-otf latexmk xindy
  script:
    - su user0 -c 'cd; scripts/bootstrap poetry run bash -c "(scripts/add-timestamp.sh; make -C docs latexpdf)"'
    - su user0 -c 'cd; tar cvzf "${CI_PROJECT_NAME}".pdf.tar.gz -C docs/_build/latex/ mohan43usspace.pdf'
  artifacts:
    name: "${CI_PROJECT_NAME}.pdf.tar.gz"
    paths:
      - "${CI_PROJECT_NAME}.pdf.tar.gz"

pages:
  stage: deploy
  only:
    - master
  before_script:
    - *initialize
  script:
    - su user0 -c 'cd; mkdir public && tar xvzf "${CI_PROJECT_NAME}.tar.gz" -C public/'
    - su user0 -c 'cd; tar xvzf "${CI_PROJECT_NAME}.pdf.tar.gz" -C public/_static/misc'
  artifacts:
    paths:
      - public

As you see, there are three jobs I defined,

  • html

  • pdf

  • pages

html job basically build html pages, pdf job builds pdf document and pages job is job specifically for publishing the contents to GitLab Pages.

Custom Domain

GitLab Pages also supports Custom Domain 5 with SSL/TLS automatically generated through LetsEncrypt 6. But, There are bit of work to do in our DNS Records. Once we do the required changes in the DNS Records, GitLab will do the rest automatically.

Thus, everything is automated, all I need to do is write blog posts consistently, I hope I’ll try to do after this migration, because I feel I really like writing in reStructuredText.

End.


1

https://pages.github.com/

2

https://about.gitlab.com/stages-devops-lifecycle/pages/

3

https://docs.gitlab.com/ee/user/project/pages/

4

https://docs.gitlab.com/ee/ci/

5

https://docs.gitlab.com/ee/user/project/pages/custom_domains_ssl_tls_certification/

6

https://letsencrypt.org/