1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
|
---
stage: Plan
group: Knowledge
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://handbook.gitlab.com/handbook/product/ux/technical-writing/#assignments
---
# Tutorial: Create a GitLab Pages website from scratch
DETAILS:
**Tier:** Free, Premium, Ultimate
**Offering:** GitLab.com, Self-managed, GitLab Dedicated
This tutorial shows you how to create a Pages site from scratch using
the [Jekyll](https://jekyllrb.com/) Static Site Generator (SSG). You start with
a blank project and create your own CI/CD configuration file, which gives
instructions to a [runner](https://docs.gitlab.com/runner/). When your CI/CD
[pipeline](../../../../ci/pipelines/index.md) runs, the Pages site is created.
This example uses Jekyll, but other SSGs follow similar steps.
You do not need to be familiar with Jekyll or SSGs
to complete this tutorial.
To create a GitLab Pages website:
- [Step 1: Create the project files](#create-the-project-files)
- [Step 2: Choose a Docker image](#choose-a-docker-image)
- [Step 3: Install Jekyll](#install-jekyll)
- [Step 4: Specify the `public` directory for output](#specify-the-public-directory-for-output)
- [Step 5: Specify the `public` directory for artifacts](#specify-the-public-directory-for-artifacts)
- [Step 6: Deploy and view your website](#deploy-and-view-your-website)
## Prerequisites
You must have a [blank project](../../index.md#create-a-blank-project) in GitLab.
## Create the project files
Create three files in the root (top-level) directory:
- `.gitlab-ci.yml`: A YAML file that contains the commands you want to run.
For now, leave the file's contents blank.
- `index.html`: An HTML file you can populate with whatever HTML content
you'd like, for example:
```html
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
```
- [`Gemfile`](https://bundler.io/gemfile.html): A file that describes dependencies for Ruby programs.
Populate it with this content:
```ruby
source "https://rubygems.org"
gem "jekyll"
```
## Choose a Docker image
In this example, the runner uses a [Docker image](../../../../ci/docker/using_docker_images.md)
to run scripts and deploy the site.
This specific Ruby image is maintained on [DockerHub](https://hub.docker.com/_/ruby).
Edit your `.gitlab-ci.yml` file and add this text as the first line:
```yaml
image: ruby:3.2
```
If your SSG needs [NodeJS](https://nodejs.org/) to build, you must specify an
image that contains NodeJS as part of its file system. For example, for a
[Hexo](https://gitlab.com/pages/hexo) site, you can use `image: node:12.17.0`.
## Install Jekyll
To run [Jekyll](https://jekyllrb.com/) locally, you must install it:
1. Open your terminal.
1. Install [Bundler](https://bundler.io/) by running `gem install bundler`.
1. Create `Gemfile.lock` by running `bundle install`.
1. Install Jekyll by running `bundle exec jekyll build`.
To run Jekyll in your project, edit the `.gitlab-ci.yml` file
and add the installation commands:
```yaml
script:
- gem install bundler
- bundle install
- bundle exec jekyll build
```
In addition, in the `.gitlab-ci.yml` file, each `script` is organized by a `job`.
A `job` includes the scripts and settings you want to apply to that specific
task.
```yaml
job:
script:
- gem install bundler
- bundle install
- bundle exec jekyll build
```
For GitLab Pages, this `job` has to include a property, called `pages`.
This setting tells the runner you want the job to deploy your website
with GitLab Pages:
```yaml
deploy-pages:
script:
- gem install bundler
- bundle install
- bundle exec jekyll build
pages: true # specifies that this is a Pages job
```
The example in this page uses [user-defined job names](../index.md#user-defined-job-names).
## Specify the `public` directory for output
Jekyll needs to know where to generate its output.
GitLab Pages only considers files in a directory called `public`.
Jekyll uses a destination flag (`-d`) to specify an output directory for the built website.
Add the destination to your `.gitlab-ci.yml` file:
```yaml
deploy-pages:
script:
- gem install bundler
- bundle install
- bundle exec jekyll build -d public
pages: true # specifies that this is a Pages job
```
## Specify the `public` directory for artifacts
Now that Jekyll has output the files to the `public` directory,
the runner needs to know where to get them. The artifacts are stored
in the `public` directory:
```yaml
deploy-pages:
script:
- gem install bundler
- bundle install
- bundle exec jekyll build -d public
pages: true # specifies that this is a Pages job
artifacts:
paths:
- public
```
Your `.gitlab-ci.yml` file should now look like this:
```yaml
image: ruby:3.2
deploy-pages:
script:
- gem install bundler
- bundle install
- bundle exec jekyll build -d public
pages: true # specifies that this is a Pages job
artifacts:
paths:
- public
```
## Deploy and view your website
After you have completed the preceding steps,
deploy your website:
1. Save and commit the `.gitlab-ci.yml` file.
1. Go to **Build > Pipelines** to watch the pipeline.
1. When the pipeline is finished, go to **Deploy > Pages** to find the link to
your Pages website.
When this `pages` job completes successfully, a special `pages:deploy` job
appears in the pipeline view. It prepares the content of the website for the
GitLab Pages daemon. GitLab runs it in the background and doesn't use a runner.
## Other options for your CI/CD file
If you want to do more advanced tasks, you can update your `.gitlab-ci.yml` file
with [other CI/CD YAML keywords](../../../../ci/yaml/index.md). You can validate
your `.gitlab-ci.yml` file with the [CI Lint](../../../../ci/lint.md) tool that's included with GitLab.
The following topics show other examples of other options you can add to your CI/CD file.
### Deploy specific branches to a Pages site
You may want to deploy to a Pages site only from specific branches.
First, add a `workflow` section to force the pipeline to run only when changes are
pushed to branches:
```yaml
image: ruby:3.2
workflow:
rules:
- if: $CI_COMMIT_BRANCH
deploy-pages:
script:
- gem install bundler
- bundle install
- bundle exec jekyll build -d public
pages: true # specifies that this is a Pages job
artifacts:
paths:
- public
```
Then configure the pipeline to run the job for the
[default branch](../../repository/branches/default.md) (here, `main`) only.
```yaml
image: ruby:3.2
workflow:
rules:
- if: $CI_COMMIT_BRANCH
deploy-pages:
script:
- gem install bundler
- bundle install
- bundle exec jekyll build -d public
pages: true # specifies that this is a Pages job
artifacts:
paths:
- public
rules:
- if: $CI_COMMIT_BRANCH == "main"
```
### Specify a stage to deploy
There are three default stages for GitLab CI/CD: build, test,
and deploy.
If you want to test your script and check the built site before deploying
to production, you can run the test exactly as it runs when you
push to your [default branch](../../repository/branches/default.md) (here, `main`).
To specify a stage for your job to run in,
add a `stage` line to your CI file:
```yaml
image: ruby:3.2
workflow:
rules:
- if: $CI_COMMIT_BRANCH
deploy-pages:
stage: deploy
script:
- gem install bundler
- bundle install
- bundle exec jekyll build -d public
pages: true # specifies that this is a Pages job
artifacts:
paths:
- public
rules:
- if: $CI_COMMIT_BRANCH == "main"
environment: production
```
Now add another job to the CI file, telling it to
test every push to every branch **except** the `main` branch:
```yaml
image: ruby:3.2
workflow:
rules:
- if: $CI_COMMIT_BRANCH
deploy-pages:
stage: deploy
script:
- gem install bundler
- bundle install
- bundle exec jekyll build -d public
pages: true # specifies that this is a Pages job
artifacts:
paths:
- public
rules:
- if: $CI_COMMIT_BRANCH == "main"
environment: production
test:
stage: test
script:
- gem install bundler
- bundle install
- bundle exec jekyll build -d test
artifacts:
paths:
- test
rules:
- if: $CI_COMMIT_BRANCH != "main"
```
When the `test` job runs in the `test` stage, Jekyll
builds the site in a directory called `test`. The job affects
all branches except `main`.
When you apply stages to different jobs, every job in the same
stage builds in parallel. If your web application needs more than
one test before being deployed, you can run all your tests at the
same time.
### Remove duplicate commands
To avoid duplicating the same scripts in every job, you can add them
to a `before_script` section.
In the example, `gem install bundler` and `bundle install` were running
for both jobs, `pages` and `test`.
Move these commands to a `before_script` section:
```yaml
image: ruby:3.2
workflow:
rules:
- if: $CI_COMMIT_BRANCH
before_script:
- gem install bundler
- bundle install
deploy-pages:
stage: deploy
script:
- bundle exec jekyll build -d public
pages: true # specifies that this is a Pages job
artifacts:
paths:
- public
rules:
- if: $CI_COMMIT_BRANCH == "main"
environment: production
test:
stage: test
script:
- bundle exec jekyll build -d test
artifacts:
paths:
- test
rules:
- if: $CI_COMMIT_BRANCH != "main"
```
### Build faster with cached dependencies
To build faster, you can cache the installation files for your
project's dependencies by using the `cache` parameter.
This example caches Jekyll dependencies in a `vendor` directory
when you run `bundle install`:
```yaml
image: ruby:3.2
workflow:
rules:
- if: $CI_COMMIT_BRANCH
cache:
paths:
- vendor/
before_script:
- gem install bundler
- bundle install --path vendor
deploy-pages:
stage: deploy
script:
- bundle exec jekyll build -d public
pages: true # specifies that this is a Pages job
artifacts:
paths:
- public
rules:
- if: $CI_COMMIT_BRANCH == "main"
environment: production
test:
stage: test
script:
- bundle exec jekyll build -d test
artifacts:
paths:
- test
rules:
- if: $CI_COMMIT_BRANCH != "main"
```
In this case, you need to exclude the `/vendor`
directory from the list of folders Jekyll builds. Otherwise, Jekyll
tries to build the directory contents along with the site.
In the root directory, create a file called `_config.yml`
and add this content:
```yaml
exclude:
- vendor
```
Now GitLab CI/CD not only builds the website, but also:
- Pushes with **continuous tests** to feature branches.
- **Caches** dependencies installed with Bundler.
- **Continuously deploys** every push to the `main` branch.
To view the HTML and other assets that were created for the site,
[download the job artifacts](../../../../ci/jobs/job_artifacts.md#download-job-artifacts).
The example in this page uses [user-defined job names](../index.md#user-defined-job-names).
## Related topics
- [Deploy your web app to staging and production](https://about.gitlab.com/blog/2021/02/05/ci-deployment-and-environments/)
- [Run jobs sequentially, in parallel, or build a custom pipeline](https://about.gitlab.com/blog/2020/12/10/basics-of-gitlab-ci-updated/)
- [Pull specific directories from different projects](https://about.gitlab.com/blog/2016/12/07/building-a-new-gitlab-docs-site-with-nanoc-gitlab-ci-and-gitlab-pages/)
- [Use GitLab Pages to produce a code coverage report](https://about.gitlab.com/blog/2016/11/03/publish-code-coverage-report-with-gitlab-pages/)
|