一、安装 gitlab
1.启动
docker pull gitlab/gitlab-ce:15.8.3-ce.0
docker run -d --name gitlab_1 -e TZ=Asia/Shanghai -p 80:80 -p 22:22 gitlab/gitlab-ce:15.8.3-ce.0
管理员是 root,初始密码在 /etc/gitlab/initial_root_password
2.修改配置文件
修改 docker 部署 gitlab 的外部访问地址和端口, ssh 和 http 地址都会变化
修改 /etc/gitlab/gitlab.rb
的 external_url
external_url 'http://gitlab.example1.com'
二、安装 gitlab-runner
1.启动
docker pull gitlab/gitlab-runner:ubuntu-v15.8.3
docker run -d --name gitlab_runner_1 -e TZ=Asia/Shanghai -v /var/run/docker.sock:/var/run/docker.sock -v /data/etc/gitlab-runner:/etc/gitlab-runner gitlab/gitlab-runner:ubuntu-v15.8.3
2.注册
在 gitlab 项目下的 Settings -> CI/CD -> Runners -> Specific runners,可以查看到注册的 token
进入 gitlab-runner 容器,执行 gitlab-runner register
进行注册,会把信息写入配置文件 /etc/gitlab-runner/config.toml
中,过程如下:
root@8d635554c227:~# gitlab-runner register
Runtime platform arch=amd64 os=linux pid=102 revision=080abeab version=15.8.3
Running in system-mode.
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://gitlab.example1.com/
Enter the registration token:
GR1348941Lk9BFxzEEyvzyZVbjxWH
Enter a description for the runner:
[8d635554c227]: my-runner-name
Enter tags for the runner (comma-separated):
my-tag
Enter optional maintenance note for the runner:
zhangsan
WARNING: Support for registration tokens and runner parameters in the 'register' command has been deprecated in GitLab Runner 15.6 and will be replaced with support for authentication tokens. For more information, see https://gitlab.com/gitlab-org/gitlab/-/issues/380872
Registering runner... succeeded runner=GR1348941Lk9BFxzE
Enter an executor: ssh, kubernetes, docker, docker-ssh, shell, virtualbox, docker+machine, docker-ssh+machine, instance, custom, parallels:
docker
Enter the default Docker image (for example, ruby:2.7):
alpine:latest
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"
3.修改配置文件
修改 gitlab-runner 的配置文件 /etc/gitlab-runner/config.toml
因为使用 docker 运行 job,和使用 maven 构建项目,需要配置 network_mode = "host
和 volumes = ["/data/cache:/cache", "/var/run/docker.sock:/var/run/docker.sock", "/home/zxm/.m2:/root/.m2"]
,
network_mode = "host
与宿主机共享网络
volumes
配置与宿主机映射
root@8d635554c227:/# cat /etc/gitlab-runner/config.toml
concurrent = 1
check_interval = 0
user = "gitlab-runner"
shutdown_timeout = 0
[session_server]
session_timeout = 1800
[[runners]]
name = "my-runner"
url = "http://gitlab.example1.com/"
id = 1
token = "iGyXWrRTvjPf8X6ue9Ys"
token_obtained_at = 2023-02-25T16:23:53Z
token_expires_at = 0001-01-01T00:00:00Z
executor = "docker"
[runners.custom_build_dir]
[runners.cache]
MaxUploadedArchiveSize = 0
[runners.cache.s3]
[runners.cache.gcs]
[runners.cache.azure]
[runners.docker]
tls_verify = false
image = "alpine:latest"
privileged = false
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
volumes = ["/data/cache:/cache", "/var/run/docker.sock:/var/run/docker.sock", "/home/zxm/.m2:/root/.m2"]
network_mode = "host"
shm_size = 0
gitlab-runner list
列出所有的 runnergitlab-runner register
注册 runnergitlab-runner unregister --all-runners
取消注册所有的 runnergitlab-runner start
gitlab-runner stop
gitlab-runner restart
gitlab-runner status
4.设置 runner
进入 Settings -> CI/CD -> Runners -> Specific runners,编辑具体的 runner,勾选 Run untagged jobs
三、编写 .gitlab-ci.yml 文件
在项目下增加文件 .gitlab-ci.yml
,内容如下:
- variables:定义变量
- cache:runner提供的缓存功能。Cache 在使用时制定一系列的文件或者文件目录,使得其在不同的 job 之间被缓存下来。这样当某一个 job 需要依赖于之前步骤产生的一些文件结果,Cache 就帮助我们在上一个 job 将产生的结果缓存下来并且在当前的 job 进行使用
- artifacts:用于指定在 job 成功或失败时应附加到作业的文件和目录的列表作业完成后,工件将被发送到 gitlab,并可在 gitlab ui 中下载
variables:
PROJECT_NAME: ${CI_PROJECT_NAME} # 项目名
COMMIT_INFO: ${CI_COMMIT_REF_SLUG}_${CI_COMMIT_SHORT_SHA} # 提交分支_提交SHA
FULL_NAME: ${CI_PROJECT_NAME}_${CI_COMMIT_REF_SLUG}_${CI_COMMIT_SHORT_SHA}_${CI_PIPELINE_CREATED_AT} # 项目名_提交分支_提交SHA_流水线创建时间
DOCKER_IMAGE: ${CI_PROJECT_NAME}:${COMMIT_INFO} # 项目名:提交分支_提交SHA
CONTAINER_NAME: ${CI_PROJECT_NAME}_${CI_COMMIT_REF_SLUG}_${CI_COMMIT_SHORT_SHA} # 项目名_提交分支_提交SHA
cache:
key: ${COMMIT_INFO}
paths:
- target/
stages:
- maven_build
- docker_build
- docker_deploy
maven_build_job:
image: maven:3.6.0-jdk-8-alpine
stage: maven_build
script:
- mvn clean package -Dmaven.test.skip=true
artifacts:
name: jar_${FULL_NAME}
expire_in: 7 days
paths:
- target/*.jar
docker_build_job:
image: docker:stable
stage: docker_build
script:
- docker build -t ${DOCKER_IMAGE} .
- docker save -o ${FULL_NAME}.tar ${DOCKER_IMAGE}
artifacts:
name: docker_${FULL_NAME}
expire_in: 7 days
paths:
- ./*.tar
docker_deploy_job:
image: docker:stable
stage: docker_deploy
script:
- docker ps -a --filter name=${PROJECT_NAME}
- CONTAINER_IDS=$(docker ps -aq --filter name=${PROJECT_NAME})
- echo ${CONTAINER_IDS}
- if [[ -n "$CONTAINER_IDS" ]]; then
docker rm -f $CONTAINER_IDS;
fi
- docker run -d --name ${CONTAINER_NAME} -p 8080:8080 ${DOCKER_IMAGE}
四、编写 Dockerfile 文件
FROM openjdk:8u191-jre-alpine3.9
MAINTAINER zxm <zxm-2018@qq.com>
ENV APPLICATION_NAME=spring-boot-demo
ARG JAVA_OPTS="-Xmx128M -Xms128M -Xmn64M -XX:MaxMetaspaceSize=64M -XX:MetaspaceSize=64M -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp/heapdump.hprof"
RUN mkdir /opt/app
COPY target/*.jar /opt/app/app.jar
WORKDIR /opt/app
EXPOSE 8080
ENTRYPOINT exec java ${JAVA_OPTS} -jar /opt/app/app.jar