Publishing a Website with Pages
A noticeboard in a school corridor shows whatever is pinned to it. Pin a new sheet, and everyone walking past sees it. Nobody has to run a printing press: the board just displays the paper.
Pages turns a repository into that noticeboard on the internet. GitHub, GitLab and Bitbucket can each serve the HTML, CSS, JavaScript and image files in a repository as a public website, for free, at an address of their own. You build the site with files, commit them, and git push. Every push updates the website. It suits project documentation, a personal portfolio, a course page or a small product site, anything that doesn't need a server-side program or database.
In this lesson, you will build a tiny site, push it, and switch on publishing on each platform.
A Tiny Website
A static website is just files. The one file every site needs is index.html, the page shown at the site's address. Navika makes a one-page site for the Fee Tracker and pushes it. As in the previous lessons, a bare repository in a folder stands in for the platform:
git init --bare --quiet fee-tracker.git
mkdir fee-tracker
cd fee-tracker
git init --quiet
mkdir docs
cat > docs/index.html <<'EOF'
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Fee Tracker</title></head>
<body>
<h1>Fee Tracker</h1>
<p>Track school fee payments in one place.</p>
</body>
</html>
EOF
git add docs
git commit --quiet -m "Add the project website"
git remote add origin ../fee-tracker.git
git push -u origin mainTo ../fee-tracker.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.The site lives in a docs folder, so it stays apart from the project's code. GitHub can publish a docs folder directly. GitLab and Bitbucket expect other places, shown below.
Turn Publishing On
Each platform publishes from a different place and gives the site a different address:
1. Repository > Settings > Pages.
2. Build and deployment > Source: Deploy from a branch.
3. Branch: main, folder: /docs (or / (root)). Click Save.
4. After a minute, the page shows the address:
https://<username>.github.io/fee-tracker/
A repository named exactly <username>.github.io becomes your
personal site at https://<username>.github.io/.
For sites that need a build step, choose Source: GitHub Actions.1. Put the site's files in a folder named public.
2. Add a .gitlab-ci.yml file with a job named pages
(see the next section), commit and push.
3. GitLab CI/CD runs the job and publishes the public folder.
4. Deploy > Pages shows the address, usually:
https://<username>.gitlab.io/fee-tracker/1. Create a repository named exactly <workspace>.bitbucket.io
(your workspace ID, then .bitbucket.io).
2. Push an index.html to its main branch.
3. The site appears at https://<workspace>.bitbucket.io/
Subfolders become pages too: a folder fees with its own
index.html is served at https://<workspace>.bitbucket.io/fees/
Each workspace has one such site.GitLab: The Pages Job
GitLab builds every site with its automation, GitLab CI/CD. The smallest setup is a job called pages that copies the site into a folder called public and hands it over as an artifact, the files a job keeps. Navika adds one, reusing her docs folder:
cat > .gitlab-ci.yml <<'EOF'
pages:
script:
- mkdir -p public
- cp -r docs/* public/
artifacts:
paths:
- public
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
EOF
git add .gitlab-ci.yml
git commit --quiet -m "Publish the docs folder with GitLab Pages"
git push --quiet
git ls-files.gitlab-ci.yml
docs/index.html| Line | Meaning |
|---|---|
pages: | The job's name. GitLab publishes the result of the job with this name. |
script: | The commands the job runs. Here, it copies docs into public. |
artifacts: paths: - public | Keep the public folder: this is what gets published |
rules: | Run the job only on the default branch (usually main) |
On GitHub and Bitbucket, the file does nothing, so one repository can serve all three platforms. Newer GitLab versions also accept other ways to name the Pages job and folder, which the GitLab Pages documentation describes.
Update the Site
There is no upload button. Change the files, commit, and push. Here, Navika adds a line crediting the team, by writing the page again:
cat > docs/index.html <<'EOF'
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Fee Tracker</title></head>
<body>
<h1>Fee Tracker</h1>
<p>Track school fee payments in one place.</p>
<p>Built by Navika, Srihitha and Advitha.</p>
</body>
</html>
EOF
git commit --quiet -am "Credit the team on the website"
git pushTo ../fee-tracker.git
f54820d..2c131ca main -> mainWithin a minute or two, the platform publishes the new version. If it doesn't change, check the platform's Pages settings or its automation log (GitHub's Actions tab, GitLab's Build > Pipelines) for an error.
Custom Domains
All three platforms can serve the site at your own domain, such as www.feetracker.example, instead of the platform's address: you add the domain in the Pages settings (on GitHub, this creates a CNAME file in the site folder) and point a DNS record to the platform, following its help page. GitHub and GitLab then provide HTTPS certificates automatically.
Common Mistakes
No index.html. The address then shows a "not found" page. Every site, and every subfolder you want as a page, needs one.
Publishing the wrong folder or branch. GitHub's settings name the branch and folder, GitLab publishes public, and Bitbucket the repository root. Check that your files are where the platform looks.
Expecting server-side code to run. Pages only serves files. Java, PHP or a database won't run there. Build static files and publish those.
Putting secrets in a Pages repository. The site is public, often even when the repository isn't.
Links that work locally but break online. A GitHub project site lives under /fee-tracker/, not at the root. Use relative links (about.html, not /about.html).
Under the Hood
Pages is not part of Git. After each push, the platform runs a small build: GitHub copies (or builds, with Jekyll or an Actions workflow) the chosen folder, GitLab runs the pages job, and Bitbucket takes the files of the special repository. The result is uploaded to the platform's web servers and a content delivery network, which answer requests for the site's address. Your repository stays the single source: to roll the website back, you revert the commit and push, exactly as for code.
Interview Corner
What is GitHub Pages?
A GitHub feature that serves a static website from a repository, from a branch and folder you choose (or from a GitHub Actions workflow), at https://<user>.github.io/<repo>/. Each push to the published branch updates the site.
How do you publish a site with GitLab Pages?
Add a .gitlab-ci.yml with a pages job whose output is the public folder (kept as an artifact). GitLab CI/CD runs it on the default branch and serves the folder at https://<namespace>.gitlab.io/<project>/.
How does Bitbucket host a static website?
From a repository named <workspace>.bitbucket.io, whose files are served at https://<workspace>.bitbucket.io/. The site is public even if the repository is private.
Can Pages run a Java web application?
No. Pages only serves static files: HTML, CSS, JavaScript, images. Server-side code needs a hosting service that runs it.
Practice
Challenge: a second page. Add a page docs/fees.html with the heading <h1>Fees</h1>, and a link to it in docs/index.html just before </body>: <a href="fees.html">See the fees</a>. (In your editor, add the one line. The solution writes the whole page again.) Commit, push, and list the files Git tracks.
What does Git print? Before you open the solution, decide how many files git ls-files lists, and why the link is written without a / at the start.
Show solution
echo "<h1>Fees</h1>" > docs/fees.html
cat > docs/index.html <<'EOF'
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Fee Tracker</title></head>
<body>
<h1>Fee Tracker</h1>
<p>Track school fee payments in one place.</p>
<p>Built by Navika, Srihitha and Advitha.</p>
<a href="fees.html">See the fees</a>
</body>
</html>
EOF
git add docs
git commit --quiet -m "Add a fees page"
git push --quiet
git ls-files.gitlab-ci.yml
docs/fees.html
docs/index.htmlThree files: the GitLab job and two pages. The link is relative (fees.html), so it works both on your computer and under https://<username>.github.io/fee-tracker/, where a link starting with / would point to the wrong place.
Checkpoint. In a repository of your own, publish a one-page site with the Pages feature of your platform. Change a sentence, push, and watch the site update. Then add a second page and link the two.
Key Takeaways
Quick reference
| Platform | Publishes | Address |
|---|---|---|
| GitHub | Branch and folder chosen in Settings > Pages | https://<user>.github.io/<repo>/ |
| GitLab | public from the pages job | https://<namespace>.gitlab.io/<project>/ |
| Bitbucket | Repository <workspace>.bitbucket.io | https://<workspace>.bitbucket.io/ |
Was this tutorial helpful?