247.md 10.4 KB
Newer Older
Lab机器人's avatar
readme  
Lab机器人 已提交
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
# Create a GitLab Pages website from scratch

> 原文:[https://docs.gitlab.com/ee/user/project/pages/getting_started/pages_from_scratch.html](https://docs.gitlab.com/ee/user/project/pages/getting_started/pages_from_scratch.html)

*   [Prerequisites](#prerequisites)
*   [Choose a Docker image](#choose-a-docker-image)
*   [Install Jekyll](#install-jekyll)
*   [Specify the `public` directory for output](#specify-the-public-directory-for-output)
*   [Specify the `public` directory for artifacts](#specify-the-public-directory-for-artifacts)
*   [Deploy specific branches to a Pages site](#deploy-specific-branches-to-a-pages-site)
*   [Specify a stage to deploy](#specify-a-stage-to-deploy)
*   [Remove duplicate commands](#remove-duplicate-commands)
*   [Build faster with cached dependencies](#build-faster-with-cached-dependencies)
*   [Related topics](#related-topics)

# Create a GitLab Pages website from scratch[](#create-a-gitlab-pages-website-from-scratch "Permalink")

本教程向您展示如何从头开始创建 Pages 站点. 您将从一个空白项目开始,并创建自己的 CI 文件,该文件向[GitLab Runner](https://docs.gitlab.com/runner/)提供指导. 当您的 CI / CD [管道](../../../../ci/pipelines/index.html)运行时,将创建 Pages 站点.

本示例使用[Jekyll](https://jekyllrb.com/)静态站点生成器(SSG). 其他 SSG 遵循类似的步骤. 您无需熟悉 Jekyll 或 SSG 即可完成本教程.

## Prerequisites[](#prerequisites "Permalink")

要继续执行本示例,请从 GitLab 中的空白项目开始. 在根(顶级)目录中创建三个文件.

*   `.gitlab-ci.yml`一个 YAML 文件,其中包含要运行的命令. 现在,将文件内容保留为空白.

*   `index.html`您可以使用所需的 HTML 内容填充 HTML 文件,例如:

    ```
    <html>
    <head>
      <title>Home</title>
    </head>
    <body>
      <h1>Hello World!</h1>
    </body>
    </html> 
    ```

*   [`Gemfile`](https://bundler.io/gemfile.html)一个描述 Ruby 程序依赖性的文件. 用以下内容填充它:

    ```
    source "https://rubygems.org"

    gem "jekyll" 
    ```

## Choose a Docker image[](#choose-a-docker-image "Permalink")

In this example, the Runner uses a [Docker image](../../../../ci/docker/using_docker_images.html) to run scripts and deploy the site.

这个特定的 Ruby 映像在[DockerHub](https://hub.docker.com/_/ruby)[维护](https://hub.docker.com/_/ruby) .

编辑您的`.gitlab-ci.yml`并将此文本添加为​​第一行.

```
image: ruby:2.7 
```

如果您的 SSG 需要构建[NodeJS](https://s0nodejs0org.icopy.site/) ,则必须指定一个包含 NodeJS 的映像作为其文件系统的一部分. 例如,对于[Hexo](https://gitlab.com/pages/hexo)网站,可以使用`image: node:12.17.0` .

## Install Jekyll[](#install-jekyll "Permalink")

要在本地运行[Jekyll](https://jekyllrb.com/) ,您需要打开终端并执行以下操作:

*   通过运行`gem install bundler`安装[Bundler](https://bundler.io/) .
*   通过运行`bundle install`创建`Gemfile.lock` .
*   通过运行`bundle exec jekyll build`安装 Jekyll.

`.gitlab-ci.yml`文件中,其写为​​:

```
script:
  - gem install bundler
  - bundle install
  - bundle exec jekyll build 
```

此外,在`.gitlab-ci.yml`文件中,每个`script`均由`job`组织. `job`包括您要应用于该特定任务的脚本和设置.

```
job:
  script:
  - gem install bundler
  - bundle install
  - bundle exec jekyll build 
```

对于 GitLab Pages,此`job`有一个特定的名称,称为`pages` . 此设置告诉 Runner 您希望工作通过 GitLab Pages 部署您的网站:

```
pages:
  script:
    - gem install bundler
    - bundle install
    - bundle exec jekyll build 
```

## Specify the `public` directory for output[](#specify-the-public-directory-for-output "Permalink")

Jekyll 需要知道在何处生成其输出. GitLab Pages 仅考虑名为`public`的目录中的文件.

Jekyll 使用目标( `-d` )为构建的网站指定输出目录:

```
pages:
  script:
    - gem install bundler
    - bundle install
    - bundle exec jekyll build -d public 
```

## Specify the `public` directory for artifacts[](#specify-the-public-directory-for-artifacts "Permalink")

既然 Jekyll 已将文件输出到`public`目录,则 Runner 需要知道从何处获取文件. 工件存储在`public`目录中:

```
pages:
  script:
    - gem install bundler
    - bundle install
    - bundle exec jekyll build -d public
  artifacts:
    paths:
      - public 
```

将其粘贴到`.gitlab-ci.yml`文件中,因此现在看起来像这样:

```
image: ruby:2.7

pages:
  script:
    - gem install bundler
    - bundle install
    - bundle exec jekyll build -d public
  artifacts:
    paths:
      - public 
```

现在保存并提交`.gitlab-ci.yml`文件. 您可以转到**CI / CD>管道**来观看管道运行.

成功后,请转到**"设置">"页面"**以查看您的网站现在可用的 URL.

如果您想执行更多高级任务,则可以使用[任何可用设置](../../../../ci/yaml/README.html)更新`.gitlab-ci.yml`文件. 您可以使用[GitLab CI / CD Lint Tool](../../../../ci/yaml/README.html#validate-the-gitlab-ciyml)来检查 CI 语法.

以下主题显示了可以添加到 CI / CD 文件中的其他选项的其他示例.

## Deploy specific branches to a Pages site[](#deploy-specific-branches-to-a-pages-site "Permalink")

您可能只想从特定分支部署到 Pages 站点.

首先,添加`workflow`部分以强制管道仅在将更改推送到分支时才运行:

```
image: ruby:2.7

workflow:
  rules:
    - if: '$CI_COMMIT_BRANCH'

pages:
  script:
    - gem install bundler
    - bundle install
    - bundle exec jekyll build -d public
  artifacts:
    paths:
      - public 
```

然后将管道配置为仅运行 master 分支的作业.

```
image: ruby:2.7

workflow:
  rules:
    - if: '$CI_COMMIT_BRANCH'

pages:
  script:
    - gem install bundler
    - bundle install
    - bundle exec jekyll build -d public
  artifacts:
    paths:
      - public
  rules:
    - if: '$CI_COMMIT_BRANCH  ==  "master"' 
```

## Specify a stage to deploy[](#specify-a-stage-to-deploy "Permalink")

GitLab CI / CD 有三个默认阶段:构建,测试和部署.

如果要测试脚本并在部署到生产环境之前检查构建的站点,则可以完全按按`master`来运行测试.

要为您的作业指定一个阶段,请在您的 CI 文件中添加一个`stage`行:

```
image: ruby:2.7

workflow:
  rules:
    - if: '$CI_COMMIT_BRANCH'

pages:
  stage: deploy
  script:
    - gem install bundler
    - bundle install
    - bundle exec jekyll build -d public
  artifacts:
    paths:
      - public
  rules:
    - if: '$CI_COMMIT_BRANCH  ==  "master"' 
```

现在,将另一个作业添加到 CI 文件,告诉它测试**除** `master`分支**以外**的每个分支上的每次推送:

```
image: ruby:2.7

workflow:
  rules:
    - if: '$CI_COMMIT_BRANCH'

pages:
  stage: deploy
  script:
    - gem install bundler
    - bundle install
    - bundle exec jekyll build -d public
  artifacts:
    paths:
      - public
  rules:
    - if: '$CI_COMMIT_BRANCH  ==  "master"'

test:
  stage: test
  script:
    - gem install bundler
    - bundle install
    - bundle exec jekyll build -d test
  artifacts:
    paths:
      - test
  rules:
    - if: '$CI_COMMIT_BRANCH  !=  "master"' 
```

`test`作业在`test`阶段运行时,Jekyll 在名为`test`的目录中构建站点. 该工作影响除`master`之外的所有分支.

将阶段应用于不同的作业时,同一阶段中的每个作业都是并行构建的. 如果您的 Web 应用程序在部署之前需要多个测试,则可以同时运行所有测试.

## Remove duplicate commands[](#remove-duplicate-commands "Permalink")

为了避免在每个作业中重复相同的脚本,可以将它们添加到`before_script`部分.

在示例中, `gem install bundler``bundle install`对于作业, `pages``test`都在运行.

将这些命令移至`before_script`部分:

```
image: ruby:2.7

workflow:
  rules:
    - if: '$CI_COMMIT_BRANCH'

before_script:
  - gem install bundler
  - bundle install

pages:
  stage: deploy
  script:
    - bundle exec jekyll build -d public
  artifacts:
    paths:
      - public
  rules:
    - if: '$CI_COMMIT_BRANCH  ==  "master"'

test:
  stage: test
  script:
    - bundle exec jekyll build -d test
  artifacts:
    paths:
      - test
  rules:
    - if: '$CI_COMMIT_BRANCH  !=  "master"' 
```

## Build faster with cached dependencies[](#build-faster-with-cached-dependencies "Permalink")

为了加快构建速度,您可以使用`cache`参数为项目的依赖项缓存安装文件.

此示例在运行`bundle install`时将 Jekyll 依赖项缓存在`vendor`目录中:

```
image: ruby:2.7

workflow:
  rules:
    - if: '$CI_COMMIT_BRANCH'

cache:
  paths:
    - vendor/

before_script:
  - gem install bundler
  - bundle install --path vendor

pages:
  stage: deploy
  script:
    - bundle exec jekyll build -d public
  artifacts:
    paths:
      - public
  rules:
    - if: '$CI_COMMIT_BRANCH  ==  "master"'

test:
  stage: test
  script:
    - bundle exec jekyll build -d test
  artifacts:
    paths:
      - test
  rules:
    - if: '$CI_COMMIT_BRANCH  !=  "master"' 
```

在这种情况下,您需要从 Jekyll 构建的文件夹列表中排除`/vendor`目录. 否则,Jekyll 将尝试与站点一起构建目录内容.

在根目录中,创建一个名为`_config.yml`的文件并添加以下内容:

```
exclude:
  - vendor 
```

现在,GitLab CI / CD 不仅可以构建网站,还可以通过对功能分支的**连续测试**进行推送, **缓存**与 Bundler 一起安装的依赖项,并将每次推送**持续部署**`master`分支.

## Related topics[](#related-topics "Permalink")

有关更多信息,请参见以下博客文章.

*   [Use GitLab CI/CD `environments` to deploy your web app to staging and production](https://about.gitlab.com/blog/2016/08/26/ci-deployment-and-environments/).
*   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/).
*   了解[如何从不同项目中提取特定目录](https://about.gitlab.com/blog/2016/12/07/building-a-new-gitlab-docs-site-with-nanoc-gitlab-ci-and-gitlab-pages/)以部署此网站[https://docs.gitlab.com](https://s0docs0gitlab0com.icopy.site) .
*   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/).