程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-06(2)

Jenkins- Jenkins的代理

发布于2021-05-29 22:40     阅读(703)     评论(0)     点赞(6)     收藏(5)


一、Jenkins 代理介绍

代理就是 Jenkins 干活的地方,就是你准备让Jenkins 在那个地方完成你给它设定的任务。

代理可以安装 Jenkins 的服务器(自己),通常成为 master, 也可以是一个 slave, 也可以是一个Docker 的容器,还可以是一个 kubernetes 的 Pod.

1 在 Pipeline 中设置代理

在 Pipeline 中设置代理的方式式通过 agent 声明

全局代理和阶段代理

  • 全局代理
    agent 出现在 pipeline 块内的顶层,就是全局代理。这是必须要设置的。

全局代理边上此 jenkinsfile 中的所有阶段都将在此代理中执行。

  • 阶段代理

出现在 stage 块中,就是阶段代理。

阶段代理只作用与此阶段。

在任何可用的代理上执行流水线或阶段。

例如: agent any

pipeline {
   agent any
   stages {
      ...
   }
}

使用 label 指定具体的某一个

pipeline {
   agent { label "node1"}
   stages {
      ...
   }
}

参数
为了支持作者可能有的各种各样的用例流水线, agent 部分支持一些不同类型的参数。这些参数应用在pipeline块的顶层, 或 stage 指令内部。

any
在任何可用的代理上执行流水线或阶段。例如: agent any

none
当在 pipeline 块的顶部没有全局代理, 该参数将会被分配到整个流水线的运行中并且每个 stage 部分都需要包含他自己的 agent 部分。比如: agent none

label
在提供了标签的 Jenkins 环境中可用的代理上执行流水线或阶段。 例如: agent { label ‘my-defined-label’ }

node
agent { node { label ‘labelName’ } } 和 agent { label ‘labelName’ } 一样, 但是 node 允许额外的选项 (比如 customWorkspace )。

docker
使用给定的容器执行流水线或阶段。该容器将在预置的 node上,或在匹配可选定义的label 参数上,动态的供应来接受基于Docker的流水线。 docker 也可以选择的接受 args 参数,该参数可能包含直接传递到 docker run 调用的参数, 以及 alwaysPull 选项, 该选项强制 docker pull ,即使镜像名称已经存在。 比如: agent { docker ‘maven:3-alpine’ } 或

agent {
    docker {
        image 'maven:3-alpine'
        label 'my-defined-label'
        args  '-v /tmp:/tmp'
    }
}

kubernetes

需要 kubernetes plugin
在部署在Kubernetes集群上的pod内执行管道或stage。要使用此选项,必须从多分支管道或SCM管道加载Jenkinsfile。Pod模板是在kubernetes{}块中定义的。例如,如果您想要一个内有一个Kaniko容器的pod,您可以将其定义为:

agent {
    kubernetes {
        label podlabel
        yaml """
kind: Pod
metadata:
  name: jenkins-agent
spec:
  containers:
  - name: kaniko
    image: gcr.io/kaniko-project/executor:debug
    imagePullPolicy: Always
    command:
    - /busybox/cat
    tty: true
    volumeMounts:
      - name: aws-secret
        mountPath: /root/.aws/
      - name: docker-registry-config
        mountPath: /kaniko/.docker
  restartPolicy: Never
  volumes:
    - name: aws-secret
      secret:
        secretName: aws-secret
    - name: docker-registry-config
      configMap:
        name: docker-registry-config
"""
   }
/**
 * This pipeline will build and deploy a Docker image with Kaniko
 * https://github.com/GoogleContainerTools/kaniko
 * without needing a Docker host
 *
 * You need to create a jenkins-docker-cfg secret with your docker config
 * as described in
 * https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/#create-a-secret-in-the-cluster-that-holds-your-authorization-token
 */

podTemplate(yaml: """
kind: Pod
spec:
  containers:
  - name: kaniko
    image: gcr.io/kaniko-project/executor:debug-539ddefcae3fd6b411a95982a830d987f4214251
    imagePullPolicy: Always
    command:
    - /busybox/cat
    tty: true
    volumeMounts:
      - name: jenkins-docker-cfg
        mountPath: /kaniko/.docker
  volumes:
  - name: jenkins-docker-cfg
    projected:
      sources:
      - secret:
          name: regcred
          items:
            - key: .dockerconfigjson
              path: config.json
"""
  ) {

  node(POD_LABEL) {
    stage('Build with Kaniko') {
      git 'https://github.com/jenkinsci/docker-inbound-agent.git'
      container('kaniko') {
        sh '/kaniko/executor -f `pwd`/Dockerfile -c `pwd` --insecure --skip-tls-verify --cache=true --destination=mydockerregistry:5000/myorg/myimage'
      }
    }
  }
}

原文链接:https://blog.csdn.net/qq_22648091/article/details/116865236



所属网站分类: 技术文章 > 博客

作者:skdk

链接:http://www.javaheidong.com/blog/article/207670/9287b400f608df8442eb/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

6 0
收藏该文
已收藏

评论内容:(最多支持255个字符)