>前言: 自己申请到公网IP后在家里搭建好了自己的私有仓库Gitlab,因为不稳定性[停电],决定实现下gitlab仓库自动同步到gitee仓库。 ## Gitlab同步gitee ### 进入项目目录  ### Setting-repositories  ### 配置gitee  ### 测试  ### 成功  ## 自动部署 ### 服务器创建key ```shell #生成ssh密钥 ssh-keygen -t rsa -C "admin@2smile.cn" # 密钥在 /root/.ssh/ 目录 cat ~/.ssh/id_rsa.pub # 得到密钥 ``` ### gitlab ssh   ### gitee ssh  ### 测试是否可以克隆 ```shell git clone git@gitee.com:colyss/douyin.git 正克隆到 'douyin'... The authenticity of host 'gitee.com (212.64.62.183)' can't be established. ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+K21YoFgbVr17bmjey0Wc. ECDSA key fingerprint is MD5:27:e5:d1:f7:2a:9e:eb:6c:93:cd:1f:c1:47:a3:54:b1. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'gitee.com,212.64.62.183' (ECDSA) to the list of known hosts. remote: Enumerating objects: 15, done. remote: Counting objects: 100% (15/15), done. remote: Compressing objects: 100% (14/14), done. remote: Total 15 (delta 5), reused 0 (delta 0), pack-reused 0 接收对象中: 100% (15/15), 8.09 KiB | 0 bytes/s, done. 处理 delta 中: 100% (5/5), done. #这样表示OK了 ``` ### 服务器配置webhook自动化部署 > 宝塔面板安装webhook插件  ### 创建一个webhook 名字随意即可 > 以下为webhook脚本,测试可用。 ```shell #!/bin/bash echo "" #输出当前时间 date --date='0 days ago' "+%Y-%m-%d %H:%M:%S" echo "Start" #判断宝塔WebHook参数是否存在 if [ ! -n "$1" ]; then echo "param参数错误" echo "End" exit fi #git项目路径($1是param后面的参数,指向你的服务器的目录) gitPath="/www/wwwroot/$1" #git 网址 (替换成你的git地址,可选择https方式或者ssh方式) gitHttp="git@gitee.com:colyss/$1.git" echo "gethttp:$gitHttp" echo "Web站点路径:$gitPath" #判断项目路径是否存在 if [ -d "$gitPath" ]; then cd $gitPath #判断是否存在git目录 if [ ! -d ".git" ]; then echo "在该目录下克隆 git" git clone $gitHttp gittemp mv gittemp/.git . mv gittemp/* $(pwd) rm -rf gittemp fi #拉取最新的项目文件 #git reset --hard origin/master git pull #设置目录权限 chown -R www:www $gitPath echo "End" exit else echo "该项目路径不存在" echo "End" exit fi ``` 1. 宝塔的webhook链接填写到gitee webhook里面。     ## webhook自动化部署 > gitlab-runner 安装 ```shell yum install gitlab-ci-multi-runner ``` ### webhook方式 ```shell #!/bin/bash echo "" #输出当前时间 date --date='0 days ago' "+%Y-%m-%d %H:%M:%S" echo "Start" #判断宝塔WebHook参数是否存在 if [ ! -n "$1" ]; then echo "param参数错误" echo "End" exit fi #git项目路径($1是param后面的参数,指向你的服务器的目录) gitPath="/www/wwwroot/$1" #git 网址 (替换成你的git地址,可选择https方式或者ssh方式) gitHttp="git@gitee.com:colyss/$1.git" echo "gethttp:$gitHttp" echo "Web站点路径:$gitPath" #判断项目路径是否存在 if [ -d "$gitPath" ]; then cd $gitPath #判断是否存在git目录 if [ ! -d ".git" ]; then echo "在该目录下克隆 git" git clone $gitHttp gittemp mv gittemp/.git . mv gittemp/* $(pwd) rm -rf gittemp fi #拉取最新的项目文件 #git reset --hard origin/master git pull #设置目录权限 chown -R www:www $gitPath echo "执行自定义脚本" ../jobs/$1.sh $1 echo "End" exit else echo "该项目路径不存在" echo "End" exit fi ``` * 在shell里面加了一个自动化执行脚本 ```shell echo "执行自定义脚本" ../jobs/$1.sh $1 ```  ### 成功自动构建  ## gitlab runner 自动部署 1. 在宿主机也就是gitlab-runner【这个得自己安装 gitlab-runner】 这台机器上面。 ```shell #执行 gitlab-runner register ```   2. 等待1分钟查看状态 出现绿标表示成功了。  3. gitlab仓库创建:gitlab-ci.yml 这个是控制宿主机自动化的脚本文件 ```shell stages: - deploy go: stage: deploy script: - docker build -t douyin . - docker run --name dy -d -it -p 6666:6666 douyin tags: - douyin ``` > push操作执行后,运行出现了,估计是权限的文件。【并没有解决】  ```shell [root@VM-12-4-centos ~]# usermod -aG docker gitlab-runner 赋予权限 [root@VM-12-4-centos ~]# service docker restart 重启docker生效 ``` ### 已解决docker不能访问的问题 ``` [root@VM-12-4-centos ~]# gitlab-ci-multi-runner list Listing configured runners ConfigFile=/etc/gitlab-runner/config.toml Node-1 Executor=docker Token=xQAxFByZnXXXNgBJF URL=http://xxxxxx.cn:6080/ #得到 ConfigFile=/etc/gitlab-runner/config.toml vi /etc/gitlab-runner/config.toml #看到类似这样的 [[runners]] name = "Node-1" url = "http:/xxxx:6080/" token = "XXXXXXXXXXXXXXXX" executor = "docker" [runners.docker] tls_verify = false image = "python:3.8" privileged = false disable_cache = false volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache","/bin/docker:/bin/docker"] shm_size = 0 [runners.cache] #重点是这个映射 volumes!!!!! 把 docker映射到这个里面即可 ``` ### 额外设置 > 每次Push都会触发自动部署,所以要创建一个分支每次提交到分支后,确定merge之后在执行比较好一点。        ### 大功告成  最后修改:2021 年 06 月 28 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏