docker搭建gitlab,集成CI/CD(gitlab-runner)

发布时间 2023-10-13 18:26:55作者: 郭小睿

搭建gitlab和gitlab-runner

首先配置GITLAB_HOME

export GITLAB_HOME=/srv/gitlab

如果你是在BASH下,可以将以上配置放到~/.bash_profile文件中,使用source ~/.bash_profile使其永久生效。

docker run --detach \
  --hostname 192.168.1.205 \
  --publish 9443:443 --publish 9080:9080 --publish 9022:22 \
  --name gitlab \
  --restart always \
  --volume $GITLAB_HOME/config:/etc/gitlab:Z \
  --volume $GITLAB_HOME/logs:/var/log/gitlab:Z \
  --volume $GITLAB_HOME/data:/var/opt/gitlab:Z \
  --shm-size 256m \
  gitlab/gitlab-ee:latest
docker run -d --name gitlab-runner --restart always \
  -v /srv/gitlab-runner/config:/etc/gitlab-runner \
  -v /var/run/docker.sock:/var/run/docker.sock \
  gitlab/gitlab-runner:latest

查看gitlab默认密码

sudo docker exec -it gitlab grep 'Password:' /etc/gitlab/initial_root_password

注册Runner

gitlab-runner容器内注册

gitlab-runner register

# 输入Gitlab实例的地址
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
http://192.168.1.200

# 输入token
Please enter the gitlab-ci token for this runner
xxxxxxxx

# 输入描述信息
Enter a description for the runner:
[2f5631485bf3]: this is the description

# 输入tag
Enter tags for the runner (comma-separated):
test_tag

# 输入Ruuner的执行者
Enter an executor: docker-ssh, ssh, virtualbox, docker-ssh+machine, kubernetes, custom, docker, parallels, shell, docker+machine:
shell

# 如果上面executor为docker,需要你在后续项目根部的.gitlab-ci.yml中  指定具体docker版本
#Enter the default Docker image (for example, ruby:2.6):
#alpine:latest

token,登陆gitlab管理后台后查看。

注册后,会自动上线到gitlab中。

创建工作流

项目根目录下创建 .gitlab-ci.yml 文件,内容如下:

cache:
  paths:
    - node_modules/
    - dist/

stages:
  - build_deploy

variables:
  NPM_REGISTRY: "https://registry.npmmirror.com/"
  REMOTE_SERVER: "root@192.168.1.205:/www/wwwroot/xxx.xxx.com"
  
before_script:
  - npm config set registry ${NPM_REGISTRY}

build:
  stage: build_deploy
  script:
    - npm install  # 执行 npm install 安装依赖
    - npm run build  # 执行 npm run build
    - sshpass -p 12345678 scp -o StrictHostKeyChecking=no -r dist/* $REMOTE_SERVER # 提交部署
    - echo 'success!'

提交并触发CI/CD流程

git add .gitlab-ci.yml
git commit -m "Add CI/CD configuration"
git push origin master

查看已注册

cat /etc/gitlab-runner/config.toml

避免重复拉取镜像

/etc/gitlab-runner/config.tomlrunners.docker中添加pull_policy = "if-not-present"

gitlab修改克隆地址

方法1

修改/opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml文件,修改gitlab下的hostport

修改完成后执行gitlab-ctl restart使配置生效
缺点:reconfigure后配置失效,需要重新配置。

方法2

修改/etc/gitlab/gitlab.rb,修改external_url

修改完成后执行gitlab-ctl reconfigure使配置生效

因为是docker部署,由于内外部映射了不同端口,http监听端口可能不是80,

需要修改文件中 nginx['listen_port']=xxx 到实际的监听端口,否则gitlab无法正常启动。

Gitlab-Runner环境配置

使用docker拉起的环境是ubuntu,我们安装软件可以使用apt install

我这个项目是nodejs的,索性把安装nodejs的步骤贴到这里。

apt update
apt install nodejs
node -v
apt install npm

在Ubuntu 20.04默认的软件仓库中,提供了一个版本为10.19的Node.js。这个版本已经比较老了。也可以采用下面的方式安装。

curl -sL https://deb.nodesource.com/setup_16.x -o nodesource_setup.sh
bash nodesource_setup.sh
apt install nodejs
node -v

推荐博文

GitLab-CI中的artifacts使用研究:http://zacksleo.top/archives/

Gitlab CI 使用高级技巧:https://www.jianshu.com/p/3c0cbb6c2936

一文搞定gitlab的环境搭建、配置CI/CD、自动构建docker镜像:https://www.cnblogs.com/hzhhhbb/p/13966904.html?share_token=4dfe4dbe-caac-4437-b2b4-ea59b03c67d1

gitlab CI/CD持续集成 https://www.cnblogs.com/linagcheng/p/14707967.html

docker安装gitlab-runner https://www.cnblogs.com/lvlinguang/p/15191669.html