Jenkins基础:Jenkinsfile使用实例:12:使用docker.build构建镜像

在前面一篇文章中我们介绍了使用在Jenkinsfile中使用dockerfile来动态生成镜像,从而在指定的阶段进行使用。而对于将构建生成的二进制应用进行容器化,比如将spring boot应用的jar文件打包生成一个基于JRE或者JDK基础的镜像,这个可以使用docker.build来实现,这篇文章将继续结合示例来进行介绍。

环境准备

本文使用Easypack的LTS Jenkins 2.176.1版,环境准备请参看

  • https://liumiaocn.blog.csdn.net/article/details/92764938

获取Jenkins-Crumb

使用如下示例代码获取Jenkins-Crumb,为使用API方式为示例作准备。

liumiaocn:jenkins liumiao$ jenkins_host_url=http://localhost:32002
liumiaocn:jenkins liumiao$ user_passwd="root:liumiaocn"
liumiaocn:jenkins liumiao$ jenkins_crumb=`curl -u $user_passwd ${jenkins_host_url}'/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)' 2>/dev/null`
liumiaocn:jenkins liumiao$ echo $jenkins_crumb
Jenkins-Crumb:6af0183fd58dea3d16f393b9e3272747
liumiaocn:jenkins liumiao$ 

创建Job

使用如下代码示例创建Job

liumiaocn:jenkins liumiao$ ls demo/pipeline/docker/build/config.xml
demo/pipeline/docker/build/config.xml
liumiaocn:jenkins liumiao$ cat demo/pipeline/docker/build/config.xml
<?xml version='1.1' encoding='UTF-8'?>
<flow-definition plugin="workflow-job@2.32">
  <actions>
    <org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobAction plugin="pipeline-model-definition@1.3.8"/>
  </actions>
  <description>Pipeline Job Sample</description>
  <keepDependencies>false</keepDependencies>
  <properties>
    <hudson.plugins.jira.JiraProjectProperty plugin="jira@3.0.7"/>
    <com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty plugin="gitlab-plugin@1.5.12">
      <gitLabConnection></gitLabConnection>
    </com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty>
  </properties>
  <definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps@2.69">
  <script>node {
       stage('Prepare Dockerfile') {
        sh 'cp /tmp/Dockerfile .' 
       }
       stage('Build with default Dockerfile') {
        docker.build('my-nginx:v1')
       } 
       stage('Build with specified dir') {
        docker.build("my-nginx:${env.BUILD_ID}","./")
       }
       stage('Build with specified Dockerfile') {
        docker.build("my-nginx:v2","-f Dockerfile ./")
       }
  }</script>
  <sandbox>false</sandbox>
  </definition>
  <triggers/>
  <disabled>false</disabled>
</flow-definition>
liumiaocn:jenkins liumiao$ curl -X POST -u $user_passwd -H ${jenkins_crumb} -H "Content-Type:application/xml"  --data-binary  "@demo/pipeline/docker/build/config.xml" ${jenkins_host_url}/createItem?name=pipeline_job_build
liumiaocn:jenkins liumiao$ echo $?
0
liumiaocn:jenkins liumiao$

Jenkinsfile说明

本文示例所使用的Jenkinsfile信息如下所示,内容非常简单易读,简单说明如下:

  • node是结构,在其中可以指定stage等信息
  • stage为具体的节点,比如本文示例中模拟实际的准备Dockerfile、构建镜像等操作
  • 本示例中使用了三种镜像构建方式,虽然使用了Dockerfile作为名称,缺省的情况向是指当前目录下的Dockerfile;第二种方式只指定目录名,Dockerfile作为缺省名称;第三种通过-f指定名称 然后紧跟所在目录。
node {
       stage('Prepare Dockerfile') {
        sh 'cp /tmp/Dockerfile .' 
       }
       stage('Build with default Dockerfile') {
        docker.build('my-nginx:v1')
       } 
       stage('Build with specified dir') {
        docker.build("my-nginx:${env.BUILD_ID}","./")
       }
       stage('Build with specified Dockerfile') {
        docker.build("my-nginx:v2","-f Dockerfile ./")
       }
  }

执行Job

  • Dockerfile准备
    Dockerfile内容如下所示
liumiaocn:jenkins liumiao$ docker exec -it jenkins_jenkins_1 sh
/ # cat /tmp/Dockerfile 
FROM nginx:latest
/ # 
  • 事前确认
liumiaocn:jenkins liumiao$ docker images |grep my-nginx
liumiaocn:jenkins liumiao$ 

使用如下命令或者直接在Jenkins上点击构建

liumiaocn:jenkins liumiao$ curl -X POST -u $user_passwd -H ${jenkins_crumb} ${jenkins_host_url}/job/pipeline_job_build/build
liumiaocn:jenkins liumiao$ echo $?
0
liumiaocn:jenkins liumiao$

确认执行结果

使用如下命令可以确认相关的执行日志信息

liumiaocn:jenkins liumiao$ curl -u $user_passwd ${jenkins_host_url}/job/pipeline_job_build/1/consoleText
Started by user root
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /data/jenkins/workspace/pipeline_job_build
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Prepare Dockerfile)
[Pipeline] sh
+ cp /tmp/Dockerfile .
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build with default Dockerfile)
[Pipeline] sh
+ docker build -t my-nginx:v1 .
Sending build context to Docker daemon  2.048kB
Step 1/1 : FROM nginx:latest
 ---> e445ab08b2be
Successfully built e445ab08b2be
Successfully tagged my-nginx:v1
[Pipeline] dockerFingerprintFrom
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build with specified dir)
[Pipeline] sh
+ docker build -t my-nginx:1 ./
Sending build context to Docker daemon  2.048kB
Step 1/1 : FROM nginx:latest
 ---> e445ab08b2be
Successfully built e445ab08b2be
Successfully tagged my-nginx:1
[Pipeline] dockerFingerprintFrom
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build with specified Dockerfile)
[Pipeline] sh
+ docker build -t my-nginx:v2 -f Dockerfile ./
Sending build context to Docker daemon  2.048kB
Step 1/1 : FROM nginx:latest
 ---> e445ab08b2be
Successfully built e445ab08b2be
Successfully tagged my-nginx:v2
[Pipeline] dockerFingerprintFrom
liumiaocn:jenkins liumiao$ 

blueocean构建结果确认

在这里插入图片描述

构建后镜像确认

liumiaocn:jenkins liumiao$ docker images |grep my-nginx
my-nginx                                        1                               e445ab08b2be        2 months ago        126MB
my-nginx                                        v1                              e445ab08b2be        2 months ago        126MB
my-nginx                                        v2                              e445ab08b2be        2 months ago        126MB
liumiaocn:jenkins liumiao$

注意事项

环境准备的docker-compose.yml中需要包含如下内容,以保证能够以容器方式运行的Jenkins能够进行镜像的生成。
- /var/run/docker.sock:/var/run/docker.sock
为了保证Jenkins-Crumb能够正常获取,不要设定如下Jenkins收起启动跳过插件内容的设定,以保证API的正常执行。
JAVA_OPTS=-Djenkins.install.runSetupWizard=false

参考内容

https://jenkins.io/doc/book/pipeline/docker/

淼叔 CSDN认证博客专家 神经网络 TensorFlow NLP
资深架构师,PMP、OCP、CSM、HPE University讲师,EXIN DevOps Professional与DevOps Master认证讲师,曾担任HPE GD China DevOps & Agile Leader,帮助企业级客户提供DevOps咨询培训以及实施指导。熟悉通信和金融领域,有超过十年金融外汇行业的架构设计、开发、维护经验,在十几年的IT从业生涯中拥有了软件开发设计领域接近全生命周期的经验和知识积累,著有企业级DevOps技术与工具实战。
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页