Use pagoda panel on centos7 a key deployment server environment to run automated configuration and implementation code

Pagoda is really a good tool to help us with fast servers running the required environment, eliminating the need to manually compile and install all kinds of tedious and error-prone areas, the most critical is that it's free, with the introduction of the pagoda panel, set up environment no longer a hard thing. Well, without further ado, let me introduce the following installed on your Tencent cloud host (operating system is centos7) pagoda panel and the code to achieve full process automation deployment, and do some recording.

First, install and configure gitlab code repository pagoda

1. Install pagoda

Not repeat them here, see the official website tutorial

https://www.bt.cn/bbs/thread-19376-1-1.html

2. Log panel, select the installation environment, I chose LNMP, the background system with nginx

3. Select the panel gitlab installed, after landing gitlab web client, modify personal information, modify personal mailbox in time to experience problems, not always receive confirmation gitlab sent through various online search configuration, modify / etc / gitlab / gitlab.rb for mailbox configuration, I use the 163 mailboxes, 163 mailboxes in the first application to send SMTP mail, on how to set 163SMTP server to send mail, see this article

https://mp.csdn.net/postedit/84566514

Then various configurations gitlab.rb, use this line of command to reset after gitlab configuration, nginx pagoda collapsed, always open

gitlab-ctl reconfigure

Pagoda community to find a solution

cp /opt/gitlab/embedded/sbin/gitlab-web /opt/gitlab/embedded/sbin/nginx

After entering the test environment gitlab

gitlab-rails console

Mail sending test

Notify.test_email('[email protected]', '111', '22222').deliver_now

This error has been reported

Said the Internet is to use port 465, and I add an open port 465 outbound rules pagoda in security group, the final configuration is so

gitlab_rails['gitlab_email_enabled'] = true
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.163.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "我的邮箱@163.com"
gitlab_rails['smtp_password'] = "邮箱授权码"
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'] = '我的邮箱@163.com'
user["git_user_email"] = "我的邮箱@163.com"
gitlab_rails['gitlab_email_display_name'] = 'gitlab用户名'

Reset the configuration again and restart gitlab, mail test, and finally sent successfully

4. Create gitlab the project, to the pagoda panel gitlab already generated public key ssh into place gitlab add public key, where I encountered a problem, because I want to pull gitlab installed on the server's gitlab the code, I use git clone git @ my server ip / root / has been an error when project.git cloning project, display cloning overtime, that I do not have permission, or the project does not exist, to search for a lot of reasons, say what 22-port firewall is disabled, or ip not on the same network segment, but I re-generate ssh keys on the local machine and cloning project has no problem, then I guess the reason is not to identify the different machines, and finally had an idea, into git clone [email protected]/root/project.git, cloning success, right ah, originally cloned on the same machine, should have thought of using 127.0.0.1.

So far, the project has been built warehouse, project by cloning the git ssh protocol, pull, push also no problem, the next step is to push the project when the time line of code updates and deployment of automation

First, the use gitlab-runner to automate

Use gitlab-runner to achieve the principle of automation is that various events gitlab-ci will automatically monitor the code repository, to push Ah, pull Ah, listen to happen once the code is pushed, it will tell the project to match the runner .gitlab-ci.yml the implementation of the project file, .gitlab-ci.yml file on the provisions of the code from a command to submit to testing and documentation needed to perform a series published by performing a series of steps in this document thereby automating the deployment of code.

1. Install runners

Add gitlab official repository

curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash

Installation runner

sudo yum install gitlab-runner

Registration runner

sudo gitlab-runner register

The runner according to configuration requirements gitlab

Once you've configured my emergence of such a mistake

I address this with my server, but then I thought that I was on the same machine installed runner, so I changed the ip address 127.0.0.1, but it is reported that this was wrong

网上说是gitlab与gitlab-runner的版本不匹配,我升级了版本也不管用,至此,使用gitlab-ci实现的自动化的思路受挫,我也没有找到合适的解决方法,希望哪位大神指点一下怎么解决这个问题

二、使用webhook实现自动化

webhook的原理就是通过api请求的方式来实现自动配置代码环境,当gitlab-ci监听到推送事件的时候,就是向你增加好的webhook钩子对应的链接地址发送一个api请求,你可以在这个api请求中处理你的代码自动化,如下图所示

 

1、实现钩子api

在服务器上新建gouzi.php文件,配置好nginx使它可以访问

在文件中验证我们将要设定的私密授权码,为了安全考虑还可以验证访问api的服务器ip地址,验证通过后就可以执行我们的shell代码了,这是我的钩子文件

$valid_token = '私密授权码';
$valid_ip = array('访问的服务器ip');
$client_token = $_SERVER['HTTP_X_GITLAB_TOKEN'];
$client_ip = $_SERVER['REMOTE_ADDR'];
if ($client_token !== $valid_token) die('Token mismatch!');
if (!in_array($client_ip, $valid_ip)) die('Ip mismatch!');
$result=array();
exec("shell脚本文件",$result);
print_r($result);

这是我的shell脚本文件,如果项目已经存在则拉取,如果项目不存在则新建

if [ -d "项目目录" ]
then
  cd 项目根目录
  git pull
  composer install
else
  cd 项目父目录
  git clone git地址
  composer install
fi

测试api的时候发现exec函数没有执行权限,需要去php.ini文件搜索disable_functions,删除里面的exec函数,取消对它的禁用,重启nginx服务,再次访问api,可以运行了

2.增加webhook钩子

来到上图所示页面,填入我们测试好的api请求地址,和设置的私密授权码,点击新增,钩子就增加好了,下次只要我们在本地推送项目的时候,服务器上就会自动构建好代码了

发布了27 篇原创文章 · 获赞 6 · 访问量 2万+

Guess you like

Origin blog.csdn.net/github_37673306/article/details/84645596