iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🦊

Setting up GitLab Pages

に公開
2023/06/13

GitHub Copilot feels a bit creepy, so I'm thinking of moving to GitLab.

Actually, I used GitLab at my previous job and liked it better than GitHub, so I'd been planning to migrate eventually; this is a good opportunity.

For now, I'll try migrating the content of tmtms.net, which I was managing with GitHub Pages, to GitLab Pages.

GitLab Pages

With GitHub Pages, you can set it up quickly through the Pages settings UI in the repository, but with GitLab Pages, you need to configure it via CI, which is a bit more of a hassle.

Basically, it's as described in How to publish a static website with GitLab Pages | GitLab.JP.

For the user's top page, create a repository named username.gitlab.io on GitLab.

Just create a .gitlab-ci.yml file at the root of the repository and make the pages job create files under the public directory.

image: alpine:latest

pages:
  stage: deploy
  script:
    - mkdir -p public/
    - cp *.html *.txt public/
  artifacts:
    paths:
    - public
  only:
  - master

If the public directory already exists in the repository, there's no need to copy files manually.

When you run git push, the CI/CD pipeline executes, and the site is published at https://username.gitlab.io. Easy.

If you do the same with other repositories, they will be published at https://username.gitlab.io/repository-name/.

Unlike GitHub, you can publish pages even if the project (repository) isn't public on the free plan.
By default, private projects are only viewable by people with access to the project. If you want to make it public, go to "Settings" → "General" → "Visibility, project features, permissions" → "Pages" and set it to "Everyone".

Custom Domains

You can also use custom domains for your public pages. It's mostly the same as GitHub Pages.

Go to "Settings" → "Pages" in your project and click "New Domain."

Enter the domain name and click "Create New Domain."

Register the _gitlab-pages-verification-code.example.com.net TXT gitlab-pages-verification-code=XXXXXXXXXXXXXXXX shown in "Verification status" in your DNS and click the refresh icon (⭯). The verification status will change from "Unverified" to "Verified."

As shown in the "DNS" field, set example.com ALIAS username.gitlab.io. in your DNS (you can use CNAME instead of ALIAS, or even an A record with the IP address directly). Once you click "Save changes," the site will be published under your custom domain.

You can follow the same steps if you want to publish on a subdomain of your custom domain.

However, you cannot publish it as a subdirectory like https://example.com/repository-name. Without a custom domain, you could publish it as https://username.gitlab.io/repository-name/.

Publishing as a subdirectory of a custom domain

Since Pages are built using CI, I thought of copying content from other repositories within the main project's CI, and it worked perfectly.

While it's more of a hassle than GitHub, I like the flexibility it offers.

The .gitlab-ci.yml for the main repository looks like this:

image: alpine:latest

pages:
  stage: deploy
  script:
    - mkdir -p public/
    - cp *.html *.txt public/
    - wget --header "PRIVATE-TOKEN:$PRIVATE_TOKEN" https://gitlab.com/api/v4/projects/username%2Frepository1/repository/archive.zip
    - unzip archive.zip
    - mkdir -p public/repository1/
    - mv repository1-master-*/* public/repository1/
    - rm -f archive.zip
    - wget --header "PRIVATE-TOKEN:$PRIVATE_TOKEN" https://gitlab.com/api/v4/projects/username%2Frepository2/repository/archive.zip
    - unzip archive.zip
    - mkdir -p public/repository2/
    - mv repository2-master-*/* public/repository2/
    - rm -f archive.zip
    - wget --header "PRIVATE-TOKEN:$PRIVATE_TOKEN" https://gitlab.com/api/v4/projects/username%2Frepository3/repository/archive.zip
    - unzip archive.zip
    - mkdir -p public/repository3/
    - mv repository3-master-*/* public/repository3/
    - rm -f archive.zip
  artifacts:
    paths:
    - public
  only:
  - master

The flow involves fetching content from other repositories via the API and placing it under subdirectories.

First, go to "User Settings" → "Access Tokens" and create a token with the read_api scope. Copy the token value.
You could write the token value directly in .gitlab-ci.yml, but that's not great, so I set it as a GitLab variable instead.
By saving it as PRIVATE_TOKEN under "Settings" → "CI/CD" → "Variables" in the main repository's project, you can refer to it as $PRIVATE_TOKEN within .gitlab-ci.yml.

Triggering the main CI when a sub-repository is updated

The public page is updated when the main repository's CI runs, but updating a sub-repository doesn't trigger the main CI, so the content remains outdated.

This is a bit inconvenient, so I wanted the main CI to run automatically whenever a sub-repository is updated.

I was able to do this easily using trigger as described in Multi-project pipelines | GitLab.

Just write the sub-repository's .gitlab-ci.yml as follows. It's simple.

job:
  trigger: username/username.gitlab.io

Conclusion

It was more of a hassle than GitHub, but I was able to set it up with a relatively simple mechanism on GitLab as well.

I'm going to start moving my projects from GitHub to GitLab.

However, zenn.dev only supports GitHub... 😖

Discussion