Skip to content
⚠️ This wiki is intended and relevant only for DTU Compute - Department of Applied Mathematics and Computer Science

Pages

You can host a project or personal web page via the Pages feature in GitLab.

The examples below uses the Hugo static site generator.

Note

Remember to enable GitLab Runners for the project.

Deploy on GitLab server

In the left sidebar, go to Deploy → Pages where a wizard/guide is available to assist with the creation of the .gitlab-ci.yml file.

# The container image that will be used to build your app
image: docker.io/hugomods/hugo:latest
create-pages:
  pages:
    # The folder that contains the files to be exposed at the Page URL
    publish: public
  script:
    - hugo
  rules:
    # This ensures that only pushes to the default branch will trigger
    # a pages deploy
    - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH

After committing this file, and for any future commits, the website will be build. When going to Deploy → Pages again, you can see the URL which will be something like:

projectname-d81a31.pages.compute.dtu.dk

Deploy pages to external web server

Alternatively, host your pages on another server, such as the DTU Compute Web Server by adding a deploy stage to your project’s .gitlab-ci.yml file, and setting up variables for your SSH keys. In the following example, we use the DTU username abcd who wants to deploy their website made with Hugo, to their personal people.compute.dtu.dk site:

Create a new SSH key pair for this purpose and authorize it on the remote host:

ssh-keygen -f ~/.ssh/deploy -C "GitLab Pages CI"
ssh-copy-id -i ~/.ssh/deploy abcd@linuxterm1.compute.dtu.dk

Using the GitLab web interface, in your Pages repository go to Settings → CI/CD and expand Variables

image

Two variables need to be added here, with the first one called SSH_PRIVATE_KEY with its value being the content of ~/.ssh/deploy.

Important

When adding the SSH_PRIVATE_KEY variable, choose the Masked and hidden visibility option.

Note that the content of the private key can not be added as is, because of whitespace characters! By default the private key will look something like this:

-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACCjvunxRYrdvTXPwMTjRbxUcDHKBDhTIsTtz5VY3axosQAAAJDeeykl3nsp
JQAAAAtzc2gtZWQyNTUxOQAAACCjvunxRYrdvTXPwMTjRbxUcDHKBDhTIsTtz5VY3axosQ
AAAEBP3LT6Ly0pkmEkTB5vvlEfm1scUQk1KJZZ2aIRvlgL3qO+6fFFit29Nc/AxONFvFRw
MoEOFMixO3PlVjdrGixAAAAC3Jvb3RAcG9kdHN0AQI=
-----END OPENSSH PRIVATE KEY-----

To get around this, encode it to a string using the base64 command:

cat ~/.ssh/deploy | base64 -w0

This will output a very long string that can be added to the variable, like this:

LS0tLS1CRUdJTiBPUEVOU1NIIFBSSVZBVEUgS0VZLS0tLS0KYjNCbGJuTnphQzFyWlhrdGRqRUFBQUFBQkc1dmJtVUFBQUFFYm05dVpRQUFBQUFBQUFBQkFBQUFNd0FBQUF0emMyZ3RaVwpReU5UVXhPUUFBQUNDanZ1bnhSWXJkdlRYUHdNVGpSYnhVY0RIS0JEaFRJc1R0ejVWWTNheG9zUUFBQUpEZWV5a2wzbnNwCkpRQUFBQXR6YzJndFpXUXlOVFV4T1FBQUFDQ2p2dW54UllyZHZUWFB3TVRqUmJ4VWNESEtCRGhUSXNUdHo1VlkzYXhvc1EKQUFBRUJQM0xUNkx5MHBrbUVrVEI1dnZsRWZtMXNjVVFrMUtKWloyYUlSdmxnTDNxTys2ZkZGaXQyOU5jL0F4T05GdkZSdwpNb0VPRk1peE8zUGxWamRyR2l4QUFBQUMzSnZiM1JBY0c5a2RITjBBUUk9Ci0tLS0tRU5EIE9QRU5TU0ggUFJJVkFURSBLRVktLS0tLQo=

This will be decoded later in the .gitlab-ci.yml file.

The second variable called SSH_KNOWN_HOSTS with its value being the output of the following command (leaving out comments starting with #):

ssh-keyscan linuxterm1.compute.dtu.dk

With the variables set it is time to setup to the .gitlab-ci.yml file:

# The container image that will be used to build your app
image: registry.gitlab.com/pages/hugo:latest

variables:
  GIT_SUBMODULE_STRATEGY: recursive # If you have git submodules
  HUGO_ENV: production              # Tells Hugo this is for production

personal-site:
  stage: deploy
  script:
    # Need SSH client for rsync to work
    - apk add --no-cache rsync openssh-client
    # Add the key and known host
    - mkdir -p ~/.ssh
    - eval "$(ssh-agent -s)"
    - echo "$SSH_PRIVATE_KEY" | base64 -d | ssh-add -
    - echo "$SSH_KNOWN_HOSTS" >> ~/.ssh/known_hosts
    - chmod -R 700 ~/.ssh
    # Build the website
    - hugo
    # Sync the built site
    - rsync -cavz --delete ./public/ abcd@linuxterm1.compute.dtu.dk:/www/home/abcd/public_html/
  artifacts:
    paths:
      - public
  rules:
    # This ensures that only pushes to the default branch will trigger a pages deploy
    - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH

Add and commit the changes and push to the repository. If the CI/CD Pipeline passed, the user’s website is now available at https://people.compute.dtu.dk/abcd

Last updated on