gitlab安装及ci/cd实战

gitlab 部署

# 拉取镜像
docker pull gitlab/gitlab-ce
# 运行镜像
docker run -d -p 443:443 -p 80:80 -p 222:22 --name gitlab --restart always -v /opt/gitlab/config:/etc/gitlab -v /opt/gitlab/log:/var/log/gitlab -v /opt/gitlab/data:/var/opt/gitlab gitlab/gitlab-ce
# 修改配置文件
vim /opt/gitlab/config/gitlab.rb 

external_url 'http://192.168.6.21'
gitlab_rails['gitlab_ssh_host'] = '192.168.6.21'
gitlab_rails['gitlab_shell_ssh_port'] = 222
# 重启容器
docker restart gitlab

gitlab执行器Runner搭建

安装Runner执行器

# 下载执行器
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
# 赋予文件夹权限
chmod +x /usr/local/bin/gitlab-runner
# 新建操作用户
useradd --comment 'gitlab-runner' --create-home gitlab-runner --shell /bin/bash
# 添加 docker 用户组
usermod -a -G docker gitlab-runner
# gitlab-runner 作为管理员执行
sudo visudo -f /etc/sudoers.d/gitlab-runner
gitlab-runner ALL=(ALL) NOPASSWD: ALL
# 安装
gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
# 启动
gitlab-runner start
# 验证
gitlab-runner verify 

注册Runner执行器

gitlab-runner register 

选择shell 
It supposes you have configured a shell Runner and have installed Docker.

编写gitlab.yaml CI配置文件

## 注意tags与注册器的保持一致
stages:
  - build
  - deploy

build_app:
  stage: build
  dependencies: []
  tags:
  - test
  script:
  - docker run -i --rm --name hello-maven -v ${PWD}:/hello -w /hello maven
      mvn clean install
  - cp target/hello.war hello.war
  - docker run -i --rm --name hello-maven -v ${PWD}:/hello -w /hello maven
      mvn clean
  artifacts:
    paths:
    - hello.war
    expire_in: 1 week

deploy:stand:
  stage: deploy
  dependencies:
  - build_app
  tags:
  - test
  script:
  - docker run -d --rm --name hello-tomcat-${CI_COMMIT_SHA:0:8} -P
      -v ${PWD}/hello.war:/usr/local/tomcat/webapps/hello.war   
      tomcat:9.0-jre8-alpine
  - docker ps -f "name=hello-tomcat-${CI_COMMIT_SHA:0:8}" --format '{{.Ports}}'

deploy:prod:
  stage: deploy
  when: manual
  dependencies:
  - build_app
  tags:
  - test
  script:
  - docker run -d --rm --name hello-tomcat-${CI_COMMIT_SHA:0:8} -P
      -v ${PWD}/hello.war:/usr/local/tomcat/webapps/hello.war   
      tomcat:9.0-jre8-alpine
  - docker ps -f "name=hello-tomcat-${CI_COMMIT_SHA:0:8}" --format '{{.Ports}}'

FAQ

部署gitlab后502错误

docker exec -it gitlab bash # 进入容器内部
gitlab-ctl restart sidekiq
gitlab-ctl hup unicorn

第一构建完成,第二次构建失败

fatal: git fetch-pack: expected shallow list 
fatal: The remote end hung up unexpectedly

更新git版本

yum install http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-2.noarch.rpm
#安装git
yum install git
#更新git
yum update git

构建日志太大无法输出

Job's log exceeded limit of 4194304 bytes.

进入/etc/gitlab-runner/config.toml

output_limit = 10240

引用

https://www.cxymm.net/article/skyyemperor/114897880
https://gainanov.pro/eng-blog/devops/gitlab-ci-create-first-pipeline/

gitlab rigister

https://docs.gitlab.com/runner/register/

wsl2设置代理

https://zinglix.xyz/2020/04/18/wsl2-proxy/

ci.yaml 文件

https://meigit.readthedocs.io/en/latest/gitlab_ci_.gitlab-ci.yml_detail.html

maven 设置代理

https://stackoverflow.com/questions/30522679/plugin-org-apache-maven-pluginsmaven-clean-plugin2-5-or-one-of-its-dependencie

posted @ 2022-01-02 11:14  gxhao  阅读(223)  评论(0编辑  收藏  举报