gitlab ci配置 以自动生成docker镜像为例
通过linux shell 方式安装runner
ci 模板:https://docs.gitlab.com/ci/examples/
gitlab创建一个项目
...
gitlab runner设置页面
打开http://xx/root/p/-/settings/ci_cd#js-runners-settings
获取注册runner命令
在linux主机执行
安装runner
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash && apt install gitlab-runner
#选择使用shell方式
执行注册runner命令
gitlab-runner register --url https://gitlab.com --token glrt-xxx
#方式选shell
#可打一个标签tag1
安装docker
略
docker 拉取一个nginx镜像
gitlab 查看runner情况
配置.gitlab-ci.yml
.gitlab-ci.yml 、Dockerfile 文件放置于项目根目录
test/内为html网页文件
根目录结构:
-rw-r--r-- 1 x 197121 65 Aug 19 09:44 Dockerfile
-rw-r--r-- 1 x 197121 6184 Aug 19 09:03 README.md
drwxr-xr-x 1 x 197121 0 Aug 19 09:46 test/
.gitlab-ci.yml内容:
stages:
- build
build-and-run-nginx:
stage: build
script:
# 构建镜像,使用项目目录下的 Dockerfile
- docker build -t my-nginx-app:latest .
# 停止并删除旧容器(如果存在)
- docker stop my-nginx || true
- docker rm my-nginx || true
# 启动新容器
- docker run -d -p 8080:80 --name my-nginx my-nginx-app:latest
tags:
- tag1
only:
- main
# 可选:指定 tags(如果你有多个 Runner,可以打标签区分)
# tags:
# - shell-runner
Dockerfile 内容:
FROM nginx:latest
COPY test/ /usr/share/nginx/html/
EXPOSE 80
测试
代码更新后推送到仓库,ci流程自动触发执行
可通过页面查看任务进度
http://xx/root/p/-/pipelines
执行成功后会在runner主机上生成docker镜像 my-nginx-app:latest,并运行容器