将Jenkins Freestyle Golang Job转换为Jenkinsfile

时间:2018-11-08 07:07:00

标签: go jenkins jenkins-pipeline

我正在尝试将现有的Jenkins Freestyle Golang Job转换为Jenkinsfile,可以与我的项目一起检入,以便可以在Pipeline Job中使用它。

所说的工作只是在运行所有Go测试并在所有测试通过的情况下构建项目。部署还不是这项工作的重点。

我的工作设置如下:

Go插件安装:

Name: Go
Install Automatically: Checked
Install from golang.org: Go 1.11.2

注意: :我给它起了Go的名称,因此Go安装文件夹部分Go/src在下面的目录中可以保持一致。

凭据(全局):

Username with password: (My email address and password)

工作配置:

Use custom workspace: Checked
    Directory: /var/lib/jenkins/tools/org.jenkinsci.plugins.golang.GolangInstallation/Go/src/MY_PROJECT_NAME

Source Code Management:
    Git
        Repository URL: MY_PRIVATE_BITBUCKET_URL.git
        Credentials: (My email address and password)
        Branches to build: */master

Build Environment:
    Set up Go programming language tools: Checked
        Go version: Go

Build
    Execute Shell
        # Remove cached test results.
        go clean -cache            
        # Run all Go tests.
        cd /var/lib/jenkins/tools/org.jenkinsci.plugins.golang.GolangInstallation/Go/src/MY_PROJECT_NAME
        go test ./... -v

    Execute Shell
        # Build the project.
        cd /var/lib/jenkins/tools/org.jenkinsci.plugins.golang.GolangInstallation/Go/src/MY_PROJECT_NAME
        go build

我尝试使用Convert To Pipeline插件,但无法完全转换作业:

// Powered by Infostretch 

timestamps {

    node () {        
        stage ('MY_PROJECT_NAME - Checkout') {
            checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'MY_CREDENTIALS_ID', url: 'MY_PRIVATE_BITBUCKET_URL.git']]]) 
        }

        stage ('MY_PROJECT_NAME - Build') {            
            // Unable to convert a build step referring to "org.jenkinsci.plugins.golang.GolangBuildWrapper". Please verify and convert manually if required.       
            // Shell build step
            sh """ 
            cd /var/lib/jenkins/tools/org.jenkinsci.plugins.golang.GolangInstallation/Go/src/MY_PROJECT_NAME
            go clean -cache
            go test ./... -v
            """     
            // Shell build step
            sh """ 
            cd /var/lib/jenkins/tools/org.jenkinsci.plugins.golang.GolangInstallation/Go/src/MY_PROJECT_NAME
            go build 
            """ 
        }
    }
}

如何将这个简单的Job转换为Jenkinsfile?如有必要,我也愿意将Docker集成到上述文件中。

1 个答案:

答案 0 :(得分:0)

我知道了。

下面是位于项目目录根目录下的Jenkinsfile的内容:

#!/usr/bin/env groovy
// The above line is used to trigger correct syntax highlighting.

pipeline {
    agent { docker { image 'golang' } }    

    stages {
        stage('Build') {                
            steps {      
                // Create our project directory.
                sh 'cd ${GOPATH}/src'
                sh 'mkdir -p ${GOPATH}/src/YOUR_PROJECT_DIRECTORY'

                // Copy all files in our Jenkins workspace to our project directory.                
                sh 'cp -r ${WORKSPACE}/* ${GOPATH}/src/YOUR_PROJECT_DIRECTORY'

                // Copy all files in our "vendor" folder to our "src" folder.
                sh 'cp -r ${WORKSPACE}/vendor/* ${GOPATH}/src'

                // Build the app.
                sh 'go build'
            }            
        }

        // Each "sh" line (shell command) is a step,
        // so if anything fails, the pipeline stops.
        stage('Test') {
            steps {
                // Remove cached test results.
                sh 'go clean -cache'

                // Run all Tests.
                sh 'go test ./... -v'                    
            }
        }
    }
}