任务详情
http://zentao.uninote.com.cn/zentao/task-view-49.html
这里只整理了比较关键的步骤,jenkins插件更新的频繁,可能有些功能只能实用一时。如果更新新版本,按照本方案实施不通,需要具体问题具体分析。
jenkins安装(docker简易部署)
docker pull jenkins/jenkins
docker run -d --name jenkins -p 18080:8080 jenkins/jenkins:latest
修改jenkins启动参数,和node环境安装
- 启动jenkins实例
docker exec -u root -it jenkins bash
# 编辑jenkins的启动脚本
vim /usr/local/bin/jenkins.sh
# 启动参数中添加如下选项
-Dhudson.security.csrf.GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION=true
- 安装nodejs
wget https://nodejs.org/dist/latest-v13.x/node-v13.14.0-linux-x64.tar.gz
tar xf node-v13.14.0-linux-x64.tar.gz
ln -s node-v13.14.0-linux-x64 node13140
ln -s /node13140/bin/node /usr/bin/node
- 重启jenkins
docker restart jenkins
jenkins配置部分:创建和配置job
-- 这里省略了插件安装步骤
- 需要的插件列表如下:
Generic Webhook Trigger Plugin
Git client plugin
Git plugin
Gogs plugin
Pipeline
- 创建一个基于pipeline的项目(如项目名称为t1) 配置:Gogs Webhook -> token -> 123456(这里可以自由填写必须跟下面gogs webhook保持一致)
- Pipeline->script->填入如下pipelie脚本内容
#!groovy
pipeline {
agent any
environment {
repoUrl = "http://git.uninote.com.cn:3000/collin/mynote-front.git"
pushRepo = "git@git.uninote.com.cn:cyb/mynote-front-dist.git"
}
parameters {
string(name:'repoBranch', defaultValue: '', description: 'git分支名称')
}
triggers {
GenericTrigger(
genericVariables: [
[key: 'branch', value: '$.ref']
],
token: 'ldajfdafdamynotefront' ,
causeString: ' Triggered on $branch' ,
printContributedVariables: true,
printPostContent: true
)
}
stages {
stage('getBranch'){
steps{
script{
try{
if("${branch}" != ""){
println "----------webhook式触发-----------"
branchName = branch - "refs/heads"
branchName = sh(returnStdout: true,script: "echo ${branchName}|sed -rn 's#^/(.*)#\\1#gp'").trim()
println "webhook触发的分支是: " + "${branchName}"
}
} catch(exc) { }
if("${params.repoBranch}" != ""){
println "-----------手动方式触发------------"
branchName = "${params.repoBranch}"
println "手动触发的分支是: " + "${branchName}"
}
}
}
}
stage('checkOut') {
steps{
checkout([$class: 'GitSCM', branches: [[name: "$branchName"]], browser: [$class: 'GogsGit', repoUrl: 'http://git.uninote.com.cn:3000'], extensions: [], userRemoteConfigs: [[credentialsId: '6', url: "$repoUrl"]]])
}
}
stage('build & push'){
environment {
branchName = "$branchName"
gitMail = "lg@mail.com"
gitUser = "lg"
npm = "/node12221/bin/npm"
npx = "/node12221/bin/npx"
}
steps{
sh '''
newBranch=false
echo ------current commit:
echo `git log --oneline -1`
echo ------FEACH_HEAD:
#echo `git log --oneline -1 FETCH_HEAD`
git reset --hard origin/$branchName
git log -1
# npm install first
$npm config set registry https://registry.npm.taobao.org
$npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/
$npm install --user=0
$npx nuxt build
echo "get version & message ..."
msg=`git log -1 --pretty="%H %s"`
echo "---- msg:"$msg
# cd dist for git op
if [ ! -d dist ];then
git clone $pushRepo dist
fi
cd dist
git fetch
if git checkout $branchName >/dev/null;then
# force to point to origin, discarding local commits
git reset --hard origin/$branchName
else
git checkout -b $branchName > /dev/null
newBranch=true
fi
# rm all & copy new
cd ..
cpFiles='node_modules/ static .nuxt jest.config.js nuxt.config.js nuxt.config.my.js package.json'
for file in $cpFiles;do
if [ ! -e $file ];then
echo "ignore not exist $file copy."
else
cp -R $file dist
fi
done
cd dist
git remote -v
git config --global user.email $gitMail
git config --global user.name $gitUser
git add .
if ! git commit -m "$msg";then
if $newBranch;then
echo "push new branch, do not need commit."
else
exit 0
fi
fi
git push origin $branchName
cd ..
rm -rf dist/*
rm -rf dist/.nuxt
echo ---------- building finished!!
'''
}
}
}
}
- 配置git账号
对应pipeline中的
credentialsId: '6',
是jenkins中添加的凭证id。 jenkins凭证添加步骤:manage jenkins->manage Credentials->system->global credentials-> add credentials->user name->填入lg
->password->填入lg123456
->id->填入6
->点击OK
gogs webhook配置
进入要配置的git项目仓库->点击setting(如无设置,则git账号对该仓库无权限,需要和gogs管理员协商)->webhooks
- 填入:PayLoad URL
http://116.62.117.165:18080/generic-webhook-trigger/invoke?token=123456
- 点击Update webhooks
- 点击Test Delivery测试触发推送
后续:
保存当前的容器 docker stop jenkins docker commit jenkins:node13140 jenkins