Gitlab-CI/CD 2

Gitlab-Runner自动构建服务器搭建2

 

注册Runner

上一节我们创建了自己的gitlab-runner镜像,并使用docker-compose up -d --build命令运行了一个名为gitlab-runner-compose的容器;

1、执行register命令注册runner;

docker exec -it gitlab-runner-compose gitlab-runner-andy register

2、输入你自己项目仓库所在gitlab实例的URL:

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
https://gitlab.com

3、输入用于注册Runner的token,从gitlab网站中获得:

Enter the token you obtained to register the Runner:
*********

4、输入Runner的描述信息,之后也可以在gitlab的网站中修改它:

Please enter the gitlab-ci description for this runner
[hostname] nodejs-runner

5、输入【与Runner关联的标签】, 之后也可以在gitlab的网站中修改:

Please enter the gitlab-ci tags for this runner (comma separated):
webapi-tag,another-tag

6、选择 runner 执行器,这里选择的是 shell

Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
shell

备注:

上述2、3中的URL、TOKEN获取——项目/Settings/CI_CD/Runners

 

编写.gitlab-ci.yml

在项目的根目录编写.gitlab-ci.yml文件:

stages:
  - pre_build
  - build
  - deploy

before_script:
  - echo "before_script started."
  
  - docker login --username=${docker_username} --password=${docker_pwd} registry.cn-hangzhou.aliyuncs.com
  
  - echo "end before_script."
# This folder is cached between builds
# http://docs.gitlab.com/ce/ci/yaml/README.html#cache
cache:
  paths:
    - node_modules/
    - dist/

pre_build:
  stage: pre_build
  script:
    - echo "start pre_build."
    
    - npm install
    - CI=false npm run build
    
    - echo "end pre_build."
  only:
    - master
    
  tags:
    - webapi-tag

build:
  stage: build
  script:
    - echo "start build."
    
    - docker build -t registry.cn-hangzhou.aliyuncs.com/***/<image>:$CI_COMMIT_SHORT_SHA .
    - docker push registry.cn-hangzhou.aliyuncs.com/***/<image>:$CI_COMMIT_SHORT_SHA
    
    - echo "end build."
  only:
    - master  
  tags:
    - webapi-tag

deploy_job:
  stage: deploy
  script:
    - echo "start deploy."
    
    - docker pull registry.cn-hangzhou.aliyuncs.com/***/<image>:$CI_COMMIT_SHORT_SHA
    - docker stop <container> && docker rm <container>
    - docker run -id -e NODE_ENV=$NODE_ENV -e MONGODB_URI=$MONGODB_URI -e LAVKEY=$LAVKEY -e LAVSECRET=$LAVSECRET -e LAVHOST=$LAVHOST -e LAVCOUNTRY=$LAVCOUNTRY -e DISH=$DISH -e DISP=$DISP -e DISPWD=$DISPWD -p 3000:3000 --mount source=***-vol,target=/usr/src/app/logs --restart=unless-stopped --name=<container> registry.cn-hangzhou.aliyuncs.com/***/<image>:$CI_COMMIT_SHORT_SHA

    - echo "end deploy."
  only:
    - master
  tags:
    - webapi-tag

上面的yml文件把一次 Pipeline 分成了三个阶段:

安装依赖、编译(pre_build):npm install、npm run build 构建docker镜像(build):docker build ……、docker push …… 部署到服务器(deploy_job):docker pull …… 、docker run ……

stages:定义构建阶段,这里定义了三个阶段,分别对应每个阶段的stage属性; script:需要执行的 shell 脚本; only:only 参数中只有master,意思是只有master分支有提交的时候才会触发相关的 Jobs; tags:与注册 Runner 时输入的 tag进行匹配。

测试集成效果:

所有操作完成后push代码到仓库的master分支,查看结果:

passed表示所有stage执行成功。

failed表示失败,进入Josbs可以查看log进行排错:

 

posted @ 2020-08-19 22:49  冷风冷雨  阅读(217)  评论(0编辑  收藏  举报