GitLab CI/CD 的使用

GitLab CI/CD 使用

前置条件:html

  • 在 GitLab 中建立一个可用于持续继承的项目;
  • 拥有项目“维护者”或“拥有者”权限;
  • 有可用的 GitLab Runner 用于运行工做;
  • 在仓库的根目录建立一个 .gitlab-ci.yml 文件,用于定义工做内容;

检查可用的 GitLab Runner

在 GitLab 中,Runner 是运行持续集成工做内容的代理。git

查看可用的 GitLab Runner:Settings > CI/CD > Runnersdocker

有绿色圆圈的 Runner 即为处于激活状态。工具

install GitLab Runner gitlab

register a runnerui

建立 .gitlab-ci.yml 文件

.gitlab-ci.yml 文件用于配置具体的工做内容。代理

在文件中能够定义如下内容:code

  • Runner 应该执行的工做结构和顺序;
  • 遇到特定条件时,Runner 应作出的决策。

建立步骤:htm

  1. Project overview > Details;
  2. 选择提交的分支,点击+号,选择New file;
  3. 文件名 .gitlab-ci.yml ,填入示例代码;
  4. 点击 Commit changes。
build-job:
  stage: build
  script:
    - echo "Hello, $GITLAB_USER_LOGIN!"

test-job1:
  stage: test
  script:
    - echo "This job tests something"

test-job2:
  stage: test
  script:
    - echo "This job tests something, but takes more time than test-job1."
    - echo "After the echo commands complete, it runs the sleep command for 20 seconds"
    - echo "which simulates a test that runs 20 seconds longer than test-job1"
    - sleep 20

deploy-prod:
  stage: deploy
  script:
    - echo "This job deploys something from the $CI_COMMIT_BRANCH branch."
$GITLAB_USER_LOGIN$CI_COMMIT_BRANCH 均为运行时变量。
.gitlab-ci.yml 文件提示

查看 pipeline 和工做内容的状态

在提交修改后,一个 pipeline 将会被启动。继承

查看 pipeline :

  • CI/CD > Pipelines
  • 点击 pipeline ID,能够查看详情
  • 点击工做标题,能够查看详细的工做过程

若是工做状态为 stuck,请检查项目中是否有可用的 Runner 。

参考:https://docs.gitlab.com/ee/ci...

相关文章
相关标签/搜索