【OpenSSH+Jenkins搭建项目自动化部署】

OpenSSH+Jenkins搭建项目自动化部署

一、Windows安装OpenSSH

1.下载

https://github.com/PowerShell/Win32-0penSSH/releases

下载最新的 OpenSSH安装包,如:openSSH-Win64.zip

2.解压

将压缩包解压到 C:\Program Files\OpenSSH

3.安装

管理员运行cmd 进入目录C:\Program Files\OpenSSH

执行命令

dart 复制代码
powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1

4.启停服务

dart 复制代码
#停止服务
net stop sshd

#启动服务
net start sshd

5.SSH免密登录

管理员运行cmd 进入目录C:\Program Files\OpenSSH

执行命令

dart 复制代码
ssh-keygen

在当前用户的文件夹下会生成一对秘钥

将文件名 id_ed25519.pub 修改成 authorized_keys

修改 authorized_keys 文件权限,保留system和用户

修改配置文件

文件路径 C:\ProgramData\ssh\sshd config

dart 复制代码
# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options override the
# default value.

#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::

#HostKey __PROGRAMDATA__/ssh/ssh_host_rsa_key
#HostKey __PROGRAMDATA__/ssh/ssh_host_dsa_key
#HostKey __PROGRAMDATA__/ssh/ssh_host_ecdsa_key
#HostKey __PROGRAMDATA__/ssh/ssh_host_ed25519_key

# Ciphers and keying
#RekeyLimit default none

# Logging
#SyslogFacility AUTH
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
#PermitRootLogin prohibit-password
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10

PubkeyAuthentication yes
PubkeyAcceptedKeyTypes  +ssh-rsa

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile	.ssh/authorized_keys

#AuthorizedPrincipalsFile none

# For this to work you will also need host keys in %programData%/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication no
#PermitEmptyPasswords no

# GSSAPI options
#GSSAPIAuthentication no

#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#PermitUserEnvironment no
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none

# no default banner path
#Banner none

# override default of no subsystems
Subsystem	sftp	sftp-server.exe

# Example of overriding settings on a per-user basis
#Match User anoncvs
#	AllowTcpForwarding no
#	PermitTTY no
#	ForceCommand cvs server

#Match Group administrators
#       AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys

防火墙打开 22 端口,并将生成的秘钥对里面的私钥拷贝到客户端中(更换秘钥后需要重启SSH服务),路径同服务端。

二、Jenkins安装

1.下载

Jenkins由Java语言编写而成,安装包即是一个war包。因此,Jenkins的运行启动依赖于Java环境,同时,它是免安装的。
https://www.jenkins.io/download/

2.安装启动

命令行:切换到jenkins.war所在目录,执行命令

dart 复制代码
java -jar jenkins.war

如果出现端口冲突相关报错,则可以修改jenkins的端口

dart 复制代码
java -jar jenkins.war --httpPort=8088

出现以下信息表示启动成功

3.登录

根据自己使用的端口,在浏览器界面打开,可以加载出jenkins的登录界面,如果没有指定端口,默认是8080端口,打开后页面如下,需要初始化密码。

进入到指定的用户文件夹中

C:\Users\XXX\.jenkins\secrets\initialAdminPassword

打开这个文件,里面会有一串密码,把它复制出来,回到解锁Jenkins页面。

输入密码后点击继续,这时候来到了新手入门界面,自定义Jenkins

点击安装之后,会让你创建第一个管理员用户,这里可以根据你的实际需求来设置,也可以点击使用admin账户继续。

点击保存并完成后,我们的Jenkins就已经准备就绪了,可以愉快地使用了。

三、项目自动化部署

1.SSH配置

jenkins安装SSH插件:Manage Jenkins > Plugins

插件装好后重启jenkins , 进入 Manage Jenkins > System

找到 Publish over SSH

SSH Servers 里面配置SSH的服务端信息,包括自定义的服务器名称服务器IP地址服务器登录用户名 、高级选项中的SSH客户端私钥 (跟本机SSH公钥成对生成的秘钥中的私钥);配置完成后可以点击右下角的 "Test Configuration" 测试是否能够连接成功。

2.项目配置

新建Item,选择构建一个自由风格的软件项目 Freestyle project

源码管理 ,添加git仓库信息以及用户账号密码信息。

Build Steps 构建步骤配置

我们是windows服务器环境,所以选择windows相关的执行命令Execute Windows batch command

dark 复制代码
#下载依赖
npm i

#打包
npm run build:pro

#进入项目文件夹
cd C:\ProgramData\Jenkins\.jenkins\workspace\EnergyAllInOne.client.beta

#删除原有的压缩包
del ZTEnergyAllInOneWeb.zip

#构建新的压缩包
zip -r ZTEnergyAllInOneWeb.zip ZTEnergyAllInOneWeb

图中是前端项目的打包相关命令,其他项目可以自行配置。

构建后步骤配置 选择执行命令类型Send build artifacts over SSH

配置SSH Server 服务器选择(SSH配置中配置的)

Transfer Set

信息填写完成后保存即可回到首页进行构建。

如果构建失败,可以点击构建历史进入查看具体的构建信息

3.权限控制

参考博客:jenkins 权限控制(用户只能看指定的项目)

相关推荐
客观花絮说5 分钟前
DSC+DW实时+异步搭建部署
运维
x66ccff24 分钟前
【linux】4张卡,坏了1张,怎么办?
linux·运维·服务器
周全全26 分钟前
Elasticsearch 检索优化:停用词的应用
大数据·elasticsearch·jenkins
昕er1 小时前
Python 中自动打开网页并点击[自动化脚本],Selenium
python·selenium·自动化
我命由我123452 小时前
GPIO 理解(基本功能、模拟案例)
linux·运维·服务器·c语言·c++·嵌入式硬件·c#
kka杰2 小时前
Linux 进程3
linux·运维·服务器
华纳云IDC服务商2 小时前
网站服务器怎么计算同时在线人数?
运维·服务器
没有名字的小羊2 小时前
Linux基础命令——账户简单管理
linux·运维·服务器·网络·数据库
smile_life_2 小时前
服务器非法关闭后MySQL服务启动失败
运维·服务器·mysql
kka杰2 小时前
Linux 进程2
linux·运维·服务器