2020-06-24 00:08:43 +00:00
---
2022-03-23 03:07:30 +00:00
stage: Create
group: Editor
2020-11-26 06:09:20 +00:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2020-06-24 00:08:43 +00:00
---
2021-07-06 00:08:15 +00:00
# Tutorial: Create a GitLab Pages website from scratch **(FREE)**
2020-06-24 00:08:43 +00:00
2021-07-06 00:08:15 +00:00
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
2020-06-24 00:08:43 +00:00
[pipeline ](../../../../ci/pipelines/index.md ) runs, the Pages site is created.
2021-07-06 00:08:15 +00:00
This example uses Jekyll, but other SSGs follow similar steps.
You do not need to be familiar with Jekyll or SSGs
2020-06-24 00:08:43 +00:00
to complete this tutorial.
2021-07-06 00:08:15 +00:00
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 )
2020-06-24 00:08:43 +00:00
## Prerequisites
2021-12-08 18:12:44 +00:00
You must have a [blank project ](../../working_with_projects.md#create-a-blank-project ) in GitLab.
2020-06-24 00:08:43 +00:00
2021-07-06 00:08:15 +00:00
## Create the project files
2020-06-24 00:08:43 +00:00
2021-07-06 00:08:15 +00:00
Create three files in the root (top-level) directory:
2020-06-24 00:08:43 +00:00
2021-07-06 00:08:15 +00:00
- `.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.
2020-06-24 00:08:43 +00:00
Populate it with this content:
```ruby
source "https://rubygems.org"
gem "jekyll"
```
## Choose a Docker image
2020-09-15 06:09:32 +00:00
In this example, the runner uses a [Docker image ](../../../../ci/docker/using_docker_images.md )
2020-06-24 00:08:43 +00:00
to run scripts and deploy the site.
This specific Ruby image is maintained on [DockerHub ](https://hub.docker.com/_/ruby ).
2021-07-06 00:08:15 +00:00
Edit your `.gitlab-ci.yml` file and add this text as the first line:
2020-06-24 00:08:43 +00:00
```yaml
image: ruby:2.7
```
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
2021-07-06 00:08:15 +00:00
To run [Jekyll ](https://jekyllrb.com/ ) locally, you must install it:
2020-06-24 00:08:43 +00:00
2021-07-06 00:08:15 +00:00
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` .
2020-06-24 00:08:43 +00:00
2021-07-06 00:08:15 +00:00
To run Jekyll in your project, edit the `.gitlab-ci.yml` file
and add the installation commands:
2020-06-24 00:08:43 +00:00
```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 a specific name, called `pages` .
2020-09-15 06:09:32 +00:00
This setting tells the runner you want the job to deploy your website
2020-06-24 00:08:43 +00:00
with GitLab Pages:
```yaml
pages:
script:
2020-07-03 09:08:53 +00:00
- gem install bundler
- bundle install
- bundle exec jekyll build
2020-06-24 00:08:43 +00:00
```
## 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` .
2021-07-06 00:08:15 +00:00
Jekyll uses a destination flag (`-d`) to specify an output directory for the built website.
Add the destination to your `.gitlab-ci.yml` file:
2020-06-24 00:08:43 +00:00
```yaml
pages:
script:
2020-07-03 09:08:53 +00:00
- gem install bundler
- bundle install
- bundle exec jekyll build -d public
2020-06-24 00:08:43 +00:00
```
## Specify the `public` directory for artifacts
Now that Jekyll has output the files to the `public` directory,
2020-09-15 06:09:32 +00:00
the runner needs to know where to get them. The artifacts are stored
2020-06-24 00:08:43 +00:00
in the `public` directory:
```yaml
pages:
script:
2020-07-03 09:08:53 +00:00
- gem install bundler
- bundle install
- bundle exec jekyll build -d public
2020-06-24 00:08:43 +00:00
artifacts:
paths:
2020-07-03 09:08:53 +00:00
- public
2020-06-24 00:08:43 +00:00
```
2021-07-06 00:08:15 +00:00
Your `.gitlab-ci.yml` file should now look like this:
2020-06-24 00:08:43 +00:00
```yaml
image: ruby:2.7
pages:
script:
2020-07-03 09:08:53 +00:00
- gem install bundler
- bundle install
- bundle exec jekyll build -d public
2020-06-24 00:08:43 +00:00
artifacts:
paths:
2020-07-03 09:08:53 +00:00
- public
2020-06-24 00:08:43 +00:00
```
2021-07-06 00:08:15 +00:00
## Deploy and view your website
2020-06-24 00:08:43 +00:00
2021-07-06 00:08:15 +00:00
After you have completed the preceding steps,
deploy your website:
1. Save and commit the `.gitlab-ci.yml` file.
1. Go to **CI/CD > Pipelines** to watch the pipeline.
1. When the pipeline succeeds, go to **Settings > Pages**
to view the URL where your site is now available.
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
2020-06-24 00:08:43 +00:00
If you want to do more advanced tasks, you can update your `.gitlab-ci.yml` file
2021-06-28 21:10:13 +00:00
with [any of the available settings ](../../../../ci/yaml/index.md ). You can validate
2020-11-04 03:09:14 +00:00
your `.gitlab-ci.yml` file with the [CI Lint ](../../../../ci/lint.md ) tool that's included with GitLab.
2020-06-24 00:08:43 +00:00
The following topics show other examples of other options you can add to your CI/CD file.
2021-07-06 00:08:15 +00:00
### Deploy specific branches to a Pages site
2020-06-24 00:08:43 +00:00
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:2.7
workflow:
rules:
- if: '$CI_COMMIT_BRANCH'
pages:
script:
2020-07-03 09:08:53 +00:00
- gem install bundler
- bundle install
- bundle exec jekyll build -d public
2020-06-24 00:08:43 +00:00
artifacts:
paths:
2020-07-03 09:08:53 +00:00
- public
2020-06-24 00:08:43 +00:00
```
2021-07-06 00:08:15 +00:00
Then configure the pipeline to run the job for the
[default branch ](../../repository/branches/default.md ) (here, `main` ) only.
2020-06-24 00:08:43 +00:00
```yaml
image: ruby:2.7
workflow:
rules:
- if: '$CI_COMMIT_BRANCH'
pages:
script:
2020-07-03 09:08:53 +00:00
- gem install bundler
- bundle install
- bundle exec jekyll build -d public
2020-06-24 00:08:43 +00:00
artifacts:
paths:
2020-07-03 09:08:53 +00:00
- public
2020-06-24 00:08:43 +00:00
rules:
2021-07-06 00:08:15 +00:00
- if: '$CI_COMMIT_BRANCH == "main"'
2020-06-24 00:08:43 +00:00
```
2021-07-06 00:08:15 +00:00
### Specify a stage to deploy
2020-06-24 00:08:43 +00:00
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
2020-12-17 21:09:57 +00:00
to production, you can run the test exactly as it runs when you
2021-07-06 00:08:15 +00:00
push to your [default branch ](../../repository/branches/default.md ) (here, `main` ).
2020-06-24 00:08:43 +00:00
To specify a stage for your job to run in,
add a `stage` line to your CI file:
```yaml
image: ruby:2.7
workflow:
rules:
- if: '$CI_COMMIT_BRANCH'
pages:
stage: deploy
script:
2020-07-03 09:08:53 +00:00
- gem install bundler
- bundle install
- bundle exec jekyll build -d public
2020-06-24 00:08:43 +00:00
artifacts:
paths:
2020-07-03 09:08:53 +00:00
- public
2020-06-24 00:08:43 +00:00
rules:
2021-07-06 00:08:15 +00:00
- if: '$CI_COMMIT_BRANCH == "main"'
2020-06-24 00:08:43 +00:00
```
Now add another job to the CI file, telling it to
2021-07-06 00:08:15 +00:00
test every push to every branch **except** the `main` branch:
2020-06-24 00:08:43 +00:00
```yaml
image: ruby:2.7
workflow:
rules:
- if: '$CI_COMMIT_BRANCH'
pages:
stage: deploy
script:
2020-07-03 09:08:53 +00:00
- gem install bundler
- bundle install
- bundle exec jekyll build -d public
2020-06-24 00:08:43 +00:00
artifacts:
paths:
2020-07-03 09:08:53 +00:00
- public
2020-06-24 00:08:43 +00:00
rules:
2021-07-06 00:08:15 +00:00
- if: '$CI_COMMIT_BRANCH == "main"'
2020-06-24 00:08:43 +00:00
test:
stage: test
script:
2020-07-03 09:08:53 +00:00
- gem install bundler
- bundle install
- bundle exec jekyll build -d test
2020-06-24 00:08:43 +00:00
artifacts:
paths:
2020-07-03 09:08:53 +00:00
- test
2020-06-24 00:08:43 +00:00
rules:
2021-07-06 00:08:15 +00:00
- if: '$CI_COMMIT_BRANCH != "main"'
2020-06-24 00:08:43 +00:00
```
When the `test` job runs in the `test` stage, Jekyll
builds the site in a directory called `test` . The job affects
2021-07-06 00:08:15 +00:00
all branches except `main` .
2020-06-24 00:08:43 +00:00
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.
2021-07-06 00:08:15 +00:00
### Remove duplicate commands
2020-06-24 00:08:43 +00:00
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:2.7
workflow:
rules:
- if: '$CI_COMMIT_BRANCH'
before_script:
- gem install bundler
- bundle install
pages:
stage: deploy
script:
2020-07-03 09:08:53 +00:00
- bundle exec jekyll build -d public
2020-06-24 00:08:43 +00:00
artifacts:
paths:
2020-07-03 09:08:53 +00:00
- public
2020-06-24 00:08:43 +00:00
rules:
2021-07-06 00:08:15 +00:00
- if: '$CI_COMMIT_BRANCH == "main"'
2020-06-24 00:08:43 +00:00
test:
stage: test
script:
2020-07-03 09:08:53 +00:00
- bundle exec jekyll build -d test
2020-06-24 00:08:43 +00:00
artifacts:
paths:
2020-07-03 09:08:53 +00:00
- test
2020-06-24 00:08:43 +00:00
rules:
2021-07-06 00:08:15 +00:00
- if: '$CI_COMMIT_BRANCH != "main"'
2020-06-24 00:08:43 +00:00
```
2021-07-06 00:08:15 +00:00
### Build faster with cached dependencies
2020-06-24 00:08:43 +00:00
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:2.7
workflow:
rules:
- if: '$CI_COMMIT_BRANCH'
cache:
paths:
2020-07-03 09:08:53 +00:00
- vendor/
2020-06-24 00:08:43 +00:00
before_script:
- gem install bundler
- bundle install --path vendor
pages:
stage: deploy
script:
2020-07-03 09:08:53 +00:00
- bundle exec jekyll build -d public
2020-06-24 00:08:43 +00:00
artifacts:
paths:
2020-07-03 09:08:53 +00:00
- public
2020-06-24 00:08:43 +00:00
rules:
2021-07-06 00:08:15 +00:00
- if: '$CI_COMMIT_BRANCH == "main"'
2020-06-24 00:08:43 +00:00
test:
stage: test
script:
2020-07-03 09:08:53 +00:00
- bundle exec jekyll build -d test
2020-06-24 00:08:43 +00:00
artifacts:
paths:
2020-07-03 09:08:53 +00:00
- test
2020-06-24 00:08:43 +00:00
rules:
2021-07-06 00:08:15 +00:00
- if: '$CI_COMMIT_BRANCH != "main"'
2020-06-24 00:08:43 +00:00
```
In this case, you need to exclude the `/vendor`
directory from the list of folders Jekyll builds. Otherwise, Jekyll
2020-12-17 21:09:57 +00:00
tries to build the directory contents along with the site.
2020-06-24 00:08:43 +00:00
In the root directory, create a file called `_config.yml`
and add this content:
```yaml
exclude:
- vendor
```
2021-07-06 00:08:15 +00:00
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.
2020-06-24 00:08:43 +00:00
## Related topics
For more information, see the following blog posts.
- [Use GitLab CI/CD `environments` to deploy your
2021-02-20 00:10:55 +00:00
web app to staging and production](https://about.gitlab.com/blog/2021/02/05/ci-deployment-and-environments/).
2020-06-24 00:08:43 +00:00
- Learn [how to run jobs sequentially,
in parallel, or build a custom pipeline](https://about.gitlab.com/blog/2016/07/29/the-basics-of-gitlab-ci/).
- Learn [how to 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/ )
to deploy this website, < https: / / docs . gitlab . com > .
- Learn [how to use GitLab Pages to produce a code coverage report ](https://about.gitlab.com/blog/2016/11/03/publish-code-coverage-report-with-gitlab-pages/ ).