Jenkins+Gitlab+Ansible自动化部署(一)

本文详细介绍了如何通过Jenkins、Gitlab和Ansible进行自动化部署,包括实验环境要求、基础环境配置、Gitlab服务的安装与配置,以及使用Gitlab进行代码管理与权限设置。通过创建项目、提交代码、创建分支和合并请求等操作,展示了完整的自动化部署流程。
摘要由CSDN通过智能技术生成

Jenkins+Gitlab+Ansible自动化部署

一、实验环境要求(更新时间 2021-08-09)

1、服务器环境要求

主机名 IP地址 服务 系统版本 内核版本
gitlab 192.168.200.157 gitlab CentOS Linux release 7.6.1810 (Core) 3.10.0-957.el7.x86_64
jenkins 192.168.200.158 jenkins CentOS Linux release 7.6.1810 (Core) 3.10.0-957.el7.x86_64
ansible 192.168.200.159 ansible CentOS Linux release 7.6.1810 (Core) 3.10.0-957.el7.x86_64

2、客户端环境要求

  • 宿主机win10系统下的C:\Windows\System32\drivers\etc\hosts文件中添加如下内容
192.168.200.157 gitlab.example.com 
192.168.200.158 jenkins.example.com 
192.168.200.159 ansible.example.com 

二、基础环境配置

1、修改主机名

[root@lqf ~]# hostnamectl --static set-hostname gitlab
[root@lqf ~]# hostnamectl --static set-hostname jenkins
[root@lqf ~]# hostnamectl --static set-hostname ansible

2、配置 hosts

[root@gitlab ~]# vim /etc/hosts
192.168.200.157 gitlab.example.com 
192.168.200.158 jenkins.example.com 
192.168.200.159 ansible.example.com 
[root@gitlab ~]# scp -P54077  /etc/hosts root@192.168.200.158:/etc/hosts
[root@gitlab ~]# scp -P54077  /etc/hosts root@192.168.200.159:/etc/hosts

3、关闭防火墙和selinux

[root@gitlab ~]# sed -i "s/enforcing/disabled/" /etc/selinux/config
[root@gitlab ~]# systemctl stop firewalld && systemmctl disable firewalld
[root@gitlab ~]# reboot
[root@gitlab ~]# getenforce
Permissive
[root@gitlab ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

三、Gitlab 服务安装配置(版本 13.8.3-ce.0.el7 )

1、安装 postfix 并启动

[root@gitlab ~]# yum install postfix
[root@gitlab ~]# systemctl start postfix && systemctl enable postfix

2、安装 Gitlab 组件及 gitlab-ce

[root@gitlab ~]# yum install -y policycoreutils openssh-server openssh-clients
[root@gitlab ~]# curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
[root@gitlab ~]# yum install -y gitlab-ce 

3、创建证书

1、创建私有密钥
[root@gitlab ~]# mkdir -p /etc/gitlab/ssl
[root@gitlab ~]# openssl genrsa -out "/etc/gitlab/ssl/gitlab.example.com.key"  2048
Generating RSA private key, 2048 bit long modulus
...............+++
...............................................................................+++
e is 65537 (0x10001)
2、创建私有证书
[root@gitlab ~]# openssl req -new -key "/etc/gitlab/ssl/gitlab.example.com.key"  -out "/etc/gitlab/ssl/gitlab.example.com.csr"
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:sh
Locality Name (eg, city) [Default City]:sh
Organization Name (eg, company) [Default Company Ltd]:  #输入空格,然后回车
Organizational Unit Name (eg, section) []:  #输入空格,然后回车
Common Name (eg, your name or your server's hostname) []:gitlab.example.com
Email Address []:admin@example.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:1234.com
An optional company name []:  #直接回车
查看
[root@gitlab ~]# ll /etc/gitlab/ssl/
total 8
-rw-r--r-- 1 root root 1066 Aug  6 18:09 gitlab.example.com.csr
-rw-r--r-- 1 root root 1679 Aug  6 18:08 gitlab.example.com.key
3、创建CRT签署证书
  • 利用私有密钥和私有证书创建CRT签署证书
[root@gitlab ~]# openssl x509 -req -days 365 -in "/etc/gitlab/ssl/gitlab.example.com.csr" -signkey "/etc/gitlab/ssl/gitlab.example.com.key" -out "/etc/gitlab/ssl/gitlab.example.com.crt"
Signature ok
subject=/C=cn/ST=sh/L=sh/O= /OU= /CN=gitlab.example.com/emailAddress=admin@example.com
Getting Private key
查看
[root@gitlab ~]# ll /etc/gitlab/ssl/
total 12
-rw-r--r-- 1 root root 1265 Aug  6 18:10 gitlab.example.com.crt
-rw-r--r-- 1 root root 1066 Aug  6 18:09 gitlab.example.com.csr
-rw-r--r-- 1 root root 1679 Aug  6 18:08 gitlab.example.com.key
4、创建pem证书
  • 利用openssl命令输出pem证书
[root@gitlab ~]# openssl dhparam -out /etc/gitlab/ssl/dhparam.pem 2048
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
........................................................+................................................................................+.....................................+..................................................................................+..............................................+..................................................................................................................................+..+..
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
在使用AnsibleJenkinsGitLab进行上线时,可以编写一个Ansible的role来实现自动化部署过程。下面是一个示例的Ansible role的目录结构: ``` my_app/ ├── files/ │ └── my_app.jar ├── templates/ │ └── application.properties.j2 ├── tasks/ │ └── main.yml ├── vars/ │ └── main.yml └── README.md ``` 在`tasks/main.yml`文件中,定义了具体的部署任务,包括拷贝文件、安装依赖、重启服务等操作。例如: ```yaml --- - name: Copy JAR file copy: src: files/my_app.jar dest: /opt/my_app/my_app.jar - name: Copy application.properties file template: src: templates/application.properties.j2 dest: /opt/my_app/application.properties - name: Install dependencies shell: /usr/bin/apt-get install -y some_package - name: Restart service service: name: my_app state: restarted ``` 在`vars/main.yml`文件中,可以定义一些变量,用于配置不同环境的部署参数。例如: ```yaml --- environment: production app_port: 8080 db_host: localhost db_port: 3306 ``` 在Jenkins中,可以创建一个Pipeline Job来触发部署操作。在Pipeline的脚本中,可以使用Ansible的`ansible-playbook`命令来执行上线任务。例如: ```groovy pipeline { agent any stages { stage('Deploy') { steps { script { sh "ansible-playbook -i inventory.ini my_app/tasks/main.yml -e environment=production" } } } } } ``` 在GitLab中,可以配置一个Webhook,将代码提交或合并时触发Jenkins的构建操作。这样,每次有新的代码版本提交时,Jenkins就会自动触发部署操作。 通过结合使用AnsibleJenkinsGitLab,可以实现自动化的上线过程,并提高部署的效率和可靠性。以上只是一个简单的示例,具体的实现方式可以根据项目的需求进行调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值