这是我参与更文应战的第3天,活动概略检查:更文应战

本文记录了怎样运用 Jenkins Pipeline 结束构建和长途安顿。

自动化流程

企业里常见的项目自动化流程应该构建appreciate机从代码仓拉apple取代码进行构建,构建结束后会将产品推送到制品库中,比方镜像仓,
然后中心会有查验环境,用于进行自动化查验或人工查验,毕竟进行长途安顿。

玩转 CICD 之 运用 Jenkins Pipeline 实现构建和长途布置

项目结构

这儿咱们用的 Go 的项目结构,它大概的结构应该是下面这样的:

|-- my-app
|-- .gitignore
|-- README.md
|-- LICENSE
|-- go.mod
|-- go.sum
|-- main.go
|-- pkg
|-- ...

项目构建github打不开

服务器装备于这儿构建的是 Go 的项目,假设用到私有库,在 go mod tidy 时会要求供应 Git 凭appear证,咱们能够现在 Jenkins 的凭据办理中创立 Usernamgitlabe with password 类型的凭据,其github官网Username 便是 GitHub 的用户名,password 则是 GitHub 的 AccessToken,这儿首要用到的是 AccessTokenUsername 其实并不需求。

但在 Jenkins Pipeline 中运用 usernamePassword 时要求一起定义用户名变量名 usernameVariable 和 暗码变量名 pagit指令sswordVariable

stage('Build') {
steps {
withCredentials(bindings: [
usernamePassword(credentialsId: 'GITHUB_CREDENTIAL',
usernameVariable: 'GITHgithub是干什么的UB_USER',
passwGitHubordVariable: 'GITHUB_http协议ACCESS_TOKEN'
)
]) {
sh '''
git config --global url."htapplicationtps://${GITHUB_ACCESgithubS_http署理TOKEN}:x-oauth-bhttp署理asic@github.com/".insteadOf "https://github.com/"
go mod tidy
go buildgithub怎样下载文件 -o bin/my-app main.go
'''
}
}
}

长途安顿

在构建结束后,咱们会HTTP将构建产品推送到制品库,然后咱们能够从制品库中拉取构建产品进行安顿查验环境并进行查验,在验证通往后,会从制品库中拉取验证经过的产品进行安顿上线。

但在本文中,咱们的应用application相对简略,可服务器连接失利是什么意思以忽略推送产品到制品库以及中心的查验验证环节,政策是结束构建后当即安顿上服务器怎样建立线。

github是干什么的般来说,线上环境和giti构建环境不会是同github中文社区一台机器,所以这个时giti分咱们需求GitHub将构建产品复制到另一台服务器上,然后在另一台服务器进步行安顿。

由于需求对另一台服服务器系统务器进行操作,所以咱们需求在 Jenkins 上装备 DEPLOY_HOSTDEPLOY_POapproachRTSSH_CREDENTIhttpwatchAL 三个凭据,其间 DEPLOY_HOSTDEPLOY_PORTSecret text 类型的凭据,SSH_CREgithub打不开DENTIALSSH Username with private key 类型的凭据。

stage('Deploy') {
environment {
DEPLOY_HOST = credentials('DEPLOY_HOST')
DEPLOY_PORT = credentials('DEPLOY_PORT')
}
steps {
withCredentials([
ssh服务器地址在哪里看UserappointmentPrivateKey(credentialsId: 'github中文社区SSH_CREDENTIAL',
keyFileVariable: 'SSH_KEY',
usernameVariable: 'appearanceSSH_USERNAgithubME'),
]) {
sh """
mkdir -p ~/.ssh && chmhttp署理od 700 ~/.ssh
echo 'StrictHostKeyChecking no' >> /etc/ssh/ssh_config
cagithub中文官网网页t ${SSH_KEY} > ~/.ssh/id_github中文官网网页rsa && chmod 400 ~/.ssh/id_rsa
scp -P ${DE服务器和电脑主机的差异PLOY_P服务器租用ORT} bin/my-app ${SSH_USER}@${DEPLOY_HOST}:/data/my-app
ssh -p ${DEPLOY_PORT} ${SSH_USER}@${DEPLOY_HOST} "nohup /dahttps和http的差异ta/my-app >> /data/my-app.log 2>&1 &"
""giti轮胎"
}
}
}

安顿的进程首要包括:

  1. 复制构建产服务器和电脑主机的差异品到安顿服务器
  2. 在安顿服务器上履行安顿指令,比方 nohup /data/my-app >> /daHTTPta/my-app.log 2>&1 &

其间简化了一git指令些细Git节,比方在安顿前,咱们需求先备份数据。所以这儿咱们能够写一个凌乱的安顿脚本 deploy.sh 放在项目中APP,然后在 Jenkins Pipeline 中运HTTPscp 将安顿脚本文件复制github官网到安顿服务appleappointment,假定放在 /data/deploy.sh,终github中文官网网页究只需 ssh -p ${DEPLOY_PORT} ${SSH_USER}@${DEPLOY_HOST} /bin/bash /http 500data/deploy.sh 即可。

完好的 Jenkins Pipeline

pipeline {
agent {
docker {
image 'golang:1.15-alpine'
args '-v /data/my-app-cache:/服务器怎样建立go/.applecache'
}
}
optgithub中文社区ions {
timeout(time: 20, unit: 'MINUTES')
disableConcurrentBuilds()
}
stahttpwatchges {
stage('Build') {
steps {
withCredentials(bindings: [
usernamePassword(credentialsIappetited: 'GITgit指令HUB_CREDENTIAL',
usernameVariable: 'GIappearanceTHUB_USEhttp://www.baidu.comR',
passwordVariablhttpcliente: 'GITHUB_ACCESS_TOKEN'
)
]) {github
sh '''
git config --global url."https://${GITHUB_ACChttpclientESS_TOKEN}:x-oauth-basic@github.com/".insteadOf "https://github.com/"
go mod tidy
go build -o bin/my-app main.go
'''
}
}
}
stage('Deploy') {
environment {
DEPLOgithub是干什么的Y_HOST = credentials('DEPLOY_HOST')
DEPLOY_PORT = credentials('DEPLOY_PORT')
}
steps {
withCredentiagithub敞开私库ls([
sshUserPrivateKey(credentialsId: 'SSH_CREDEhttp 500NTIAL',
keyFileVariable: 'SSH_KEY',
usernameVariable: 'SSH_USERNAME'),
]) {
sh """
m服务器怎样建立kdir -p ~/.ssh && chmod 700 ~/.ssh
echo 'StrictHostKeyChecking n服务器系统o' >> /etc/ssh/ssh_config
cat ${SSH_KEY} > ~/.ssh/id_rsa && chmoGitHubd 400 ~/.github是干什么的ssh/id_rsa
shttp 500cp -P ${DEPLOY_Pgithub中文社区ORT} bin/my-app ${SSH_USER}@${DEPLOY_HOST}:/data/my-app
ssh -p ${DEPLOY_PORT} ${SSH_USER}@${DEPgiti轮胎LOY_HOST} "nohup /data/my-app >> /data/my-app.log 2>&amGitHubp;1 &"
"""
}
}
}
}http协议
}

感谢阅读

笔者不才,非常欢迎咱们对github打不开本文的内容进行纠正,或许与笔者进行谈论!

个人博客

k8scatgithub敞开私库.com/posts/remot…