Commit 48026c7b authored by Kati Paizee's avatar Kati Paizee Committed by Amy Qualls

Update Pages topic to a tutorial style

parent 98a6368b
...@@ -4,40 +4,54 @@ group: Release ...@@ -4,40 +4,54 @@ group: Release
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 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
--- ---
# Create a GitLab Pages website from scratch **(FREE)** # Tutorial: Create a GitLab Pages website from scratch **(FREE)**
This tutorial shows you how to create a Pages site from scratch. You start with This tutorial shows you how to create a Pages site from scratch using
a blank project and create your own CI file, which gives instruction to the [Jekyll](https://jekyllrb.com/) Static Site Generator (SSG). You start with
a [runner](https://docs.gitlab.com/runner/). When your CI/CD 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. [pipeline](../../../../ci/pipelines/index.md) runs, the Pages site is created.
This example uses the [Jekyll](https://jekyllrb.com/) Static Site Generator (SSG). This example uses Jekyll, but other SSGs follow similar steps.
Other SSGs follow similar steps. You do not need to be familiar with Jekyll or SSGs You do not need to be familiar with Jekyll or SSGs
to complete this tutorial. 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 ## Prerequisites
To follow along with this example, start with a blank project in GitLab. You must have a [blank project](../../working_with_projects.md#blank-projects) in GitLab.
Create three files in the root (top-level) directory.
- `.gitlab-ci.yml` A YAML file that contains the commands you want to run. ## Create the project files
For now, leave the file's contents blank.
- `index.html` An HTML file you can populate with whatever HTML content you'd like, Create three files in the root (top-level) directory:
for example:
```html - `.gitlab-ci.yml`: A YAML file that contains the commands you want to run.
<html> For now, leave the file's contents blank.
<head>
<title>Home</title> - `index.html`: An HTML file you can populate with whatever HTML content
</head> you'd like, for example:
<body>
<h1>Hello World!</h1> ```html
</body> <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.
- [`Gemfile`](https://bundler.io/gemfile.html) A file that describes dependencies for Ruby programs.
Populate it with this content: Populate it with this content:
```ruby ```ruby
...@@ -53,7 +67,7 @@ to run scripts and deploy the site. ...@@ -53,7 +67,7 @@ to run scripts and deploy the site.
This specific Ruby image is maintained on [DockerHub](https://hub.docker.com/_/ruby). This specific Ruby image is maintained on [DockerHub](https://hub.docker.com/_/ruby).
Edit your `.gitlab-ci.yml` and add this text as the first line. Edit your `.gitlab-ci.yml` file and add this text as the first line:
```yaml ```yaml
image: ruby:2.7 image: ruby:2.7
...@@ -65,13 +79,15 @@ image that contains NodeJS as part of its file system. For example, for a ...@@ -65,13 +79,15 @@ image that contains NodeJS as part of its file system. For example, for a
## Install Jekyll ## Install Jekyll
To run [Jekyll](https://jekyllrb.com/) locally, you would open your terminal and: To run [Jekyll](https://jekyllrb.com/) locally, you must install it:
- Install [Bundler](https://bundler.io/) by running `gem install bundler`. 1. Open your terminal.
- Create `Gemfile.lock` by running `bundle install`. 1. Install [Bundler](https://bundler.io/) by running `gem install bundler`.
- Install Jekyll by running `bundle exec jekyll build`. 1. Create `Gemfile.lock` by running `bundle install`.
1. Install Jekyll by running `bundle exec jekyll build`.
In the `.gitlab-ci.yml` file, this is written as: To run Jekyll in your project, edit the `.gitlab-ci.yml` file
and add the installation commands:
```yaml ```yaml
script: script:
...@@ -109,7 +125,8 @@ pages: ...@@ -109,7 +125,8 @@ pages:
Jekyll needs to know where to generate its output. Jekyll needs to know where to generate its output.
GitLab Pages only considers files in a directory called `public`. GitLab Pages only considers files in a directory called `public`.
Jekyll uses destination (`-d`) to specify an output directory for the built website: 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 ```yaml
pages: pages:
...@@ -136,7 +153,7 @@ pages: ...@@ -136,7 +153,7 @@ pages:
- public - public
``` ```
Paste this into `.gitlab-ci.yml` file, so it now looks like this: Your `.gitlab-ci.yml` file should now look like this:
```yaml ```yaml
image: ruby:2.7 image: ruby:2.7
...@@ -151,23 +168,29 @@ pages: ...@@ -151,23 +168,29 @@ pages:
- public - public
``` ```
Now save and commit the `.gitlab-ci.yml` file. You can watch the pipeline run ## Deploy and view your website
by going to **CI/CD > Pipelines**.
When it succeeds, go to **Settings > Pages** to view the URL where your site After you have completed the preceding steps,
is now available. 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
If you want to do more advanced tasks, you can update your `.gitlab-ci.yml` file If you want to do more advanced tasks, you can update your `.gitlab-ci.yml` file
with [any of the available settings](../../../../ci/yaml/index.md). You can validate with [any of the available settings](../../../../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. your `.gitlab-ci.yml` file with the [CI Lint](../../../../ci/lint.md) tool that's included with GitLab.
After successful execution of this `pages` job, a special `pages:deploy` job appears in the
pipeline view. It prepares the content of the website for GitLab Pages daemon. GitLab executes it in
the background and doesn't use runner.
The following topics show other examples of other options you can add to your CI/CD file. The following topics show other examples of other options you can add to your CI/CD file.
## Deploy specific branches to a Pages site ### Deploy specific branches to a Pages site
You may want to deploy to a Pages site only from specific branches. You may want to deploy to a Pages site only from specific branches.
...@@ -191,7 +214,8 @@ pages: ...@@ -191,7 +214,8 @@ pages:
- public - public
``` ```
Then configure the pipeline to run the job for the `master` branch only. Then configure the pipeline to run the job for the
[default branch](../../repository/branches/default.md) (here, `main`) only.
```yaml ```yaml
image: ruby:2.7 image: ruby:2.7
...@@ -209,17 +233,17 @@ pages: ...@@ -209,17 +233,17 @@ pages:
paths: paths:
- public - public
rules: rules:
- if: '$CI_COMMIT_BRANCH == "master"' - if: '$CI_COMMIT_BRANCH == "main"'
``` ```
## Specify a stage to deploy ### Specify a stage to deploy
There are three default stages for GitLab CI/CD: build, test, There are three default stages for GitLab CI/CD: build, test,
and deploy. and deploy.
If you want to test your script and check the built site before deploying 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 to production, you can run the test exactly as it runs when you
push to `master`. push to your [default branch](../../repository/branches/default.md) (here, `main`).
To specify a stage for your job to run in, To specify a stage for your job to run in,
add a `stage` line to your CI file: add a `stage` line to your CI file:
...@@ -241,11 +265,11 @@ pages: ...@@ -241,11 +265,11 @@ pages:
paths: paths:
- public - public
rules: rules:
- if: '$CI_COMMIT_BRANCH == "master"' - if: '$CI_COMMIT_BRANCH == "main"'
``` ```
Now add another job to the CI file, telling it to Now add another job to the CI file, telling it to
test every push to every branch **except** the `master` branch: test every push to every branch **except** the `main` branch:
```yaml ```yaml
image: ruby:2.7 image: ruby:2.7
...@@ -264,7 +288,7 @@ pages: ...@@ -264,7 +288,7 @@ pages:
paths: paths:
- public - public
rules: rules:
- if: '$CI_COMMIT_BRANCH == "master"' - if: '$CI_COMMIT_BRANCH == "main"'
test: test:
stage: test stage: test
...@@ -276,19 +300,19 @@ test: ...@@ -276,19 +300,19 @@ test:
paths: paths:
- test - test
rules: rules:
- if: '$CI_COMMIT_BRANCH != "master"' - if: '$CI_COMMIT_BRANCH != "main"'
``` ```
When the `test` job runs in the `test` stage, Jekyll When the `test` job runs in the `test` stage, Jekyll
builds the site in a directory called `test`. The job affects builds the site in a directory called `test`. The job affects
all branches except `master`. all branches except `main`.
When you apply stages to different jobs, every job in the same When you apply stages to different jobs, every job in the same
stage builds in parallel. If your web application needs more than stage builds in parallel. If your web application needs more than
one test before being deployed, you can run all your tests at the one test before being deployed, you can run all your tests at the
same time. same time.
## Remove duplicate commands ### Remove duplicate commands
To avoid duplicating the same scripts in every job, you can add them To avoid duplicating the same scripts in every job, you can add them
to a `before_script` section. to a `before_script` section.
...@@ -317,7 +341,7 @@ pages: ...@@ -317,7 +341,7 @@ pages:
paths: paths:
- public - public
rules: rules:
- if: '$CI_COMMIT_BRANCH == "master"' - if: '$CI_COMMIT_BRANCH == "main"'
test: test:
stage: test stage: test
...@@ -327,10 +351,10 @@ test: ...@@ -327,10 +351,10 @@ test:
paths: paths:
- test - test
rules: rules:
- if: '$CI_COMMIT_BRANCH != "master"' - if: '$CI_COMMIT_BRANCH != "main"'
``` ```
## Build faster with cached dependencies ### Build faster with cached dependencies
To build faster, you can cache the installation files for your To build faster, you can cache the installation files for your
project's dependencies by using the `cache` parameter. project's dependencies by using the `cache` parameter.
...@@ -361,7 +385,7 @@ pages: ...@@ -361,7 +385,7 @@ pages:
paths: paths:
- public - public
rules: rules:
- if: '$CI_COMMIT_BRANCH == "master"' - if: '$CI_COMMIT_BRANCH == "main"'
test: test:
stage: test stage: test
...@@ -371,7 +395,7 @@ test: ...@@ -371,7 +395,7 @@ test:
paths: paths:
- test - test
rules: rules:
- if: '$CI_COMMIT_BRANCH != "master"' - if: '$CI_COMMIT_BRANCH != "main"'
``` ```
In this case, you need to exclude the `/vendor` In this case, you need to exclude the `/vendor`
...@@ -386,10 +410,11 @@ exclude: ...@@ -386,10 +410,11 @@ exclude:
- vendor - vendor
``` ```
Now GitLab CI/CD not only builds the website, Now GitLab CI/CD not only builds the website, but also:
but also pushes with **continuous tests** to feature-branches,
**caches** dependencies installed with Bundler, and - Pushes with **continuous tests** to feature branches.
**continuously deploys** every push to the `master` branch. - **Caches** dependencies installed with Bundler.
- **Continuously deploys** every push to the `main` branch.
## Related topics ## Related topics
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment