搭建Jenkins+Github的自动部署环境并使用守护进程(Supervisor)部署Golang(gin)项目
零、前言
Jenkins是一个自动部署的平台,可以极大的帮助我们自动部署项目,把注意力放在书写代码上。
教程环境说明:
系统:ubuntu20
配置:2核4G
软件:JAVA11
安装方式:APT(不使用Docker)
注意: 您至少需要预留256MB的内存空间,推荐1G内存空间,实测占用1G左右,安装所需要的环境,可以查阅官网的入门指南
一、软件安装
安装Jenkins
1.添加用于安装的密钥到您的系统里
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee \/usr/share/keyrings/jenkins-keyring.asc > /dev/null
2.添加安装Jenkins所需的软件源
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \https://pkg.jenkins.io/debian-stable binary/ | sudo tee \/etc/apt/sources.list.d/jenkins.list > /dev/null
3.更新软件列表
sudo apt-get update
4.安装fontconfig
和openjdk-11-jre
两个依赖
sudo apt-get install fontconfig openjdk-11-jre
5.安装Jenkins
sudo apt-get install jenkins
提醒:安装会自动新建一个名为jenkins的用户,这个很重要!
这里附上官网的下载页面
6.打开网址进行配置
http://ip:8080
7.此处需要输入一个配置密码,按提示到相应位置寻找即可
8.安装推荐的插件
有极大的可能会有部分插件没法安装,可以先跳过,之后会在进入系统后修复这个问题
9.按照指引,配置一个管理用户
10.配置访问域名
如果您打算配置反向代理,建议先去配置好反向代理后,再确认此处的访问域名
如果此处已经配置,但又有配置反向代理的计划,请查看文末的解决办法
11.完成配置,进入系统
12.前往系统管理->插件管理->高级
配置国内的镜像源
我使用清华大学镜像源
https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
13.重新安装失败的插件
14.重启,安装结束
安装Supervisor(守护进程)
为了更好的安装,这里建议使用root用户
*此处假设你已经安装好了pip并配置好了国内镜像源
1.使用pip安装
pip install supervisor
2、获取一份启动服务脚本(将supervisor设为服务,虽然是centos的,但也可以用啦)
wget https://raw.githubusercontent.com/Supervisor/initscripts/master/centos-systemd-etcs -O /usr/lib/systemd/system/systemd-supervisor.service
3.使用Supervisor自带的配置文件模板生成配置文件
echo_supervisord_conf > /etc/supervisord.conf
生成好的配置文件应该长这样
; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Notes:
; - Shell expansion ("~" or "$HOME") is not supported. Environment
; variables can be expanded using this syntax: "%(ENV_HOME)s".
; - Quotes around values are not supported, except in the case of
; the environment= options as shown below.
; - Comments must have a leading space: "a=b ;comment" not "a=b;comment".
; - Command will be truncated if it looks like a config file comment, e.g.
; "command=bash -c 'foo ; bar'" will truncate to "command=bash -c 'foo ".
;
; Warning:
; Paths throughout this example file use /tmp because it is available on most
; systems. You will likely need to change these to locations more appropriate
; for your system. Some systems periodically delete older files in /tmp.
; Notably, if the socket file defined in the [unix_http_server] section below
; is deleted, supervisorctl will be unable to connect to supervisord.[unix_http_server]
file=/tmp/supervisor.sock ; the path to the socket file
;chmod=0700 ; socket file mode (default 0700)
;chown=nobody:nogroup ; socket file uid:gid owner
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server); Security Warning:
; The inet HTTP server is not enabled by default. The inet HTTP server is
; enabled by uncommenting the [inet_http_server] section below. The inet
; HTTP server is intended for use within a trusted environment only. It
; should only be bound to localhost or only accessible from within an
; isolated, trusted network. The inet HTTP server does not support any
; form of encryption. The inet HTTP server does not use authentication
; by default (see the username= and password= options to add authentication).
; Never expose the inet HTTP server to the public internet.;[inet_http_server] ; inet (TCP) server disabled by default
;port=127.0.0.1:9001 ; ip_address:port specifier, *:port for all iface
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)[supervisord]
logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
logfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10 ; # of main logfile backups; 0 means none, default 10
loglevel=info ; log level; default info; others: debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false ; start in foreground if true; default false
silent=false ; no logs to stdout if true; default false
minfds=1024 ; min. avail startup file descriptors; default 1024
minprocs=200 ; min. avail process descriptors;default 200
;umask=022 ; process file creation umask; default 022
;user=supervisord ; setuid to this UNIX account at startup; recommended if root
;identifier=supervisor ; supervisord identifier, default is 'supervisor'
;directory=/tmp ; default is not to cd during start
;nocleanup=true ; don't clean up tempfiles at start; default false
;childlogdir=/tmp ; 'AUTO' child log dir, default $TEMP
;environment=KEY="value" ; key value pairs to add to environment
;strip_ansi=false ; strip ansi escape codes in logs; def. false; The rpcinterface:supervisor section must remain in the config file for
; RPC (supervisorctl/web interface) to work. Additional interfaces may be
; added by defining them in separate [rpcinterface:x] sections.[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface; The supervisorctl section configures how supervisorctl will connect to
; supervisord. configure it match the settings in either the unix_http_server
; or inet_http_server section.[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris ; should be same as in [*_http_server] if set
;password=123 ; should be same as in [*_http_server] if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available; The sample program section below shows all possible program subsection values.
; Create one or more 'real' program: sections to be able to control them under
; supervisor.;[program:theprogramname]
;command=/bin/cat ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1 ; number of processes copies to start (def 1)
;directory=/tmp ; directory to cwd to before exec (def no cwd)
;umask=022 ; umask for process (default None)
;priority=999 ; the relative start priority (default 999)
;autostart=true ; start at supervisord start (default: true)
;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
;startretries=3 ; max # of serial start failures when starting (default 3)
;autorestart=unexpected ; when to restart if exited after running (def: unexpected)
;exitcodes=0 ; 'expected' exit codes used with autorestart (default 0)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=true ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10 ; # of stdout logfile backups (0 means none, default 10)
;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stdout_syslog=false ; send stdout to syslog with process name (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10 ; # of stderr logfile backups (0 means none, default 10)
;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;stderr_syslog=false ; send stderr to syslog with process name (default false)
;environment=A="1",B="2" ; process environment additions (def no adds)
;serverurl=AUTO ; override serverurl computation (childutils); The sample eventlistener section below shows all possible eventlistener
; subsection values. Create one or more 'real' eventlistener: sections to be
; able to handle event notifications sent by supervisord.;[eventlistener:theeventlistenername]
;command=/bin/eventlistener ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1 ; number of processes copies to start (def 1)
;events=EVENT ; event notif. types to subscribe to (req'd)
;buffer_size=10 ; event buffer queue size (default 10)
;directory=/tmp ; directory to cwd to before exec (def no cwd)
;umask=022 ; umask for process (default None)
;priority=-1 ; the relative start priority (default -1)
;autostart=true ; start at supervisord start (default: true)
;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
;startretries=3 ; max # of serial start failures when starting (default 3)
;autorestart=unexpected ; autorestart if exited after running (def: unexpected)
;exitcodes=0 ; 'expected' exit codes used with autorestart (default 0)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=false ; redirect_stderr=true is not allowed for eventlisteners
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10 ; # of stdout logfile backups (0 means none, default 10)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stdout_syslog=false ; send stdout to syslog with process name (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10 ; # of stderr logfile backups (0 means none, default 10)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;stderr_syslog=false ; send stderr to syslog with process name (default false)
;environment=A="1",B="2" ; process environment additions
;serverurl=AUTO ; override serverurl computation (childutils); The sample group section below shows all possible group values. Create one
; or more 'real' group: sections to create "heterogeneous" process groups.;[group:thegroupname]
;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions
;priority=999 ; the relative start priority (default 999); The [include] section can just contain the "files" setting. This
; setting can list multiple files (separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files *cannot*
; include files themselves.;[include]
;files = /etc/supervisor/*.conf
此处;
的作用类似于注释
4.修改配置
注意最后两行的include
部分
取消注释并按需修改(此处配置的是Supervisor用于创建服务的配置文件的位置)
此处,我配置的是
[include]
files = /etc/supervisor/*.conf
5.绑定配置文件
supervisord -c /etc/supervisord.conf
*创建服务的配置文件将会在配置完Jenkins后配置
二、配置Jenkins
添加凭据
1.前往系统管理->安全->Manage Credentials
2.按照图中横线标识往里点
首先点击此处
再点击此处
直到出现这个页面
正如所见,我已经创建了两个凭证,接下来我将教您配置这两个凭据
配置github-token
1.进入Github
2.进入设置中的Developer settings
3.选择Personal access token
并点击右上角Generate new token
按自己的需求生成密钥(为了方便,我给与了token所有权限)
4.复制生成的token
注意:该token只会显示一次
5.回到Jenkins的控制台,点击左侧添加凭据
6.选择类型为Secret text
,在Secret字段中粘贴您刚刚复制的token
在描述中添加一些描述(空着也行,但是凭据多了不利于区分),ID空着,jenkins会自己生成
7.确定后完成
配置SSH-KEY
注意:此操作一定要在jenkins用户下进行
1.首先打开终端
先切换为root用户,在切换为jenkins用户(这样做就不用输入Jenkins的密码了)
su
Password:<root用户的密码>
su jenkins
2.生成SSH公私钥
ssh-keygen -t rsa -C
一路回车
3.转换公钥格式
cd #敲一下cd进入用户根目录
cd .ssh #进入SSH目录
ssh-keygen -m PEM -t rsa -f <yourfile>#将公钥转化为PEM格式
4.找到刚刚生成的密钥
sudo cat /etc/passwd #该命令可以帮您列出用户列表,以及用户根目录
不出意外的话,您的ssh目录下应该有以上几个文件
我们需要使用id_rsa.pub
和id_rsa.pub.pub
两个文件
5.打开Github,进入settings
中的SSH and GPG keys
6.添加一个新的KEY,并将id_rsa.pub.pub
中的内容复制进去
7.回到Jenkins中继续添凭据,将类型改为SSH Username with private key
Username输入您的Github ID
勾选Enter directly ,并将id_rsa.pub
中的内容复制进去
8.凭据配置完成
新建任务
1.新建任务,选择自由风格的软件项目
2.添加项目URL(如果没有,可能是Github插件未安装)
3.添加Git仓库,并选择刚刚创建好的SSH凭据
4.按图示选择Hook
5.在构建环境中绑定刚刚的Secret text
6.添加一个构建步骤执行shell
7.先保存,之后会添加shell脚本
8.还记得supervisor
吗?,接下来,我将未我们的程序创建一个守护进程的配置文件
9.在/etc/supervisor/中创建一个配置文件,例如:golang.conf
(如果您在配置supervisor时选择的位置与我不同,请在实际位置添加)
10.寻找jenkins的工作空间目录,如果没有修改配置,工作空间的目录应该位于
/var/lib/jenkins/workspace/<任务名称>
11.输入配置
示例:
[program:<此处填写程序的名称>]
command=/var/lib/jenkins/workspace/<任务名称>/<编译后的二进制文件>
directory=/home/ubuntu/config/
autostart=true
autorestart=true
startsecs=10
stdout_logfile=/var/log/<此处填写程序的名称>.log
stdout_logfile_maxbytes=1MB
stdout_logfile_backups=10
stdout_capture_maxbytes=1MB
stderr_logfile=/var/log/<此处填写程序的名称>.log
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=10
stderr_capture_maxbytes=1MB
command应为二进制文件位置
directory应为程序运行的目录(如果要读一些配置文件啥的,就需要谨慎配置)
stdout_logfile为生成运行日志的目录位置一定要有操作权限,不然跑不起来QAQ
stderr_logfile为生成错误日志的目录位置一定要有操作权限,不然跑不起来QAQ
12.添加Jenkins的构建脚本(假设已经拥有了构建的环境)
此处为构建Go的示例叫脚本
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
go build main.go
sudo supervisorctl restart <此处填写程序的名称>
此脚本将会生成一个名为main
的可执行文件在工作目录下
您可能需要给这个文件一个可执行的权限?
14.立即构建
正常情况下supervisor会报错,找不到进程
15.重启supervisor
supervisorctl reload
16.再次立即构建
supervisor错误将会消失
配置钩子
1.进入Github仓库的设置页
2.添加Webhook,并做下图的配置
注意地址后还需要/github-webhook
3.配置完成后,github将会做一次请求尝试,如果没有问题将会提示200
4.Push一次代码做测试
5.您会在Jenkins的Github Hook Log里看到对应的日志
至此基础配置已经全部完成
搭建Jenkins+Github的自动部署环境并使用守护进程(Supervisor)部署Golang(gin)项目相关推荐
- 构建基于Jenkins + Github的持续集成环境
登录 | 注册 John_cdy的专栏 学无止境 目录视图 摘要视图 订阅 有奖征资源,博文分享有内涵 6月推荐文章汇总 CSDN博文大赛初赛晋级名单公布 关注CSDN社区微信,福利多多 构建基于Je ...
- 这可能是东半球最详细的Linux下搭建Jenkins服务器实现自动打包的教程(上)
前言:作为开发者,我们都遇到过这样一个问题,项目在测试期的时候,测试总会找我们打各个环境的包,什么生产包.prd包.pre包,每次都是我们打完包再发给测试.可是打包是要时间的啊,项目小还好,项目大的话 ...
- 这可能是东半球最详细的Linux下搭建Jenkins服务器实现自动打包的教程(下)
上一篇我们从零开始,在Linux环境下配置了Jenkins的Android构建环境,但也仅仅是搭建环境,至于参数化构建.输出apk .定时构建.邮件发送等功能因为篇幅的原因没有介绍,本篇博客就来介绍介 ...
- 搭建Jenkins+springcloud一键自动部署
用jenkins 实现项目自动部署笔记 1.目的 1.一键实现项目自动构建打包发布(用于生产环境) 2.实现代码提交到代码仓库立刻自动打包部署(用于测试环境) 3.本测试脚本代码在最后 2.操作环境 ...
- 在 CentOS 7 上搭建 Jenkins + Maven + Git 持续集成环境
本文以部署 Spring boot + Maven 项目为例,使用码云作为代码托管仓库,在 CentOS 7 上搭建 Jenkins 持续集成环境. 1. 准备工作 1.1 安装 Java 环境 Je ...
- vue 不会热启动_使用PM2搭建在线vue.js开发环境(以守护进程方式热启动)
项目以vue.js+layUI的作为前端开发技术栈,需要有一个在线的环境供项目成员实时查看效果,总不能每次都webpack打包发布后才能看到效果吧!刚开始就简单使用npm run dev命令热启动,但 ...
- 使用PM2搭建在线vue.js开发环境(以守护进程方式热启动)
项目以vue.js+layUI的作为前端开发技术栈,需要有一个在线的环境供项目成员实时查看效果,总不能每次都webpack打包发布后才能看到效果吧!刚开始就简单使用npm run dev命令热启动,但 ...
- 一、linux搭建jenkins+github详细步骤
事情缘由: 现在在做的主要工作是通过jenkins+postman实现api的自动化测试,想要达到的效果是,api自动化测试定时跑脚本的同时,github有新的代码提交,jenkins会自动检测部署新 ...
- 【Jenkins+RF】从零搭建Jenkins+Robot Framework持续集成环境
转自:https://www.jianshu.com/p/ef8d3109ac5f 感恩 安装Jenkins 系统环境:CentOS Linux release 7.3.1611 x86_64 ...
最新文章
- python os.system 不打印_Python初体验之我爱你
- Redis整合Spring结合使用缓存实例
- 数字后端——时钟树综合
- python绘制如下图形、小三角形边长20_OpenGL学习脚印_ 绘制移动三角形 - 王定桥的专栏.pdf...
- Item 27 避免使用ICloneable接口
- html 两个表合并,SQL中将两个表合并成一个新表
- matlab时空地理回归,★时空地理加权回归space-time GWR GTWR分析软件-香港中文大学-黄波教授...
- BUUCTF-Misc-No.3
- android 去掉google搜索,Android 7.1 去掉桌面上的谷歌搜索框
- c语言中提取单词首字母,C语言实现单词小帮手
- vander范德蒙德行列式
- Hyperledger Fabric 2.1 BYFN 测试用例
- Google 总部员工终于也享受到了 “蜗居” 待遇
- 影视推荐的相关处理方法的思维导图
- Linux内核编译很简单,6步编译一个自己的内核
- 正则表达式系列(一):匹配开头与结尾
- 10个常用的汇总公式,拿来即用
- [转]Android应用及应用管理简介(作者:邹伟)
- 全新二改广告横幅在线源码
- 开源工具 — GridExcel支持无实体类读写Excel文件