gitlab-ci的权限问题

利用gitlab-runner在gitlab上完成CI/CD遇见的权限坑

需要在gitlab上建立一个用户,例如:gitlab-runner用户

再将此用户加入需要CI/CD的项目中

如果需要需要登录其他服务器执行shell,则需要把gitlab-runner容器中的gitlab-runner用户的公钥复制到远端主机,并登录一次。

学习gitlab-runner

在项目根目录构造一个.gitlab-ci.yml文件。当开发和运维分开时,此文件必须被版本控制管理

现在本项目注册两个runner,tags名为:myproject,mycode

.gitlab-ci.yml的基本结构

## 定义流水线的阶段,可以自己定义名称,用来管理作业流程,默认stages为:build,test,deploy
## 作业顺序就是按照stages定义的顺序,自上而下的执行
## 不管job的编写顺序如何,只按照job内定义的stage顺序执行。
## 不同的job,使用同一个stage,会并行执行
stages:
    - build
    - test
    - deploy
## 第一条作业
job1:
    stage: build ## 指定作业阶段
    tags:   ## 指定作业的runner
        - mycode
    script:  ## 作业执行的脚本
        - "ssh root@目标主机 'sh /opt/data/git pull;exit'"
    only:    ## 针对哪个分支执行
        - dev
job2:
    stage: deploy
    tags: ## 一条流水线可以定义不同的tags
        - myproject
    script: ## 作业执行的脚本
        - "ssh root@目标主机 '/usr/bin/docker-compose -f /opt/data/docker-compose.yml'"
    only:
        - dev

补充:

runner的定义,其实是跟着项目走的,一个项目一个runner比较好

gitlab-server的备份迁移

gitlab-server基于docker搭建,备份迁移命令组如下:

## 备份
### 12.2以后版本
docker exec -t <container name> gitlab-backup create
### 12.1以前版本
docker exec -t <container name> gitlab-rake gitlab:backup:create

在backups目录里面,有名为xxx_gitlab_backup.tar
的文件

## 恢复
### 12.2以后版本
# Stop the processes that are connected to the database
docker exec -it <name of container> gitlab-ctl stop unicorn
docker exec -it <name of container> gitlab-ctl stop puma
docker exec -it <name of container> gitlab-ctl stop sidekiq

# Verify that the processes are all down before continuing
docker exec -it <name of container> gitlab-ctl status

# Run the restore
 12.2以后版本
docker exec -it <name of container> gitlab-backup restore BACKUP=xxxx
 ## 注意,没有_gitlab_backup.tar部分

# Run the restore 12.1以前版本
docker exec -it gitlab-server gitlab-rake gitlab:backup:restore BACKUP=xxxx ## 注意,没有_gitlab_backup.tar部分

# Restart the GitLab container
docker restart <name of container>

# Check GitLab
docker exec -it <name of container> gitlab-rake gitlab:check SANITIZE=true