Gitlab-CI前端shell形式示范

/ Linux服务安装搭建 / 没有评论 / 1870浏览

environment

Gitlab 安装配置好 192.168.0.165

runner机器 nginx+node 192.168.0.166

install runner

地址:https://docs.gitlab.com/runner/install/

install runner

root@paa2:/tmp# wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
root@paa2:/tmp# chmod +x /usr/local/bin/gitlab-runner
root@paa2:/tmp# gitlab-runner run

create runner user

# useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

check runner

root@paa2:/tmp# which gitlab-runner  && echo $?
/usr/local/bin/gitlab-runner
0

gitlab ui setting

选择项目===》settings ===》Auto DevOps 打开Default to Auto DevOps pipeline ==>> Runners ==>> 选择手动设置runner ==》找到URL与注册Token

配置

Token位置,后面注册会用到

runner register

root@paa2:~# gitlab-runner register
Runtime platform                                    arch=amd64 os=linux pid=17878 revision=8bb608ff version=11.7.0
Running in system-mode.

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://192.168.0.165/
Please enter the gitlab-ci token for this runner:
sJNme2oxBYsbb9CQPvxZ
Please enter the gitlab-ci description for this runner:   #输入该 Runner 的描述
[paa2]: front
Please enter the gitlab-ci tags for this runner (comma separated): #给该 Runner 指派 tags, 稍后也可以在 GitLab's UI 修改
front
Registering runner... succeeded                     runner=sJNme2ox
Please enter the executor: docker+machine, docker-ssh+machine, parallels, shell, ssh, virtualbox, kubernetes, docker, docker-ssh:
[true]: shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

注册完之后能看到runner是否活着,green色为活着

.gitlab-ci.yml

Pipeline

一次 Pipeline 其实相当于一次构建任务,里面可以包含多个流程,如安装依赖、运行测试、编译、部署测试服务器、部署生产服务器等流程。我们的任何提交或者 Merge Request 的合并都可以触发 Pipeline。如下图

Stages

Stages 表示构建阶段,说白了就是上面提到的流程。 我们可以在一次 Pipeline 中定义多个 Stages,每个Stage可以完成不同的任务。 Stages有下面的特点:

Jobs

Jobs 表示构建工作,表示某个 Stage 里面执行的工作。 我们可以在 Stages 里面定义多个 Jobs,这些 Jobs 会有以下特点:

如图

Example

before_script:
  - echo "begin"
  - cd /git/front
  - git pull

after_script:
  - echo "end"

stages:
  - Test
  - Build

# 构建阶段1
job1:
  stage: Test
  script:
    - npm install 
  tags:
    - front
# 构建阶段2
job2:
  stage: Test
  script:
    - service nginx reload 
  tags:
    - front    

demo