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

将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 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'                    
                }
            }
        }
    }
    
    点赞 评论 复制链接分享

相关推荐