Skip to content

Instantly share code, notes, and snippets.

@SoulSu
Created August 14, 2018 13:26
Show Gist options
  • Select an option

  • Save SoulSu/d1cdc61bb3e1587906c84de424c5015f to your computer and use it in GitHub Desktop.

Select an option

Save SoulSu/d1cdc61bb3e1587906c84de424c5015f to your computer and use it in GitHub Desktop.
Golang build with jenkins pipleline
node {
// 使用的Go版本号
def root = tool name: 'Go1103', type: 'go'
// 使用的第三方包目录
def sysGoVendorPath = "/opt/govendor"
// 系统编译目录
def projBuildPath = "${JENKINS_HOME}/jobs/${JOB_NAME}/builds/${BUILD_ID}/"
// 项目构建目录
def goBuildPath = "${projBuildPath}src/quoteserver/quotereal"
// 相对编译输出文件夹名称
def relBuildOutDir = "build"
// 编译输出文件夹
def absBuildOutDir = "${projBuildPath}src/${relBuildOutDir}"
// 编译二进制文件
def buildBinary = "quotereal"
// 编译成功后的包名称
def buildSuccessTarName = sh returnStdout: true, script: """echo -n ${buildBinary}-`date "+%Y%m%d%H%M%S"`-${BUILD_ID}.tar.gz"""
ws("${projBuildPath}/src") {
withEnv(["GOPATH=${projBuildPath}:${sysGoVendorPath}", "PATH+GO=${root}/bin"]) {
env.PATH="${sysGoVendorPath}/bin:$PATH"
env.GOOS="linux"
env.GOARCH="amd64"
echo "Build path:${projBuildPath}"
stage('Checkout'){
echo 'Checking out svn code'
checkout([
$class: 'SubversionSCM',
additionalCredentials: [], excludedCommitMessages: '', excludedRegions: '', excludedRevprop: '',
excludedUsers: '', filterChangelog: false, ignoreDirPropChanges: false, includedRegions: '',
locations: [[
cancelProcessOnExternalsFail: true,
credentialsId: '3bff7825-7c09-4793-8db8-3b0e152008f5', depthOption: 'infinity',
ignoreExternalsOption: true, local: 'quoteserver',
remote: 'https://192.168.0.192/svn/allprj/bibiprj/src_server_2.0/trunk/src/quoteserver'
]],
quietOperation: true, workspaceUpdater: [$class: 'UpdateWithRevertUpdater']
])
echo 'Checking out devkit code'
checkout([
$class: 'SubversionSCM',
additionalCredentials: [], excludedCommitMessages: '', excludedRegions: '', excludedRevprop: '',
excludedUsers: '', filterChangelog: false, ignoreDirPropChanges: false, includedRegions: '',
locations: [[
cancelProcessOnExternalsFail: true,
credentialsId: '3bff7825-7c09-4793-8db8-3b0e152008f5', depthOption: 'infinity',
ignoreExternalsOption: true, local: 'devkit',
remote: 'https://192.168.0.192/svn/allprj/bibiprj/src_server_2.0/trunk/src/devkit'
]],
quietOperation: true, workspaceUpdater: [$class: 'UpdateWithRevertUpdater']
])
}
stage('Pre Test') {
sh 'go env'
sh 'golint'
echo 'Pulling Dependencies'
}
stage('Test'){
sh "cd ${projBuildPath}/src/quoteserver && go list ./... | grep -v /vendor/ | grep -v github.com | grep -v golang.org > projectPaths1"
def paths = sh returnStdout: true, script: """cd ${projBuildPath}/src/quoteserver && awk '\$0="./src/"\$0' projectPaths1 > projectPaths && cat projectPaths | tr '\n' ' '"""
echo 'Vetting'
sh """cd ${projBuildPath} && go tool vet ${paths}"""
echo 'Linting'
sh """cd ${projBuildPath} && golint ${paths}"""
echo 'Testing'
// sh """cd ${projBuildPath} && go test -race -cover ${paths}"""
}
stage('Build'){
echo 'Building Executable'
sh """mkdir ${relBuildOutDir}"""
// 编译二进制
sh """cd ${goBuildPath} && go build -v -o ${absBuildOutDir}/${buildBinary} -ldflags '-s -w'"""
// 打包文件输出
sh """
cd ${absBuildOutDir} && ls -alh &&
tar -czvf ${buildSuccessTarName} ${buildBinary}
"""
}
// 收集编译结果
stage('ArchiveArtifacts Publish'){
archiveArtifacts artifacts: "${relBuildOutDir}/${buildSuccessTarName}", fingerprint: true, onlyIfSuccessful: true
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment