可以在pipeline工程中打開Pipeline Syntax
在代碼倉庫創建Jenkinsfile
文件
pipeline {
agent any
stages {
stage('ready') {
steps {
sh 'echo "ready to build"'
}
}
stage('Build') {
steps {
echo "Building"
sh '''mvn clean install'''
}
}
stage('Test') {
steps {
echo "Testing"
}
}
stage('Deploy') {
steps {
echo "Deploying"'
archiveArtifacts artifacts: '**/target/*. jar', fingerprint: true, onlyIfSuccessful: true
sh '''cp ${WORKSPACE}/${buildPath}/target/*. jar ${uploadPath}
}
}
stage('Clean') {
steps {
echo "Cleaning"
}
}
}
post {
succese {
echo 'success build!'
sh '''for((i=1;i<=`expr ${BUILD_NUMBER} - 5`;i++));
do
archivePath="${JENKINS_HOME}/jobs/${JOB_NAME}/builds/$i/archive/"
if [ -d $archivePath ]; then
echo "$archivePath文件夾已經存在"
rm -rf $archivePath
fi
done'''
}
}
}
sh
命令多行時需要三個單引號'
常用Step
echo
, 打印消息。echo 'hello'
build
, 構建其他jenkins job
。build 'api-job'
sh
, 執行linux腳本。
bat
, 執行windows腳本。
archiveArtifacts artifacts
, 歸檔打包好的文件archiveArtifacts artifacts: '**/target/*. jar'
fileExists
, 判斷文件是否存在。fileExists 'mypath'
dir
, 改變構建文件夾。
dir (mypath) {
sh '''mvn clean install'''
}
常用的環境變量
${WORKSPACE} #構建工作區
${GIT_COMMIT} #git代碼構建時commit SHA1值
${JOB_NAME} #構建…工程名稱