当前位置: 首页 > news >正文

Jenkins plugin 的用法和示例

今天介绍一下比较常见的Jenkins plugin 的使用方法

1. 通过AWS s3 upload 插件上传文件到AWS S3 存储桶

前提条件:

  • 安装AWS pipeline step插件
  • 在Jenkins 中创建credentials,包含access_key_id和secret_key_id
  • 创建S3 存储桶

脚本:

pipeline{agent { label 'master' } stages{stage("Aws test credentials"){steps{withAWS(credentials: 'jenkins-test', region: 'ap-southeast-2'){sh 'echo "Hello DevOps" > hello.txt's3Upload (acl: 'Private' , bucket: 'jenkins-s3-upload-test01' , file: 'hello.txt')s3Download(file:'downloaded.txt', bucket:'jenkins-s3-upload-test01', path:'hello.txt',force:true) sh "cat downloaded.txt"}}}}
}

效果:

2. 在Jenkins pipeline 中assume AWS role 

前提条件:

  • 创建IAM role 并设置trust relationship 
{"Version": "2012-10-17","Statement": [{"Sid": "Statement1","Effect": "Allow","Principal": {"AWS": "arn:aws:iam::654654314383:user/jenkins-test"},"Action": ["sts:AssumeRole"]}]
}
  • 确保您assume 的 IAM 角色具有在管道内执行 AWS CLI 命令所需的权限。

脚本:

pipeline {agent {label 'master'}environment {PATH = "/opt/homebrew/bin:$PATH"}stages {stage('Assume Role') {steps {withAWS(credentials: 'jenkins-test', , region: 'ap-southeast-2') {withAWS(role: 'arn:aws:iam::654654314383:role/jenkins-assumerole', region: 'ap-southeast-2') {sh 'aws --version'sh 'aws s3 ls s3://jenkins-s3-upload-test01'  // Example S3 CLI command}}}}}
}

验证:

3. 通过Email ext 插件发送邮件通知

前提条件:

  • 安装Email ext plugin 
  • 在Jenkins 上配置SMTP 邮件服务器,并保证可以测试成功

脚本:

pipeline{agent { label 'master' } stages {stage('Gmail')
{steps{emailext body: "*${currentBuild.currentResult}:* Job Name: ${env.JOB_NAME} || Build Number: ${env.BUILD_NUMBER}\nMore information at: ${env.BUILD_URL}",subject: 'Declarative Pipeline Build Status',to: 'rockwang415@gmail.com'}}}
}

验证:

4. Docker plugin for Jenkins

许多组织使用 Docker 来统一跨机器的构建和测试环境,并提供高效的应用程序部署机制。从 Pipeline 2.5 及更高版本开始,Pipeline 内置了在 Jenkinsfile 中与 Docker 交互的支持。

前提条件:

  • 安装docker plugin

  • 在salve 节点上安装docker
pipeline {agent anystages {stage('Build') {steps {script {// Run in a Docker containerdocker.image('gradle:8.2.0-jdk17-alpine').inside {// Run the build commandsh 'gradle --version'}}}}}
}

 验证:

5. Kubernetes plugin for Jenkins

前提条件

  • 安装kubernetes 插件
  • 创建kubernetes secret file(kubeconfig)
  • 添加kubernetes 节点到Jenkins cloud,并确保可以正常连接到kubernetes 

  • 创建 POD 和容器模板

脚本:

podTemplate(containers: [containerTemplate(name: 'jnlp', image: 'jenkins/inbound-agent:latest')
]) {node('kubeagent') { // Replace POD_LABEL with 'kubeagent'stage('Get a Maven project') {container('jnlp') {stage('Shell Execution') {sh '''echo "Hello! I am executing shell"'''}}}}
}

验证:

Started by user admin
[Pipeline] Start of Pipeline (hide)
[Pipeline] podTemplate
[Pipeline] {
[Pipeline] node
Still waiting to schedule task
‘pod-template-vc6lb’ is offline
Agent pod-template-vc6lb is provisioned from template pod-template
---
apiVersion: "v1"
kind: "Pod"
metadata:annotations:kubernetes.jenkins.io/last-refresh: "1745298965608"labels:jenkins: "slave"jenkins/label-digest: "ffa3ba115a1a18165cef0867902fabef92179d38"jenkins/label: "kubeagent"kubernetes.jenkins.io/controller: "http___192_168_1_2_8080x"name: "pod-template-vc6lb"namespace: "default"
spec:containers:- env:- name: "JENKINS_SECRET"value: "********"- name: "REMOTING_OPTS"value: "-noReconnectAfter 1d"- name: "JENKINS_AGENT_NAME"value: "pod-template-vc6lb"- name: "JENKINS_NAME"value: "pod-template-vc6lb"- name: "JENKINS_AGENT_WORKDIR"value: "/home/jenkins/agent"- name: "JENKINS_URL"value: "http://192.168.1.2:8080/"image: "jenkins/inbound-agent:latest"imagePullPolicy: "Always"name: "jnlp"resources: {}tty: falsevolumeMounts:- mountPath: "/home/jenkins/agent"name: "workspace-volume"readOnly: falseworkingDir: "/home/jenkins/agent"hostNetwork: falsenodeSelector:kubernetes.io/os: "linux"restartPolicy: "Never"volumes:- emptyDir:medium: ""name: "workspace-volume"Running on pod-template-vc6lb in /home/jenkins/agent/workspace/kubernetes-2
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Get a Maven project)
[Pipeline] container
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Shell Execution)
[Pipeline] sh
+ echo Hello! I am executing shell
Hello! I am executing shell
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
Finished: SUCCESS

6. 多容器 Pod 模板

前提条件:

添加多个container template:

脚本: 

pipeline {agent nonestages {stage('Deploy') {steps {podTemplate(label: 'kubeagent', containers: [containerTemplate(name: 'jnlp', image: 'jenkins/inbound-agent:latest'),containerTemplate(name: 'python',image: 'python:latest')]) {node('kubeagent') {container('python') {// Invoke commands or build steps here using kubeagentsh 'echo "Running on kubeagent container python"'}container('jnlp') {// Invoke commands or build steps here using kubeagentsh 'echo "Running on kubeagent container jnlp"'}}}}}}
}

验证:

 

http://www.xdnf.cn/news/80083.html

相关文章:

  • Python 设计模式:桥接模式
  • 电商虚拟户分账系统:破解电商资金管理难题的密钥
  • 数据安全,从治理体系开始认清全局
  • 【音视频】AAC-ADTS分析
  • transformer预测寿命
  • 【音视频】FFmpeg内存模型
  • 香港免费云服务器申请教程,配置4核8G
  • 【Maven】配置文件
  • 网络威胁情报 | Friday Overtime Trooper
  • VB.NET 2008影音播放器开发指南
  • 量子计算在密码学中的应用与挑战:重塑信息安全的未来
  • Git,本地上传项目到github
  • 超越GPT-4?下一代大模型的技术突破与挑战
  • OpenLDAP 管理 ELK 用户
  • 运行neo4j.bat console 报错无法识别为脚本,PowerShell 教程:查看语言模式并通过注册表修改受限模式
  • DeepSeek开源引爆AI Agent革命:应用生态迎来“安卓时刻”
  • 【Python】Selenium切换网页的标签页的写法(全!!!)
  • 力扣hot100 LeetCode 热题 100 Java 哈希篇
  • Spring之我见 - Spring MVC重要组件和基本流程
  • N8N 官方 MCP 节点实战指南:AI 驱动下的多工具协同应用场景全解析
  • 多台电脑切换解决方案:KVM 切换器
  • 小技巧1,在vue3中利用自定义ref实现防抖(customRef)
  • 晨控CK-FR12与欧姆龙NX系列PLC配置EtherNet/IP通讯连接操作手册
  • C++_并发编程_thread_01_基本应用
  • LoRA微调技术全景解析:大模型高效适配的革新之道
  • 【RuleUtil】适用于全业务场景的规则匹配快速开发工具
  • ffmpeg 硬解码相关知识
  • spark-SQL实验
  • 川翔云电脑32G大显存集群机器上线!
  • 进阶篇 第 6 篇:时间序列遇见机器学习与深度学习