Prerequisites:
- Set up a gitlab page
- Have a cloudflare account
- Have your domain registered at a registrar or at cloudflare
Introduction
In a previous blog i explained howto setup gitlab page. However it is possible to use a custom domainname. And since i’m a nerd, i wanted to configure that aswell.
Register your domain
Register you domain name at a registrar like transip.nl or clouddlfare.com
I used transip.nl myself
- Change the nameservers to:
terin.ns.cloudflare.com
zoe.ns.cloudflare.com
When using transip.nl, make sure that Geavanceerd domeinbeheer TransIP-instellingen
is set to off
2. Remove the ipv6 (AAAA) record
3. Save all
Cloudflare config
Let’s change the DNS records at cloudflare so it will point in the right direction.
- Enter cloudflare and add your domain
add a site
- remove the ipv6 (AAAA) record
- Do a dig on
<username>.gitlab.io
or<username>.gitlab.io/<projectname>
- copy the ipv4 address
- Edit the A record of your domain and alter the ip address to the one you just copied. (This will point it to the gitlab page rather than your isp)
- Add the following record:
CNAME WWW <username>.gitlab.io
- Save
Add a pagerule if you so choose to redirect root (domain.com)
to www.domain.com
- in Cloudflare click on the rules button
- Add a new rule:
- At the url box type:
domain.com/*
- In the settings dropdown box choose:
Forwarding URL
- As a status code select:
301 permanent redirect
- And as for the destination url:
https://www.domain.com/$1
- At the url box type:
Set the correct SSL / TLS mode
- In cloudflare click on SSL / TLS and then overview
- Select
Full
as SSL / TLS encryption mode
note: This will make sure all traffic is encrypted and the 301 redirect rule does not come in a http –> https loop
Gitlab page: adding the custom domain
Let’s add your custom domain to the gitlab page configuration
- Go to the page’s settings on gitlab
You can use this example url and change
<username>
with your own:
https://gitlab.com/<username>/<username>.gitlab.io/edit#js-general-project-settings
- On the samepage click
New domain
- Enter your domain here
- Slide the bar of
Certificate
to activated (blue) - Click new domain, voila the domain is added
Gitlab page: verification token
Now we have to register this domain to the gitlab page.
- Go to the page’s settings on gitlab and click edit behind your domain
- Copy the data next to verification status
- Go back to your cloudflare environment, DNS
- Add a new record, like underneath: use your own specific record ofcourse ;-)
_gitlab-pages-verification-code.www.customdomain.com TXT gitlab-pages-verification-code=b123451435146324151
- Save it
- Now the waiting and F5-ing game begins! It can take up to 24 hours for dns records to be globally available to the various (ISP) DNS servers. Usally between 1 and 4 hours though.
- After awhile you can verify the status at your gitlab domain page by clicking the refresh arrow behind
verification status
. When it’s green you’re done here.
Changing the baseurl
- In your git repo, open
config.toml
- Change the baseurl var as follows: note: else CSS etc will not work properly
#baseurl = "https://<username>.gitlab.io"
baseurl = "https://wwww.domain.com"
- commit and push the change to git
- Merge dev into master
- Let the CI/CD pipeline work it’s magic
- Voila your gitlab page is reachable on your own (custom) domain.
Clear history / data / cookies in case it doesn’t work straight away