{"id":207018,"date":"2025-07-08T09:29:40","date_gmt":"2025-07-08T01:29:40","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207018\/"},"modified":"2025-07-08T09:29:40","modified_gmt":"2025-07-08T01:29:40","slug":"jenkins-golang-%e5%a3%b0%e6%98%8e%e5%bc%8f%e7%ae%a1%e9%81%93%ef%bc%9a%e6%9e%84%e5%bb%ba-docker-%e6%98%a0%e5%83%8f%e5%b9%b6%e6%8e%a8%e9%80%81%e5%88%b0-docker-hub","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207018\/","title":{"rendered":"Jenkins Golang \u58f0\u660e\u5f0f\u7ba1\u9053\uff1a\u6784\u5efa Docker \u6620\u50cf\u5e76\u63a8\u9001\u5230 Docker Hub"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>Jenkins Golang \u58f0\u660e\u5f0f\u7ba1\u9053\uff1a\u6784\u5efa Docker \u6620\u50cf\u5e76\u63a8\u9001\u5230 Docker Hub<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 15:27:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>Golang\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300aJenkins Golang \u58f0\u660e\u5f0f\u7ba1\u9053\uff1a\u6784\u5efa Docker \u6620\u50cf\u5e76\u63a8\u9001\u5230 Docker Hub\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u4e3a\u6211\u7684 golang \u9879\u76ee\u521b\u5efa\u4e00\u4e2a docker \u955c\u50cf\uff0c\u5e76\u901a\u8fc7 jenkins declarative pipeline \u5c06\u5176\u4e0a\u4f20\u5230 docker hub\u3002<\/p>\n<p>\u6211\u80fd\u591f\u6784\u5efa\u6211\u7684\u9879\u76ee\u5e76\u8fd0\u884c\u6240\u6709\u6d4b\u8bd5\u3002<\/p>\n<p>\u6211\u7684<code>jenkinsfile<\/code>\u5982\u4e0b\uff1a<\/p>\n<pre>#!\/usr\/bin\/env groovy\n\/\/ the above line is used to trigger correct syntax highlighting.\n\npipeline {\n    agent { docker { image 'golang' } }\n\n    stages {\n        stage('build') {   \n            steps {                                           \n                \/\/ create our project directory.\n                sh 'cd ${gopath}\/src'\n                sh 'mkdir -p ${gopath}\/src\/my_project_directory'\n\n                \/\/ copy all files in our jenkins workspace to our project directory.                \n                sh 'cp -r ${workspace}\/* ${gopath}\/src\/my_project_directory'\n\n                \/\/ copy all files in our \"vendor\" folder to our \"src\" folder.\n                sh 'cp -r ${workspace}\/vendor\/* ${gopath}\/src'\n\n                \/\/ build the app.\n                sh 'go build'\n            }            \n        }\n\n        \/\/ each \"sh\" line (shell command) is a step,\n        \/\/ so if anything fails, the pipeline stops.\n        stage('test') {\n            steps {                                \n                \/\/ remove cached test results.\n                sh 'go clean -cache'\n\n                \/\/ run unit tests.\n                sh 'go test .\/... -v'                                  \n            }\n        }           \n    }\n}<\/pre>\n<p>\u6211\u7684<code>dockerfile<\/code>\uff08\u5982\u679c\u9700\u8981\uff09\u5982\u4e0b\uff1a<\/p>\n<pre># make a golang container from the \"golang alpine\" docker image from docker hub.\nfrom golang:1.11.2-alpine3.8\n\n# expose our desired port.\nexpose 9000\n\n# create the proper directory.\nrun mkdir -p $gopath\/src\/my_project_directory\n\n# copy app to the proper directory for building.\nadd . $gopath\/src\/my_project_directory\n\n# set the work directory.\nworkdir $gopath\/src\/my_project_directory\n\n# run cmd commands.\nrun go get -d -v .\/...\nrun go install -v .\/...\n\n# provide defaults when running the container.\n# these will be executed after the entrypoint.\n# for example, if you ran docker run &lt;image&gt;,\n# then the commands and parameters specified by cmd would be executed.\ncmd [\"my_project\"]<\/pre>\n<p>\u6211\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u83b7\u53d6\u6211\u7684 docker hub \u51ed\u636e\uff1a<\/p>\n<pre>environment {\n    \/\/ extract the username and password of our credentials into \"docker_credentials_usr\" and \"docker_credentials_psw\".\n    \/\/ (note 1: docker_credentials will be set to \"your_username:your_password\".)\n    \/\/ the new variables will always be your_variable_name + _usr and _psw.\n    docker_credentials = credentials('my_jenkins_credential_id')\n}<\/pre>\n<p>\u4f5c\u4e3a\u53c2\u8003\uff0c\u4ee5\u4e0b\u662f\u6211\u5728 freestyle \u5de5\u4f5c\u4e2d\u6240\u505a\u7684\u5de5\u4f5c\uff1a<\/p>\n<pre>Execute Shell:\n\n    # Log in to Docker.\n    sudo docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD\n\n    # Make a Docker image for our app.\n    cd \/var\/lib\/jenkins\/tools\/org.jenkinsci.plugins.golang.GolangInstallation\/Go\/src\/MY_PROJECT_DIRECTORY\/\n    sudo docker build -t MY-PROJECT-img .\n\n    # Tag our Docker image.\n    sudo docker tag  MY-PROJECT-img   $DOCKER_USERNAME\/MY-PROJECT-img\n\n    # Push our Docker image to Docker Hub.\n    sudo docker push $DOCKER_USERNAME\/MY-PROJECT-img\n\n    # Log out of Docker.\n    docker logout<\/pre>\n<p>\u6ce8\u610f\uff1a\u6211\u901a\u8fc7\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\u6388\u4e88\u4e86 jenkins sudo \u6743\u9650\uff1a<\/p>\n<ol>\n<li>\n<p>\u6253\u5f00 sudoers \u6587\u4ef6\u8fdb\u884c\u7f16\u8f91\uff1a<\/p>\n<p>sudo visudo -f \/etc\/sudoers<\/p>\n<\/li>\n<li>\n<p>\u6309\u201ci\u201d\u8fdb\u5165 insert \u6a21\u5f0f\u3002<\/p>\n<\/li>\n<li>\n<p>\u7c98\u8d34\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<p>\u8a79\u91d1\u65af all=(all) nopasswd: all<\/p>\n<\/li>\n<li>\n<p>\u6309\u201cesc\u201d\u9000\u51fa\u63d2\u5165\u6a21\u5f0f\u3002<\/p>\n<\/li>\n<li>\n<p>\u8981\u4fdd\u5b58\uff0c\u8bf7\u8f93\u5165\uff1a<\/p>\n<p>\uff1aw<\/p>\n<\/li>\n<li>\n<p>\u8981\u9000\u51fa\uff0c\u8bf7\u8f93\u5165\uff1a<\/p>\n<p>:q<\/p>\n<\/li>\n<\/ol>\n<p>\u6211\u76f8\u4fe1\u901a\u8fc7\u5728\u6211\u7684\u58f0\u660e\u5f0f\u7ba1\u9053\u4e2d\u6dfb\u52a0\u65b0\u7684 <code>stage<\/code> \u548c <code>agent<\/code> \u5e94\u8be5\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u4f46\u6211\u5728\u7f51\u4e0a\u6ca1\u6709\u627e\u5230\u4efb\u4f55\u5185\u5bb9\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u6211\u660e\u767d\u4e86\u3002<\/p>\n<p>\u6211\u66f4\u65b0\u7684<code>jenkinsfile<\/code>\u5982\u4e0b\uff1a<\/p>\n<pre>#!\/usr\/bin\/env groovy\n\/\/ The above line is used to trigger correct syntax highlighting.\n\npipeline {\n    \/\/ Lets Jenkins use Docker for us later.\n    agent any    \n\n    \/\/ If anything fails, the whole Pipeline stops.\n    stages {\n        stage('Build &amp; Test') {   \n            \/\/ Use golang.\n            agent { docker { image 'golang' } }\n\n            steps {                                           \n                \/\/ Create our project directory.\n                sh 'cd ${GOPATH}\/src'\n                sh 'mkdir -p ${GOPATH}\/src\/MY_PROJECT_DIRECTORY'\n\n                \/\/ Copy all files in our Jenkins workspace to our project directory.                \n                sh 'cp -r ${WORKSPACE}\/* ${GOPATH}\/src\/MY_PROJECT_DIRECTORY'\n\n                \/\/ Copy all files in our \"vendor\" folder to our \"src\" folder.\n                sh 'cp -r ${WORKSPACE}\/vendor\/* ${GOPATH}\/src'\n\n                \/\/ Build the app.\n                sh 'go build'               \n            }            \n        }\n\n        stage('Test') {\n            \/\/ Use golang.\n            agent { docker { image 'golang' } }\n\n            steps {                 \n                \/\/ Create our project directory.\n                sh 'cd ${GOPATH}\/src'\n                sh 'mkdir -p ${GOPATH}\/src\/MY_PROJECT_DIRECTORY'\n\n                \/\/ Copy all files in our Jenkins workspace to our project directory.                \n                sh 'cp -r ${WORKSPACE}\/* ${GOPATH}\/src\/MY_PROJECT_DIRECTORY'\n\n                \/\/ Copy all files in our \"vendor\" folder to our \"src\" folder.\n                sh 'cp -r ${WORKSPACE}\/vendor\/* ${GOPATH}\/src'\n\n                \/\/ Remove cached test results.\n                sh 'go clean -cache'\n\n                \/\/ Run Unit Tests.\n                sh 'go test .\/... -v -short'            \n            }\n        }      \n\n        stage('Docker') {         \n            environment {\n                \/\/ Extract the username and password of our credentials into \"DOCKER_CREDENTIALS_USR\" and \"DOCKER_CREDENTIALS_PSW\".\n                \/\/ (NOTE 1: DOCKER_CREDENTIALS will be set to \"your_username:your_password\".)\n                \/\/ The new variables will always be YOUR_VARIABLE_NAME + _USR and _PSW.\n                \/\/ (NOTE 2: You can't print credentials in the pipeline for security reasons.)\n                DOCKER_CREDENTIALS = credentials('my-docker-credentials-id')\n            }\n\n            steps {                           \n                \/\/ Use a scripted pipeline.\n                script {\n                    node {\n                        def app\n\n                        stage('Clone repository') {\n                            checkout scm\n                        }\n\n                        stage('Build image') {                            \n                            app = docker.build(\"${env.DOCKER_CREDENTIALS_USR}\/my-project-img\")\n                        }\n\n                        stage('Push image') {  \n                            \/\/ Use the Credential ID of the Docker Hub Credentials we added to Jenkins.\n                            docker.withRegistry('https:\/\/registry.hub.docker.com', 'my-docker-credentials-id') {                                \n                                \/\/ Push image and tag it with our build number for versioning purposes.\n                                app.push(\"${env.BUILD_NUMBER}\")                      \n\n                                \/\/ Push the same image and tag it as the latest version (appears at the top of our version list).\n                                app.push(\"latest\")\n                            }\n                        }              \n                    }                 \n                }\n            }\n        }\n    }\n\n    post {\n        always {\n            \/\/ Clean up our workspace.\n            deleteDir()\n        }\n    }\n}<\/pre>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aJenkins Golang \u58f0\u660e\u5f0f\u7ba1\u9053\uff1a\u6784\u5efa Docker \u6620\u50cf\u5e76\u63a8\u9001\u5230 Docker Hub\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207018","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207018","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207018"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207018\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}