{"id":1986,"date":"2023-04-01T10:51:49","date_gmt":"2023-04-01T02:51:49","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1986"},"modified":"2023-04-22T07:42:33","modified_gmt":"2023-04-21T23:42:33","slug":"jenkins-docker-automated-deployment","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/jenkins-docker-automated-deployment\/","title":{"rendered":"Jenkins+Docker\u81ea\u52a8\u5316\u90e8\u7f72"},"content":{"rendered":"<p>SpringBoot\u5e94\u7528\u6253\u5305Docker\u955c\u50cf\u90fd\u662f\u53ef\u4ee5\u901a\u8fc7Maven\u63d2\u4ef6\u6765\u5b9e\u73b0\u7684\uff0c\u4f46\u7531\u4e8e\u8fdc\u7a0b\u670d\u52a1\u5668\u9700\u8981\u5f00\u53d12375\u7aef\u53e3\uff0c\u5b58\u5728\u4e00\u5b9a\u7684\u5b89\u5168\u9690\u60a3\u3002\u4ecb\u7ecd\u53e6\u4e00\u79cd\u65b9\u6cd5\uff0c\u4f7f\u7528DockerFile+Jar+\u81ea\u52a8\u5316\u811a\u672c\u7684\u5f62\u5f0f\u6765\u90e8\u7f72\u3002<\/p>\n<h2>\u9879\u76ee\u6253\u5305<\/h2>\n<p>\u6211\u4eec\u4e0d\u4f7f\u7528Docker\u7684Maven\u63d2\u4ef6\u6765\u6253\u5305\uff0c\u5148\u5728<code>pom.xml<\/code>\u4e2d\u6ce8\u91ca\u6389\u5b83<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-xml\">&lt;build&gt;\n    &lt;plugins&gt;\n        &lt;plugin&gt;\n            &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n            &lt;artifactId&gt;spring-boot-maven-plugin&lt;\/artifactId&gt;\n        &lt;\/plugin&gt;\n&lt;!--        &lt;plugin&gt;--&gt;\n&lt;!--            &lt;groupId&gt;com.spotify&lt;\/groupId&gt;--&gt;\n&lt;!--            &lt;artifactId&gt;docker-maven-plugin&lt;\/artifactId&gt;--&gt;\n&lt;!--            &lt;version&gt;1.1.0&lt;\/version&gt;--&gt;\n&lt;!--            &lt;executions&gt;--&gt;\n&lt;!--                &lt;execution&gt;--&gt;\n&lt;!--                    &lt;id&gt;build-image&lt;\/id&gt;--&gt;\n&lt;!--                    &lt;phase&gt;package&lt;\/phase&gt;--&gt;\n&lt;!--                    &lt;goals&gt;--&gt;\n&lt;!--                        &lt;goal&gt;build&lt;\/goal&gt;--&gt;\n&lt;!--                    &lt;\/goals&gt;--&gt;\n&lt;!--                &lt;\/execution&gt;--&gt;\n&lt;!--            &lt;\/executions&gt;--&gt;\n&lt;!--            &lt;configuration&gt;--&gt;\n&lt;!--                &lt;imageName&gt;mall-tiny\/${project.artifactId}:${project.version}&lt;\/imageName&gt;--&gt;\n&lt;!--                &lt;dockerHost&gt;http:\/\/192.168.6.132:2375&lt;\/dockerHost&gt;--&gt;\n&lt;!--                &lt;baseImage&gt;java:8&lt;\/baseImage&gt;--&gt;\n&lt;!--                &lt;entryPoint&gt;[&quot;java&quot;, &quot;-jar&quot;,&quot;\/${project.build.finalName}.jar&quot;]--&gt;\n&lt;!--                &lt;\/entryPoint&gt;--&gt;\n&lt;!--                &lt;resources&gt;--&gt;\n&lt;!--                    &lt;resource&gt;--&gt;\n&lt;!--                        &lt;targetPath&gt;\/&lt;\/targetPath&gt;--&gt;\n&lt;!--                        &lt;directory&gt;${project.build.directory}&lt;\/directory&gt;--&gt;\n&lt;!--                        &lt;include&gt;${project.build.finalName}.jar&lt;\/include&gt;--&gt;\n&lt;!--                    &lt;\/resource&gt;--&gt;\n&lt;!--                &lt;\/resources&gt;--&gt;\n&lt;!--            &lt;\/configuration&gt;--&gt;\n&lt;!--        &lt;\/plugin&gt;--&gt;\n    &lt;\/plugins&gt;\n&lt;\/build&gt;<\/code><\/pre>\n<p>\u7136\u540e\u4f7f\u7528Maven\u7684<code>package<\/code>\u547d\u4ee4\u76f4\u63a5\u5c06\u5e94\u7528\u6253\u6210Jar\u5305<\/p>\n<p>\u6b64\u65f6\u5728<code>target<\/code>\u76ee\u5f55\u4e0b\u5c31\u4f1a\u751f\u6210\u4e00\u4e2aJar\u5305\uff0c\u6211\u4eec\u6253\u5305Docker\u955c\u50cf\u7684\u65f6\u5019\u4f1a\u7528\u5230\u5b83<\/p>\n<h2>DockerFile<\/h2>\n<p>\u4e3b\u8981\u662f\u5b9a\u4e49\u4e86\u5982\u4f55\u5c06Jar\u5305\u6253\u5305\u6210Docker\u955c\u50cf\uff0c\u5177\u4f53\u5185\u5bb9\u5982\u4e0b\u3002<\/p>\n<pre><code class=\"language-bash\"># \u8be5\u955c\u50cf\u9700\u8981\u4f9d\u8d56\u7684\u57fa\u7840\u955c\u50cf\nFROM java:8\n# \u5c06\u5f53\u524d\u76ee\u5f55\u4e0b\u7684jar\u5305\u590d\u5236\u5230docker\u5bb9\u5668\u7684\/\u76ee\u5f55\u4e0b\nADD mall-tiny-jenkins-1.0-SNAPSHOT.jar \/mall-tiny-jenkins-1.0-SNAPSHOT.jar\n# \u58f0\u660e\u670d\u52a1\u8fd0\u884c\u57288088\u7aef\u53e3\nEXPOSE 8088\n# \u6307\u5b9adocker\u5bb9\u5668\u542f\u52a8\u65f6\u8fd0\u884cjar\u5305\nENTRYPOINT [&quot;java&quot;, &quot;-jar&quot;,&quot;\/mall-tiny-jenkins-1.0-SNAPSHOT.jar&quot;]\n# \u6307\u5b9a\u7ef4\u62a4\u8005\u7684\u540d\u5b57\nMAINTAINER Joe.Ye<\/code><\/pre>\n<h2>\u81ea\u52a8\u5316\u811a\u672c<\/h2>\n<p>\u53ef\u4ee5\u4f5c\u4e3a\u901a\u7528\u811a\u672c\u6765\u4f7f\u7528\u7684\u6a21\u677f\u811a\u672c\uff0c\u53ea\u9700\u6539\u53d8\u5176\u4e2d\u7684\u4e00\u4e9b\u53c2\u6570\u5373\u53ef\uff0c\u5177\u4f53\u6267\u884c\u6d41\u7a0b\u4e3a\uff1a\u505c\u6b62\u65e7\u670d\u52a1-&gt;\u5220\u9664\u65e7\u5bb9\u5668-&gt;\u5220\u9664\u65e7\u955c\u50cf-&gt;\u6253\u5305\u65b0\u955c\u50cf-&gt;\u8fd0\u884c\u65b0\u955c\u50cf<\/p>\n<pre><code class=\"language-bash\">#!\/usr\/bin\/env bash\n# \u5b9a\u4e49\u5e94\u7528\u7ec4\u540d\ngroup_name=&#039;mall-tiny&#039;\n# \u5b9a\u4e49\u5e94\u7528\u540d\u79f0\napp_name=&#039;mall-tiny-jenkins&#039;\n# \u5b9a\u4e49\u5e94\u7528\u7248\u672c\napp_version=&#039;1.0-SNAPSHOT&#039;\n# \u5b9a\u4e49\u5e94\u7528\u73af\u5883\nprofile_active=&#039;qa&#039;\necho &#039;----copy jar----&#039;\ndocker stop ${app_name}\necho &#039;----stop container----&#039;\ndocker rm ${app_name}\necho &#039;----rm container----&#039;\ndocker rmi ${group_name}\/${app_name}:${app_version}\necho &#039;----rm image----&#039;\n# \u6253\u5305\u7f16\u8bd1docker\u955c\u50cf\ndocker build -t ${group_name}\/${app_name}:${app_version} .\necho &#039;----build image----&#039;\ndocker run -p 8088:8088 --name ${app_name} \\\n--link mysql:db \\\n-e &#039;spring.profiles.active&#039;=${profile_active} \\\n-e TZ=&quot;Asia\/Shanghai&quot; \\\n-v \/etc\/localtime:\/etc\/localtime \\\n-v \/mydata\/app\/${app_name}\/logs:\/var\/logs \\\n-d ${group_name}\/${app_name}:${app_version}\necho &#039;----start container----&#039;<\/code><\/pre>\n<p>\u4e0b\u9762\u8bb2\u4e0b\u81ea\u52a8\u5316\u811a\u672c\u91cc\u9762\u503c\u5f97\u6ce8\u610f\u7684\u5730\u65b9\uff1a<\/p>\n<ul>\n<li><code>group_name<\/code>\u3001<code>app_name<\/code>\u3001<code>app_version<\/code>\u53ef\u4ee5\u7528\u6765\u5b9a\u4e49\u6253\u5305\u955c\u50cf\u7684\u5c5e\u6027<\/li>\n<li><code>profile_active<\/code>\u53ef\u4ee5\u8ba9\u4f60\u7684\u5e94\u7528\u4f7f\u7528\u4e0d\u540c\u73af\u5883\u4e0b\u7684\u914d\u7f6e\uff0c\u6bd4\u5982\u4f7f\u7528<code>qa<\/code>\u53ef\u4ee5\u542f\u7528\u6d4b\u8bd5\u73af\u5883\u7684\u914d\u7f6e\uff0c\u4f7f\u7528<code>prod<\/code>\u53ef\u4ee5\u542f\u7528\u751f\u4ea7\u73af\u5883\u914d\u7f6e\uff0c\u771f\u6b63\u7684\u4e00\u5305\u591a\u7528<\/li>\n<li><code>docker rmi<\/code>\u8fd9\u6b65\u4e00\u5b9a\u8981\u6709\uff0c\u5982\u679c\u4e0d\u5220\u9664\u65e7\u955c\u50cf\uff0c\u5f53\u65b0\u955c\u50cf\u6253\u5305\u7684\u65f6\u5019\u4f1a\u4ea7\u751f<code>none<\/code>\u955c\u50cf<\/li>\n<li><code>docker run<\/code>\u547d\u4ee4\u4e2d\u7684<code>-e TZ=&quot;Asia\/Shanghai&quot;<\/code>\u65f6\u533a\u4e00\u5b9a\u8981\u8bbe\u7f6e\uff0c\u5426\u5219\u5bb9\u5668\u65f6\u95f4\u4f1a\u548c\u5bbf\u4e3b\u673a\u4f1a\u76f8\u5dee8\u4e2a\u5c0f\u65f6<\/li>\n<\/ul>\n<h2>\u90e8\u7f72\u8fd0\u884c<\/h2>\n<p>\u76f4\u63a5\u4e0a\u4f20\u6211\u4eec\u7684\u5e94\u7528Jar\u5305\u3001DockerFile\u6587\u4ef6\u548c\u81ea\u52a8\u5316\u90e8\u7f72\u811a\u672c\u5230\u6307\u5b9a\u76ee\u5f55\u4e0b\uff1a<\/p>\n<pre><code class=\"language-bash\">Dockerfile\nmall-tiny-jenkins-1.0-SNAPSHOT.jar\nrun.sh<\/code><\/pre>\n<p>Dockerfile\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"language-bash\"># \u8be5\u955c\u50cf\u9700\u8981\u4f9d\u8d56\u7684\u57fa\u7840\u955c\u50cf\nFROM java:8\n# \u5c06\u5f53\u524d\u76ee\u5f55\u4e0b\u7684jar\u5305\u590d\u5236\u5230docker\u5bb9\u5668\u7684\/\u76ee\u5f55\u4e0b\nADD mall-tiny-docker-jenkins-1.0-SNAPSHOT.jar \/mall-tiny-docker-jenkins.jar\n# \u8fd0\u884c\u8fc7\u7a0b\u4e2d\u521b\u5efa\u4e00\u4e2amall-tiny-docker-jenkins.jar\u6587\u4ef6\nRUN bash -c &#039;touch \/mall-tiny-docker-jenkins.jar&#039;\n# \u58f0\u660e\u670d\u52a1\u8fd0\u884c\u57288080\u7aef\u53e3\nEXPOSE 8080\n# \u6307\u5b9adocker\u5bb9\u5668\u542f\u52a8\u65f6\u8fd0\u884cjar\u5305\nENTRYPOINT [&quot;java&quot;, &quot;-jar&quot;,&quot;\/mall-tiny-docker-jenkins.jar&quot;]\n# \u6307\u5b9a\u7ef4\u62a4\u8005\u7684\u540d\u5b57\nMAINTAINER Joe.Ye<\/code><\/pre>\n<p>\u5c06\u81ea\u52a8\u5316\u811a\u672c\u4fee\u6539\u4e3a\u53ef\u6267\u884c\uff1a<\/p>\n<pre><code class=\"language-bash\">chmod +x run.sh<\/code><\/pre>\n<p>\u4f7f\u7528<code>.\/run.sh<\/code>\u547d\u4ee4\u76f4\u63a5\u8fd0\u884c\u811a\u672c\u5373\u53ef<\/p>\n<h2>\u7ed3\u5408Jenkins\u81ea\u52a8\u5316\u90e8\u7f72<\/h2>\n<p>\u63a8\u8350\u5b89\u88c5<code>Publish Over SSH<\/code>\u8fd9\u6b3eJenkins\u63d2\u4ef6\uff0c\u5b83\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u53ef\u4ee5\u901a\u8fc7SSH\u5728\u4e0d\u540c\u670d\u52a1\u5668\u4e4b\u95f4\u4f20\u8f93\u6587\u4ef6\u548c\u6267\u884c\u547d\u4ee4\u3002\u6bd4\u5982\u8bf4\u6211\u4eec\u628aJenkins\u88c5\u5728\u4e86\u6d4b\u8bd5\u670d\u52a1\u5668\u4e0a\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Jenkins\u5728\u6d4b\u8bd5\u670d\u52a1\u5668\u4e0a\u4eceGit\u4ed3\u5e93\u83b7\u53d6\u4ee3\u7801\uff0c\u7136\u540e\u6253\u6210Jar\u5305\u3002\u6253\u5305\u5b8c\u6210\u540e\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u63d2\u4ef6\u5c06Jar\u5305\u4f20\u8f93\u5230\u6b63\u5f0f\u670d\u52a1\u5668\u4e0a\u53bb\uff0c\u7136\u540e\u6267\u884c\u6b63\u5f0f\u670d\u52a1\u5668\u4e0a\u7684\u81ea\u52a8\u5316\u811a\u672c\uff0c\u4ece\u800c\u5b9e\u73b0\u6b63\u5f0f\u670d\u52a1\u5668\u4e0a\u7684\u81ea\u52a8\u5316\u90e8\u7f72\u3002<\/p>\n<p>\u9996\u5148\u6211\u4eec\u53ef\u4ee5\u5728<code>\u7cfb\u7edf\u7ba1\u7406<\/code>-&gt;<code>\u63d2\u4ef6\u7ba1\u7406<\/code>\u4e2d\u627e\u5230\u8be5\u63d2\u4ef6\uff0c\u7136\u540e\u8fdb\u884c\u5b89\u88c5<\/p>\n<p>\u7136\u540e\u5728<code>\u7cfb\u7edf\u7ba1\u7406<\/code>-&gt;<code>\u63d2\u4ef6\u7ba1\u7406<\/code>\u4e2d\u6dfb\u52a0\u76f8\u5e94\u7684SSH\u914d\u7f6e<\/p>\n<p>\u914d\u7f6e\u5b8c\u6210\u540e\u521b\u5efa\u4e00\u4e2a\u5e94\u7528\u7684\u6784\u5efa\u4efb\u52a1\uff0c\u5728<code>\u6784\u5efa<\/code>\u8fd9\u4e00\u6b65\u4e2d\uff0c\u6dfb\u52a0\u6784\u5efa\u6b65\u9aa4\u4e3a<code>\u901a\u8fc7SSH\u53d1\u9001\u6587\u4ef6\u5e76\u6267\u884c\u547d\u4ee4<\/code><\/p>\n<p>\u914d\u7f6e\u597d\u6211\u4eec\u7684<code>SSH Publisher<\/code>\uff0c\u4e3b\u8981\u662f\u6e90\u6587\u4ef6\u8def\u5f84\u548c\u76ee\u6807\u6587\u4ef6\u8def\u5f84\uff0c\u4ee5\u53ca\u9700\u8981\u6267\u884c\u7684\u811a\u672c<\/p>\n<pre><code class=\"language-bash\">Source files: target\/mall-tiny-jenkins-1.0-SNAPSHOT.jar\nRemove prefix files: target\nRemote directory: \/\nExec command: \n\ncd \/mydata\/deploy\n.\/run.sh<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>SpringBoot\u5e94\u7528\u6253\u5305Docker\u955c\u50cf\u90fd\u662f\u53ef\u4ee5\u901a\u8fc7Maven\u63d2\u4ef6\u6765\u5b9e\u73b0\u7684\uff0c\u4f46\u7531\u4e8e\u8fdc\u7a0b\u670d\u52a1\u5668\u9700\u8981\u5f00\u53d1237 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[179,229],"tags":[],"class_list":["post-1986","post","type-post","status-publish","format-standard","hentry","category-docker","category-jenkins"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1986","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1986"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1986\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}