dreamfly2016 2018-11-08 07:06
浏览 176
已采纳

将Jenkins Freestyle Golang Job转换为Jenkinsfile

I'm trying to convert my existing Jenkins Freestyle Golang Job into a Jenkinsfile that I can check-in along with my project so I can use it in a Pipeline Job instead.

Said job is simply running all Go tests and Building the project if all tests pass. Deployment is not yet a concern of this job.

My working setup is as follows:

Go Plugin Installations:

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

NOTE: I gave it the name Go so the Go installation folder portion Go/src can be consistent in the directories below.

Credentials (Global):

Username with password: (My email address and password)

Job Configuration:

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

I tried using the Convert To Pipeline plugin, but it was not able to completely convert the job:

// 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 
            """ 
        }
    }
}

How do I convert this simple Job into a Jenkinsfile? I am also willing to integrate Docker into said file if necessary.

  • 写回答

1条回答 默认 最新

  • doujiaci7976 2018-11-12 10:27
    关注

    I figured it out.

    Below are the contents of my Jenkinsfile located at the root of my project directory:

    #!/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'
    
                    // Remove build cache.
                    sh 'go clean -cache'
    
                    // 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 -testcache'
    
                    // Run all Tests.
                    sh 'go test ./... -v'                    
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 前台报错,加我微信帮我解决一下,15043457399
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题