docker-compose 部署gitlab,jenkins

2019-10-21  本文已影响0人  夜醉梦紅尘

一、Docker Compose 部署 GitLab

GitLab 是利用 Ruby on Rails 一个开源的版本管理系统,实现一个自托管的 Git 项目仓库,可通过 Web 界面进行访问公开的或者私人项目。它拥有与 Github 类似的功能,能够浏览源代码,管理缺陷和注释。可以管理团队对仓库的访问,它非常易于浏览提交过的版本并提供一个文件历史库。团队成员可以利用内置的简单聊天程序 (Wall) 进行交流。它还提供一个代码片段收集功能可以轻松实现代码复用,便于日后有需要的时候进行查找。

1、部署 GitLab

我们使用 Docker 来安装和运行 GitLab 中文版,docker-compose.yml 配置如下:

version: '3'
services:
    gitlab:
      image: 'twang2218/gitlab-ce-zh'
      restart: always
      container_name: 'gitlab'
      privileged: true
      hostname: 'gitlab'
      environment:
        TZ: 'Asia/Shanghai'
        GITLAB_OMNIBUS_CONFIG: |
          external_url 'http://192.168.152.204或者域名'
          gitlab_rails['time_zone'] = 'Asia/Shanghai'
          gitlab_rails['smtp_enable'] = true
          gitlab_rails['smtp_address'] = "smtp.163.com"
          gitlab_rails['smtp_port'] = 465
          gitlab_rails['smtp_user_name'] = "xxx@163.com"
          gitlab_rails['smtp_password'] = "xxx"
          gitlab_rails['smtp_domain'] = "163.com"
          gitlab_rails['smtp_authentication'] = "login"
          gitlab_rails['smtp_enable_starttls_auto'] = true
          gitlab_rails['smtp_tls'] = true
          gitlab_rails['gitlab_email_from'] = 'xxx@163.com'
          gitlab_rails['gitlab_shell_ssh_port'] = 220
          unicorn['port'] = 8888
          nginx['listen_port'] = 80
      ports:
        - '8081:80'
        - '443:443'
        - '220:22'
      volumes:
        - /data/gitlab/config:/etc/gitlab
        - /data/gitlab/data:/var/opt/gitlab
        - /data/gitlab/logs:/var/log/gitlab

2、配置 GitLab

image.png image.png

注意: 如果服务器配置较低,启动运行可能需要较长时间,请耐心等待

3、设置 GitLab

第一次使用时可以做一些初始化设置,点击 管理区域 -> 设置

image.png image.png image.png

4、账户管理

使用时请不要直接通过 root 用户操作,需要先创建用户,然后通过创建的用户操作,如果你是管理员还需要为其他开发人员分配账户

image.png image.png image.png image.png image.png image.png

注意: 创建完账户,第一次登录时还会提示你修改登录密码

5、项目管理

image.png image.png image

6、使用 SSH 方式拉取和推送

1、生成 SSH KEY

ssh-keygen -t rsa -C "GitLab的邮箱账号"
Microsoft Windows [版本 10.0.14393]
(c) 2016 Microsoft Corporation。保留所有权利。

C:\Program Files\Git\usr\bin>ssh-keygen -t rsa -C "topsale@vip.qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Lusifer/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Lusifer/.ssh/id_rsa.
Your public key has been saved in /c/Users/Lusifer/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:cVesJKa5VnQNihQOTotXUAIyphsqjb7Z9lqOji2704E topsale@vip.qq.com
The key's randomart image is:
+---[RSA 2048]----+
|  + ..=o=.  .+.  |
| o o + B .+.o.o  |
|o   . + +=o+..   |
|.=   .  oo...    |
|= o     So       |
|oE .    o        |
| .. .. .         |
| o*o+            |
| *B*oo           |
+----[SHA256]-----+

C:\Program Files\Git\usr\bin>

2、复制 SSH-KEY 信息到 GitLab

image.png image.png

7、使用 TortoiseGit(window10)

1、克隆项目

15718833-b4c3ddb1e0ee23d5.png image.png image.png image.png

2、推送项目

target/
!.mvn/wrapper/maven-wrapper.jar

## STS ##
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans

## IntelliJ IDEA ##
.idea
*.iws
*.iml
*.ipr

## JRebel ##
rebel.xml

## MAC ##
.DS_Store

## Other ##
logs/
temp/
image.png image.png image.png 15718833-f50cdfe681e2c39e.png

8、查看 GitLab 确认提交成功

image.png

二、Docker-Compose 部署 Jenkins

官方的:https://jenkins.io/doc/book/installing/

1、部署 Jenkins

docker-compose.yml文件:

version: '3'
services:
      jenkins:
        image: jenkins/jenkins:lts stephdw/jenkins-ansible
        volumes:
            - /data/jenkins/:/var/jenkins_home
            - /var/run/docker.sock:/var/run/docker.sock
            - /usr/bin/docker:/usr/bin/docker
            - /usr/lib/x86_64-linux-gnu/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7
        ports:
            - "8080:8080"
        expose:
            - "8080"
            - "50000"
        privileged: true
        user: root
        restart: always
        container_name: jenkins
        environment:
            JAVA_OPTS: '-Djava.util.logging.config.file=/var/jenkins_home/log.properties'

这里注意一点使用镜像:jenkins 拥有root权限这里要注意 运行命令:

docker-compose up -d

如果如下错误:

image.png

解决办法修改文件权限:chown -R 1000:1000 /data/jenkins 直接可以访问:ip:端口

image.png
查看正在运行的容器:
docker ps
docker-compose ps
查看安装密码:
cat /data/jenkins/secrets/initialAdminPassword
image.png

选择默认插件


image.png

账户密码设置

image.png image.png

2、Jenkins 安装 Gitlab 插件

1.首先登录Jenkins,点击“系统管理”>"插件管理"。在插件管理中可以看到“可更新”、“可选插件”、“已安装”插件。

328388-20180721160417180-703176816.png

2.我们切换到“可选插件”选项卡,在搜索框中输入gitlab,找到gitlab pulgin。选中点击安装。如下是已经安装好的Gitlab插件。

328388-20180721160710945-1000936579.png

3、Jenkins 整合 Gitlab

1、配置 Gitlab

创建一个新项目

image.png

2、上传服务器秘钥到 gitlab

# docker 生成密钥
[root@deploy ~]# docker exec -it jenkins /bin/bash
root@2e8b64783be4:/# ssh-keygen -t rsa -C "xxx@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:+wxdAKOjCWMiiLNktBgnzH0h9RToHBrvUsdWUPf9tt4 18611142071@163.com
The key's randomart image is:
+---[RSA 2048]----+
|=.o..oo+*. .     |
|=*.o.+o. +. . .  |
|B++ B =.. .  . . |
|+= + B =   .    .|
|.   = o S   .   o|
|   . .   o .   ..|
|    .   o .    . |
|         +    . .|
|          o    .E|
+----[SHA256]-----+

root@2e8b64783be4:/# cat ~/.ssh/id_rsa.pub
秘钥放到gitlab中


# 虚拟机生成秘钥
[root@deploy ~]# ssh-keygen -t rsa -C "xxx@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:+wxdAKOjCWMiiLNktBgnzH0h9RToHBrvUsdWUPf9tt4 18611142071@163.com
The key's randomart image is:
+---[RSA 2048]----+
|=.o..oo+*. .     |
|=*.o.+o. +. . .  |
|B++ B =.. .  . . |
|+= + B =   .    .|
|.   = o S   .   o|
|   . .   o .   ..|
|    .   o .    . |
|         +    . .|
|          o    .E|
+----[SHA256]-----+
[root@deploy ~]# cat .ssh/id_rsa.pub 
ssh-rsa AAAB3NzaC1yc2EAAAADAQABAAABAQC1kOUt1qRxd/rJJkOIYOeM9u0twE9pZq4txKwEtAy3lT2LCcxgWbcjC0VBI84FO9XE8BKA/oEA1wCErVevnDoYfLT99x8t6SLc8bKZ9SOSAAt1LO5HkzaaBDnuoXyVRduWJHlN9zS7lGYxbEY1AHYWwA3hDl3Y/SPUC6ulwCU7QnbOn8GE7qGXW8BDdeTyIEXqn3pFYFTa7TiqnjR9w3iqzso32Yo/sDeEsdsm6uw8uEVUexBqeUdjB6OP35JLpnwaGhoXeiIW87fCtk37PFzb5quZB2yoBKTJHFU8lC+Ihh/Z/iGOyU0mmjvq8ot8P5RfDPFtpFc4Oipm3c3etNd root@deploy

3、将公钥上传到 gitlab

image.png

1、初始化项目

[root@deploy ~]# mkdir -p /data/Ansible/
[root@deploy ~]# cd /data/Ansible/
[root@deploy Ansible]# git config --global user.name "baiyongjie"
[root@deploy Ansible]# git config --global user.email "misterbyj@163.com"

[root@deploy Ansible]# git clone ssh://git@101.89.82.106:59888/root/Ansible.git code
Cloning into 'code'...
The authenticity of host '[101.89.82.106]:59888 ([101.89.82.106]:59888)' can't be established.
ECDSA key fingerprint is SHA256:KgiTZyrCxFQwHivVyAYc1XrpvfWVliLaC+S0cg2fwYE.
ECDSA key fingerprint is MD5:b8:f9:75:c9:53:c1:48:6e:90:65:c5:5b:e7:5c:bd:ba.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[101.89.82.106]:59888' (ECDSA) to the list of known hosts.
warning: You appear to have cloned an empty repository.

[root@deploy Ansible]# cd code/
[root@deploy code]# echo "##### jenkins test"  > README.md 
[root@deploy code]# git add .
[root@deploy code]# git commit -m 'add readme'
[master (root-commit) 3b6717d] add readme
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
[root@deploy code]# git push -u origin master    
Counting objects: 3, done.
Writing objects: 100% (3/3), 227 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://git@101.89.82.106:59888/root/Ansible.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

2、配置 Gitlab 为整合 Jenkins 做准备

创建访问令牌,设置方法如下图


image.png

复制访问令牌,备用

image.png

复制Gitlab仓库地址,备用

image.png

配置Jenkins

选择系统管理->管理插件->可选择插件->搜索 gitlab

image.png

插件安装完成后会显示

image.png

然后继续安装Gitlab Authentication和Gitlab Hook

image.png

配置jenkins连接gitlab,选择左侧的系统管理,然后选择中间的系统设置

image.png

然后点击左上角的配置,选择Gitlab


image.png

配置GitLab,Connection Name随便填,Git Host URL填GitLab的访问地址,然后点Add—jenkins,如图:

image.png image.png
image](https://i.loli.net/2018/08/03/5b64754243a45.png)

Credentials选择刚创建的GitLab Api Token,然后点下Test Connection,看下是否成功,如成功点击最下面的保存

image.png

配置Git plugin

image.png

创建一个测试工程,选择自由风格

image.png

源码管理选择git,选择Add-Jenkins,配置Gitlab的账号密码

image.png [ image.png

配置自动触发,点击构建触发器,先勾选Build when a change is pushed to GitLab,点击高级,然后再点击一下Generate就会生成一个Secret Token,如下

image.png

点击左下角的保存按钮。记录两个东西,一个是Build when a change is pushed to GitLab那一行中,GitLab CI Service URL:后面的 URL; 还有一个就是刚刚生成的Secret Token,这俩在后面配置GitLab工程时需要用到

配置Gitlab工程

在gitlab进入那个叫Ansible的项目,点击设置,点击导入所有仓库,在URL里填写刚刚记下来的URL,在安全令牌里填写刚刚记下来的Secret Token,如图:

image.png

然后点击下面绿色的增加Web钩子,如多出一个如下图的web钩子


image.png

如果提示 Urlis blocked: Requests to localhost are not allowed,这需要开启允许钩子访问本地网络

image.png

对刚刚生成的web钩子点击test,选择push events,然后就会出现200的成功字样,如图:

image.png

如果你再点击一下test上面的edit,就会看到钩子最近调用情况,再点击view details的话,就会看到具体的调用细节,如图:

image.png
测试自动构建

在服务器上push代码,

[root@deploy code]# echo "测试jenkins是否会自动构建" > auto-jenkins
[root@deploy code]# git add .
[root@deploy code]# git commit -m 'test autojenkins'
[master 2290848] test autojenkins
 1 file changed, 1 insertion(+)
 create mode 100644 auto-jenkins
[root@deploy code]# git push origin master
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 320 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://git@101.89.82.106:59888/root/Ansible.git
   3b6717d..2290848  master -> master

到Jenkins查看构建记录,发现push动作已经出发了Jenkins的自动构建

image.png

查看构建的控制台输出日志,重点看commit message和Finished

image.png

结束语.

至此,Jenkins结合Gitlab就已经做完了, 每次开发push代码后触发jenkins的自动构建, 可以在下面的构建步骤里增加一些shell,ansible-playbook等来帮助我们完成自动部署等操作.

Jenkins 整合 Ansible

安装 jenkins 和 ansible

要 jenkins 支持 ansible 的前提条件是在 jenkins 所在的主机上安装 ansible
jenkins 和 ansible 安装可以参考 https://baiyongjie.com/?p=188

测试ansible

[root@deploy ansible]# cd /etc/ansible/
[root@deploy ansible]# > hosts
[root@deploy ansible]# vim hosts
[client]
10.241.0.2

# 生成秘钥
[root@deploy ~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:NRuBl+TBQow5l3nb+E/MCg95tpZRF/+amoRTaBIhTXw root@deploy
The key's randomart image is:
+---[RSA 2048]----+
|      .O+*+.     |
|      +.XoEo   . |
|       o.==+    o|
|         oo=. . o|
|        S +o.+ ..|
|         o+o= + .|
|          o*.B o |
|           o*.+  |
|           .o.   |
+----[SHA256]-----+

#将秘钥拷贝到客户机
[root@deploy ansible]# ssh-copy-id  10.241.0.2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@10.241.0.2's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '10.241.0.2'"
and check to make sure that only the key(s) you wanted were added.

#测试ansible能否正常运行
[root@deploy ansible]# ansible  client -m ping
10.241.0.2 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

#新建一个测试的ansible-playbook
[root@deploy ~]# mkdir -p /data/Ansible-playbook
[root@deploy ~]# vim /data/Ansible-playbook/jenkins-mkdir.yml
---
- hosts: client
  gather_facts: no

  tasks:
    - name: mkdir jenkins-test-dir
      file: path=/data/jenkins-test-dir  state=directory

配置jenkins

登陆jenkins 选择系统管理->管理插件 安装以下两个插件: Ansible、AnsiColor

image.png

回到首页,新建一个任务,选择构建一个只有风格的任务

image.png

然后构建的步骤新加一个Ansible-playbook,配置脚本路径,然后保存


image.png

查看新构建的工程

image.png

执行刚才构建工程

image.png

查看工程构建的控制台输出

image.png [ image.png

到client主机查看 jenkins-test-dir 目录是否创建成功

#查看client的ip地址
[root@deploy ~]# cat /etc/ansible/hosts 
[client]
10.241.0.2

#登陆到远程主机
[root@deploy ~]# ssh 10.241.0.2
Last login: Fri Aug  3 22:02:58 2018 from gateway

#查看目录是否存在
[root@client ~]# tree  /data/
/data/
└── jenkins-test-dir

#查看目录的详细信息
[root@client ~]# stat  /data/jenkins-test-dir/
  File: ‘/data/jenkins-test-dir/’
  Size: 6               Blocks: 0          IO Block: 4096   directory
Device: 802h/2050d      Inode: 68197841    Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-08-03 22:19:48.393000000 +0800
Modify: 2018-08-03 22:12:27.046000000 +0800
Change: 2018-08-03 22:12:27.046000000 +0800

#从上面的信息可以看到目录的创建时间为22:12:27, 与jenkins构建时间一致
上一篇下一篇

猜你喜欢

热点阅读