亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Jenkins Golang 聲明式管道:構建 Docker 鏡像并推送到 Docker Hub

Jenkins Golang 聲明式管道:構建 Docker 鏡像并推送到 Docker Hub

Go
POPMUISE 2023-05-08 15:49:31
我正在嘗試創建我的 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"]
查看完整描述

1 回答

?
智慧大石

TA貢獻1946條經驗 獲得超3個贊

我想到了。


我的更新Jenkinsfile如下:


#!/usr/bin/env groovy

// The above line is used to trigger correct syntax highlighting.


pipeline {

    // Lets Jenkins use Docker for us later.

    agent any    


    // If anything fails, the whole Pipeline stops.

    stages {

        stage('Build & Test') {   

            // Use golang.

            agent { docker { image 'golang' } }


            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'               

            }            

        }


        stage('Test') {

            // Use golang.

            agent { docker { image 'golang' } }


            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'


                // Remove cached test results.

                sh 'go clean -cache'


                // Run Unit Tests.

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

            }

        }      


        stage('Docker') {         

            environment {

                // Extract the username and password of our credentials into "DOCKER_CREDENTIALS_USR" and "DOCKER_CREDENTIALS_PSW".

                // (NOTE 1: DOCKER_CREDENTIALS will be set to "your_username:your_password".)

                // The new variables will always be YOUR_VARIABLE_NAME + _USR and _PSW.

                // (NOTE 2: You can't print credentials in the pipeline for security reasons.)

                DOCKER_CREDENTIALS = credentials('my-docker-credentials-id')

            }


            steps {                           

                // Use a scripted pipeline.

                script {

                    node {

                        def app


                        stage('Clone repository') {

                            checkout scm

                        }


                        stage('Build image') {                            

                            app = docker.build("${env.DOCKER_CREDENTIALS_USR}/my-project-img")

                        }


                        stage('Push image') {  

                            // Use the Credential ID of the Docker Hub Credentials we added to Jenkins.

                            docker.withRegistry('https://registry.hub.docker.com', 'my-docker-credentials-id') {                                

                                // Push image and tag it with our build number for versioning purposes.

                                app.push("${env.BUILD_NUMBER}")                      


                                // Push the same image and tag it as the latest version (appears at the top of our version list).

                                app.push("latest")

                            }

                        }              

                    }                 

                }

            }

        }

    }


    post {

        always {

            // Clean up our workspace.

            deleteDir()

        }

    }

}   



查看完整回答
反對 回復 2023-05-08
  • 1 回答
  • 0 關注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號