Jenkins Golang 声明式管道:构建 Docker 镜像并推送到 Docker Hub

我正在尝试创建我的 Golang 项目的 Docker 映像,并通过 Jenkins 声明式管道将其上传到 Docker Hub。


我能够构建我的项目并运行我的所有测试。


我的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/MY_PROJECT_DIRECTORY'


                // Copy all files in our Jenkins workspace to our project directory.                

                sh 'cp -r ${WORKSPACE}/* ${GOPATH}/src/MY_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 Unit Tests.

                sh 'go test ./... -v'                                  

            }

        }           

    }

}   

我的Dockerfile(如果需要)如下:


# Make a golang container from the "golang alpine" docker image from Docker Hub.

FROM golang:1.11.2-alpine3.8


# Expose our desired port.

EXPOSE 9000


# Create the proper directory.

RUN mkdir -p $GOPATH/src/MY_PROJECT_DIRECTORY


# Copy app to the proper directory for building.

ADD . $GOPATH/src/MY_PROJECT_DIRECTORY


# Set the work directory.

WORKDIR $GOPATH/src/MY_PROJECT_DIRECTORY


# Run CMD commands.

RUN go get -d -v ./...

RUN go install -v ./...


# Provide defaults when running the container.

# These will be executed after the entrypoint.

# For example, if you ran docker run <image>,

# then the commands and parameters specified by CMD would be executed.

CMD ["MY_PROJECT"]



POPMUISE
浏览 130回答 1
1回答

智慧大石

我想到了。我的更新Jenkinsfile如下:#!/usr/bin/env groovy// The above line is used to trigger correct syntax highlighting.pipeline {&nbsp; &nbsp; // Lets Jenkins use Docker for us later.&nbsp; &nbsp; agent any&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // If anything fails, the whole Pipeline stops.&nbsp; &nbsp; stages {&nbsp; &nbsp; &nbsp; &nbsp; stage('Build & Test') {&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Use golang.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; agent { docker { image 'golang' } }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; steps {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Create our project directory.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sh 'cd ${GOPATH}/src'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sh 'mkdir -p ${GOPATH}/src/MY_PROJECT_DIRECTORY'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Copy all files in our Jenkins workspace to our project directory.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sh 'cp -r ${WORKSPACE}/* ${GOPATH}/src/MY_PROJECT_DIRECTORY'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Copy all files in our "vendor" folder to our "src" folder.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sh 'cp -r ${WORKSPACE}/vendor/* ${GOPATH}/src'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Build the app.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sh 'go build'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; stage('Test') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Use golang.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; agent { docker { image 'golang' } }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; steps {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Create our project directory.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sh 'cd ${GOPATH}/src'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sh 'mkdir -p ${GOPATH}/src/MY_PROJECT_DIRECTORY'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Copy all files in our Jenkins workspace to our project directory.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sh 'cp -r ${WORKSPACE}/* ${GOPATH}/src/MY_PROJECT_DIRECTORY'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Copy all files in our "vendor" folder to our "src" folder.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sh 'cp -r ${WORKSPACE}/vendor/* ${GOPATH}/src'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Remove cached test results.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sh 'go clean -cache'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Run Unit Tests.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sh 'go test ./... -v -short'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; stage('Docker') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; environment {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Extract the username and password of our credentials into "DOCKER_CREDENTIALS_USR" and "DOCKER_CREDENTIALS_PSW".&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // (NOTE 1: DOCKER_CREDENTIALS will be set to "your_username:your_password".)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // The new variables will always be YOUR_VARIABLE_NAME + _USR and _PSW.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // (NOTE 2: You can't print credentials in the pipeline for security reasons.)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DOCKER_CREDENTIALS = credentials('my-docker-credentials-id')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; steps {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Use a scripted pipeline.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; script {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; def app&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stage('Clone repository') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; checkout scm&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stage('Build image') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app = docker.build("${env.DOCKER_CREDENTIALS_USR}/my-project-img")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stage('Push image') {&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Use the Credential ID of the Docker Hub Credentials we added to Jenkins.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; docker.withRegistry('https://registry.hub.docker.com', 'my-docker-credentials-id') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Push image and tag it with our build number for versioning purposes.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app.push("${env.BUILD_NUMBER}")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Push the same image and tag it as the latest version (appears at the top of our version list).&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app.push("latest")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; post {&nbsp; &nbsp; &nbsp; &nbsp; always {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Clean up our workspace.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; deleteDir()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}&nbsp; &nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go