gitlab 多个ci yml文件配置
在前后端放在一个仓库或者多个子应用在同一个仓库 为每个子模块配置对应 .gitlab-ci.yml 可以使项目结构更清晰
在项目根目录引入子模块ci文件
.gitlab-ci.yml 设置如下 分别引入子模块
include:
- local: frontend/.gitlab-ci.yml
- local: backend/.gitlab-ci.yml
再在对应子模块中编写对应.gitlab-ci.yml 这里以vue前端和python后端示例,测试代码省略
frontend/.gitlab-ci.yml
default:
tags:
- node-build
stages:
- init
# - test
- build
- push
cache:
key: ${CI_BUILD_REF_NAME}
paths:
- frontend/node_modules/
frontend_init:
image: node:18.19.1-slim
stage: init
needs: []
before_script:
- cd frontend
- npm config set strict-ssl false
script:
- npm i --registry https://nexusoss.xxxx.com/repository/npm-group
only:
changes:
- package.json
# test:
# stage: test
# script:
# - npm run test
# only:
# - dev
frontend_build:
image: node:18.19.1-slim
stage: build
before_script:
- cd frontend
script:
- npm run build
artifacts:
paths:
- frontend/dist/
expire_in: 7 day
only:
- main
frontend_push:
image: docker:24.0.5
stage: push
needs: ['frontend_build']
before_script:
- ls -l /var/run/docker.sock
- mkdir -p /root/.docker
- echo '{"auths":{"'$HARBOR_URL'":{"auth":"'$HARBOR_AUTH'"}}}' > /root/.docker/config.json
- cat /root/.docker/config.json
- cd frontend
script:
- docker build -t ${HARBOR_URL}/${CI_PROJECT_NAME}/frontend-nginx:latest .
- docker push ${HARBOR_URL}/${CI_PROJECT_NAME}/frontend-nginx:latest
only:
- main
when: manual # 设置为手动触发
backend/.gitlab-ci.yml
default:
tags:
- docker-push
stages:
# - test
- push
# test:
# stage: test
# script:
# - npm run test
# only:
# - dev
backend_push:
image: docker:24.0.5
stage: push
needs: []
before_script:
- ls -l /var/run/docker.sock
- mkdir -p /root/.docker
- echo '{"auths":{"'$HARBOR_URL'":{"auth":"'$HARBOR_AUTH'"}}}' > /root/.docker/config.json
- cat /root/.docker/config.json
- cd backend
script:
- docker build -t ${HARBOR_URL}/${CI_PROJECT_NAME}/backend-api:latest .
- docker push ${HARBOR_URL}/${CI_PROJECT_NAME}/backend-api:latest
only:
- main
when: manual # 设置为手动触发
提示
配置其实很简单 和导包差不多
下面我分享一个需要注意事项 docker in docker 注意事项
docker in docker
当我们在docker容器中构建docker镜像时候 (上面push步骤) 我们需要额外配置 不然会权限不足构建失败 在Docker中实现Docker的三种方法:
- 通过挂载docker.sock(DooD方法)运行docker
- dind 方法
- 使用Nestybox sysbox Docker运行时
这里只分享 我最常用办法 第一种DooD
- 如果gitlab-runner 也是靠docker运行的需要先把 docker.sock挂载到 gitlab-runner
提示
要是gitlab-runner 运行在物理机中 此步骤可以省略
/var/run/docker.sock:/var/run/docker.sock
- 正常注册完runner
gitlab-runner register
- 为对应runner 挂载docker.sock
vi /etc/gitlab-runner/config.toml
[runners.docker]
tls_verify = false
image = "docker:24.0.5"
privileged = false
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
volumes = ["/var/run/docker.sock:/var/run/docker.sock","/cache"]
shm_size = 0