Gitlab-CICD

围巾🧣 2021年09月17日 543次浏览

Gitlab安装

sudo docker pull gitlab/gitlab-ce:latest

sudo docker run --detach \
  --publish 10443:443 --publish 1080:80 --publish 1022:22 \
  --name gitlab \
  --restart always \
  --volume /mydata/gitlab/config:/etc/gitlab \
  --volume /mydata/gitlab/logs:/var/log/gitlab \
  --volume /mydata/gitlab/data:/var/opt/gitlab \
  gitlab/gitlab-ce:latest

配置:略

重置密码

  1. 进入容器

  2. gitlab-rails console
    Loading production environment (Rails 4.2.10)
    irb(main):001:0> user=User.where(id:1).first
    => #<User id:1 @root>
    irb(main):003:0> user.password='9aa9871ouf906'
    => "9aa9871ouf906"
    irb(main):004:0> user.password_confirmation='9aa9871ouf906'          
    => "9aa9871ouf906"
    irb(main):005:0> user.save!
    Enqueued ActionMailer::DeliveryJob (Job ID: 55d5938a-8506-49cc-be8c-6afd666c4efe) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", gid://gitlab/User/1
    => true
    irb(main):006:0> 
    

Gitlab-runner

sudo docker pull gitlab/gitlab-runner:latest

sudo 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

sudo docker exec -it gitlab-runner /bin/bash
gitlab-ci-multi-runner register

配置过程

选择的是 shell 执行器

image-20210318211954065

配置.gitlab-ci.yml

gitlab-runner

由于配置是shell的运行方式,执行流程是这样的:

  1. runner 容器里面安装 jdk maven docker

    apt install openjdk-8-jdk docker.io maven
    

    maven 配置阿里云镜像加速

    # 查位置
    whereis maven
    cd /etc/maven
    vim setting.xml
    

    mirrors添加如下内容

    <mirrors>
        <mirror>  
            <id>alimaven</id>  
            <mirrorOf>central</mirrorOf>  
            <name>aliyun maven</name>  
            <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>  
        </mirror>
    </mirrors>
    
  2. 使用 maven 编译 jar

  3. 用生成的 jar 和 Dockerfile 打包镜像,部署镜像到宿主机,看 .gitlab-ci.yml 文件内容:工作都在 build 处理了

    stages:
      - build
      - test
      - deploy
    
    before_script:
      - echo "Restoring Packages..."
    
    build_job:
      stage: build
      only:
        - master
      tags:
        - jin
      script:
        - echo "The project build..."
        - mvn clean
        - mvn package
        - chmod 775 nacos-consumer.sh
        - echo "$PASSWORD"|sudo -S ./nacos-consumer.sh
    
    test_job:
      stage: test
      only:
        - master
      tags:
        - jin
      script:
        - echo "Tests run..."
        - ls .
    
    deploy_job:
      stage: deploy
      only:
        - master
      tags:
        - jin
      script:
        - echo "Deploy run..."
        - ls .
    

    shell 文件:

    #!/usr/bin/env bash
    # 定义应用组名
    group_name='stuv'
    # 定义应用名称
    app_name='nacos-consumer'
    # 定义应用版本
    app_version='0.0.1-SNAPSHOT'
    # 定义应用环境
    profile_active='qa'
    sudo docker stop ${app_name}
    echo '----stop container----'
    sudo docker rm ${app_name}
    echo '----rm container----'
    sudo docker rmi ${group_name}/${app_name}:${app_version}
    echo '----rm image----'
    # 打包编译docker镜像,在当前路径
    sudo docker build -t ${group_name}/${app_name}:${app_version} .
    echo '----build image----'
    sudo docker run -p 20012:20012 --name ${app_name} \
    -e 'spring.profiles.active'=${profile_active} \
    -e TZ="Asia/Shanghai" \
    -v /etc/localtime:/etc/localtime \
    -v /mydata/app/${app_name}/logs:/var/logs \
    -d ${group_name}/${app_name}:${app_version}
    echo '----start container----'
    
    

shell脚本中针对sudo等密码输入的问题解决方案

  1. 项目CICD设置中配置PASSWORD变量
  2. echo "$PASSWORD"|sudo -S 命令