Open wuyuedefeng opened 4 years ago
Dockerfile
FROM gitlab/gitlab-runner:v11.8.0
ARG build_env=staging
WORKDIR /app
COPY ./ca.crt /app
构建镜像
$ docker build -t staging-gitlab-runner:v1 .
启动容器
$ docker run \
# --add-host git.51xxx.com:134.96.1xx.28 \
-d --name staging-gitlab-runner \
--restart always \
-v /data/gitlab-runner/config:/etc/gitlab-runner \
-v /var/run/docker.sock:/var/run/docker.sock \
staging-gitlab-runner:v1
注册runner
docker exec -it staging-gitlab-runner gitlab-runner register \
--non-interactive \
--tls-ca-file /app/ca.crt \
--url https://git.51xxx.com/ \
--registration-token Ef4TsExLAs3TqsGj6Kxx \
--tag-list stagingRunInDk \
--description "stagingRunInDk" \
--executor docker \
--docker-image "docker:lastest" \
--docker-privileged \
--docker-volumes /var/run/docker.sock:/var/run/docker.sock
或者进入容器中执行
# 进入docker容器内
$ docker logs -f aac29379c413
$ docker exec -it aac29379c413 /bin/bash
# 注册runner, 可添加自定义ca证书 eg: $ gitlab-runner register --tls-ca-file /app/ca.crt
# docker executor: gitlab-runner register --tls-ca-file /app/ca.crt --docker-privileged
$ gitlab-runner register
文档 【executor docker】如何使用docker和gitlab-runner自动化部署node项目
.gitlab-ci.yml
stages: # 定义Pipeline的阶段,可以多个
- test
- deploy
- try_version
# 内部没定义before_script的stage才会使用这个
before_script: # 定义在job执行之前的执行脚本(就一次)
- echo "hello gitlab ci"
# 添加mirrors镜像, 加速apk安装插件
# - echo -e "https://mirrors.ustc.edu.cn/alpine/latest-stable/main\nhttps://mirrors.ustc.edu.cn/alpine/latest-stable/community" > /etc/apk/repositories && apk update
#- apk --update add nodejs # 安装nodejs
- npm install --registry=https://registry.npm.taobao.org
# 部分需要重复利用又被gitignore的文件
cache:
paths:
- node_modules/
test_develop:
image: node:12.6.0-alpine
stage: test
script:
- npm test
# 在哪个分支上可用
only:
- develop
# 指定哪个ci runner跑该工作
tags:
- stagingRunInDk
deploy_develop:
stage: deploy
#variables:
# GIT_SSL_NO_VERIFY: 1
before_script: # 定义在job执行之前的执行脚本(就一次)
- echo "gitlab ci deploy"
- docker rm -f wechat-auth-service
# 备份老的镜像
- docker tag wechat-auth-service:v1.0 wechat-auth-service:v1.0.old
# 删除镜像操作,可以使用Docker Hub远程管理镜像的版本,而不是删除,这里只是为了方便
- docker rmi wechat-auth-service:v1.0
script:
- docker build -t wechat-auth-service:v1.0 .
- docker run -it -d -p 3333:3333 --name=wechat-auth-service:v1.0
# 在哪个分支上可用
only:
- develop
# 指定哪个ci runner跑该工作
tags:
- stagingRunInDk
try_production_version:
image: node:12.6.0-alpine
stage: try_version
script:
- export CI_NODE_VERSION=$(node -e "(function () { console.log(require('./package.json').version) })()")
- echo "$CI_NODE_VERSION"
# 在哪个分支上可用
only:
- master
# 指定哪个ci runner跑该工作
tags:
- stagingRunInDk
.gitlab-ci.yml
中使用docker-compose
部署命令image: docker/compose:last
内集成了docker-compose
命令
# https://docs.gitlab.com/ee/ci/docker/using_docker_images.html
stages: # 定义Pipeline的阶段,可以多个
- deploy
image:
name: docker/compose:latest
pull: always # available: always, if-not-present, never [defualt: always](https://gitlab.com/gitlab-org/gitlab-runner/-/issues/3279)
# entrypoint: ["/bin/sh", "-c"]
# variables:
# DOCKER_DRIVER: overlay
# services:
# - docker:dind
before_script: # 定义在job执行之前的执行脚本(就一次)
- echo "hello gitlab ci"
- docker version
- docker-compose version
deploy_staging_v3:
stage: deploy
variables:
GIT_SSL_NO_VERIFY: 1
DOCKER_DRIVER: overlay
before_script:
# https://www.cnblogs.com/sunsky303/p/11548343.html
- echo -e "https://mirrors.ustc.edu.cn/alpine/latest-stable/main\nhttps://mirrors.ustc.edu.cn/alpine/latest-stable/community\nhttps://mirror.tuna.tsinghua.edu.cn/alpine/edge/testing\nhttps://mirror.tuna.tsinghua.edu.cn/alpine/edge/main" > /etc/apk/repositories && apk update
# Install Docker Compose
# - apk update
# - apk upgrade
- apk add nodejs npm
- npm config set registry http://registry.cnpmjs.com
- npm install -g cnpm --registry=https://registry.npm.taobao.org
- cd ./npfrontend
- cnpm install #--registry=https://registry.npm.taobao.org
- npm run build:all
- cd ..
- cd ./backend
- echo -e "fca33744418a210e95d73b7ecexxxxfc" > ./config/master.key
- docker build -t np_staging2 .
- cd ..
script:
- echo $PWD
- docker-compose rm -sf
- docker-compose up -d
# 在哪个分支上可用
only:
- v3-ci
# 指定哪个ci runner跑该工作
tags:
- stagingRunInDk
配置CI/CD
配置runner
/data/gitlab-runner/config
(后续volumn到docker的配置目录)新建config.toml
crt
和pem
只是文件结尾不同,重命名即可, 参考自: https://www.fujieace.com/jingyan/pem-crt-key.htmlhttps://blog.csdn.net/qq_34206560/article/details/88802893 https://stackoverflow.com/questions/55622960/gitlab-runner-x509-certificate-signed-by-unknown-authority