Skip to content

Commit

Permalink
Merge pull request #40 from nekoimi/master
Browse files Browse the repository at this point in the history
更新jenkins最低支持版本到2.401.3
  • Loading branch information
nekoimi authored Aug 7, 2024
2 parents 6ea6653 + eeec8d3 commit 8dcb836
Show file tree
Hide file tree
Showing 15 changed files with 266 additions and 115 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/cd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ on:
types:
- completed

permissions:
checks: read
contents: write

jobs:
maven-cd:
uses: jenkins-infra/github-reusable-workflows/.github/workflows/maven-cd.yml@v1
with:
java-version: 11
secrets:
MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }}
MAVEN_TOKEN: ${{ secrets.MAVEN_TOKEN }}
12 changes: 6 additions & 6 deletions .mvn/extensions.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<extensions xmlns="http://maven.apache.org/EXTENSIONS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/EXTENSIONS/1.0.0 http://maven.apache.org/xsd/core-extensions-1.0.0.xsd">
<extension>
<groupId>io.jenkins.tools.incrementals</groupId>
<artifactId>git-changelist-maven-extension</artifactId>
<version>1.7</version>
</extension>
</extensions>
<extension>
<groupId>io.jenkins.tools.incrementals</groupId>
<artifactId>git-changelist-maven-extension</artifactId>
<version>1.7</version>
</extension>
</extensions>
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ node {
text: [
'hello',
'world',
'env: ${ENV_NAME}' // 使用环境变量
"env: ${ENV_NAME}", // 使用环境变量
"params: ${params.name}", // 使用构建参数变量
"displayName: ${currentBuild.displayName}", // 使用全局变量
]
)
Expand All @@ -45,7 +47,9 @@ node {
'- ppppppppp',
'- ppppppppp',
'> content',
'env: ${ENV_NAME}' // 使用环境变量
"env: ${ENV_NAME}", // 使用环境变量
"params: ${params.name}", // 使用构建参数变量
"displayName: ${currentBuild.displayName}", // 使用全局变量
]
)
Expand Down
4 changes: 1 addition & 3 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
version: "3.6"
services:
jenkins:
image: jenkins/jenkins:2.319.3-jdk8
image: jenkins/jenkins:2.387.3-jdk17
ports:
- "8080:8080"
restart: on-failure
volumes:
- .data:/var/jenkins_home
41 changes: 33 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.51</version>
<!-- 插件pom:https://github.com/jenkinsci/plugin-pom -->
<version>4.55</version>
<relativePath/>
</parent>
<groupId>io.jenkins.plugins</groupId>
Expand All @@ -16,17 +17,17 @@
<url>https://github.com/jenkinsci/${project.artifactId}-plugin</url>

<properties>
<revision>1.0.1</revision>
<revision>1.0.2</revision>
<changelist>999999-SNAPSHOT</changelist>
<!-- https://www.jenkins.io/doc/developer/plugin-development/choosing-jenkins-baseline/ -->
<jenkins.version>2.324</jenkins.version>
<jenkins.version>2.401.3</jenkins.version>
<gitHubRepo>jenkinsci/${project.artifactId}-plugin</gitHubRepo>
</properties>

<developers>
<developer>
<id>nekoimi</id>
<name>JinBo Yang</name>
<name>nekoimi</name>
<email>nekoimime@gmail.com</email>
</developer>
</developers>
Expand All @@ -48,10 +49,10 @@
<dependencyManagement>
<dependencies>
<dependency>
<!-- Pick up common dependencies for the selected LTS line: https://github.com/jenkinsci/bom#usage -->
<!-- 版本以及选择 LTS 说明: https://github.com/jenkinsci/bom#usage -->
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.319.x</artifactId>
<version>1654.vcb_69d035fa_20</version>
<artifactId>bom-2.401.x</artifactId>
<version>2745.vc7b_fe4c876fa_</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand All @@ -63,10 +64,34 @@
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>jackson2-api</artifactId>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>token-macro</artifactId>
<version>2.15</version>
</dependency>
<dependency>
<groupId>org.kohsuke.stapler</groupId>
<artifactId>stapler</artifactId>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-step-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>jenkins-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/io/jenkins/plugins/wxwork/WXWorkRobotProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.verb.POST;

import java.util.Objects;

/**
* <p>WXWorkRobotProperty</p>
*
Expand Down Expand Up @@ -125,13 +127,16 @@ public FormValidation doTest(@QueryParameter("id") String id, @QueryParameter("n
RobotProperty property = new WXWorkRobotProperty(id, name, webhook);
RobotRequest message = TextMessage.builder().content("企业微信机器人测试成功!").atAll(true).build();
RobotResponse robotResponse = WXWorkRobotMessageSender.instance().send(property, message);
if (robotResponse != null && robotResponse.isOk()) {
// ok
String rootUrl = Jenkins.get().getRootUrl();
return FormValidation.respond(FormValidation.Kind.OK, "<img src='" + rootUrl + "/images/16x16/accept.png'>" + "<span style='padding-left:4px;color:#52c41a;font-weight:bold;'>测试成功</span>");
} else {
return FormValidation.error(robotResponse.errorMessage());
if (Objects.nonNull(robotResponse)) {
if (robotResponse.isOk()) {
// ok
String rootUrl = Jenkins.get().getRootUrl();
return FormValidation.respond(FormValidation.Kind.OK, "<img src='" + rootUrl + "/images/16x16/accept.png'>" + "<span style='padding-left:4px;color:#52c41a;font-weight:bold;'>测试成功</span>");
} else {
return FormValidation.error(robotResponse.errorMessage());
}
}
return FormValidation.error("企业微信机器人测试出现错误!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,36 @@
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractProject;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import io.jenkins.plugins.wxwork.bo.RobotPipelineVars;
import io.jenkins.plugins.wxwork.bo.RunUser;
import io.jenkins.plugins.wxwork.contract.RobotMessageSender;
import io.jenkins.plugins.wxwork.contract.RobotProperty;
import io.jenkins.plugins.wxwork.contract.RobotRequest;
import io.jenkins.plugins.wxwork.contract.RobotResponse;
import io.jenkins.plugins.wxwork.contract.RobotMessageSender;
import io.jenkins.plugins.wxwork.enums.MessageType;
import io.jenkins.plugins.wxwork.factory.RobotMessageFactory;
import io.jenkins.plugins.wxwork.robot.WXWorkRobotMessageSender;
import io.jenkins.plugins.wxwork.utils.JenkinsUtils;
import jenkins.tasks.SimpleBuildStep;
import lombok.Getter;
import lombok.Setter;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.*;

/**
* <p>Pipeline支持</p>
*
* @author nekoimi 2022/07/12
* @author nekoimi 2024/8/6 23:56
*/
@Getter
@Setter
@SuppressWarnings("unused")
public class WXWorkPipelineBuilder extends Builder implements SimpleBuildStep {

public class WXWorkStep extends Step {
/**
* 机器人ID
*/
Expand Down Expand Up @@ -84,7 +75,7 @@ public class WXWorkPipelineBuilder extends Builder implements SimpleBuildStep {
private final RobotMessageSender robotSender = WXWorkRobotMessageSender.instance();

@DataBoundConstructor
public WXWorkPipelineBuilder(String robot) {
public WXWorkStep(String robot) {
this.robot = robot;
}

Expand Down Expand Up @@ -120,43 +111,100 @@ public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}

@Override
public void perform(
@NonNull Run<?, ?> run,
@NonNull FilePath workspace,
@NonNull EnvVars envVars,
@NonNull Launcher launcher,
@NonNull TaskListener listener) throws InterruptedException, IOException {
/**
* <p>发送机器人消息</p>
*
* @param run
* @param workspace
* @param envVars
* @param listener
*/
public void send(Run<?, ?> run, FilePath workspace, EnvVars envVars, TaskListener listener) {
RobotProperty property = WXWorkGlobalConfig.instance().getRobotPropertyById(robot);
if (property == null) {
if (Objects.isNull(property)) {
listener.error("机器人[%s]配置找不到!", robot);
return;
}
RunUser runUser = JenkinsUtils.getRunUser(run, listener);
RobotPipelineVars pipelineVars = RobotPipelineVars.builder()
.robot(envVars.expand(this.robot)).type(this.type).atMe(this.atMe).atAll(this.atAll)
.at(this.at).text(this.text).imageUrl(envVars.expand(this.imageUrl)).runUser(runUser)
.envVars(envVars).workspace(workspace).listener(listener).build();
.run(run).envVars(envVars).workspace(workspace).listener(listener)
.runUser(runUser)
.robot(JenkinsUtils.expandAll(run, workspace, listener, this.robot))
.type(this.type).atMe(this.atMe).atAll(this.atAll).at(this.at)
.text(this.text)
.imageUrl(JenkinsUtils.expandAll(run, workspace, listener, this.imageUrl))
.build();
RobotRequest robotRequest = RobotMessageFactory.makeRobotRequest(pipelineVars);
if (robotRequest == null) {
listener.error("不支持的消息!");
return;
}
RobotResponse robotResponse = robotSender.send(property, robotRequest);
if (robotResponse != null && robotResponse.isOk()) {
listener.getLogger().println("WXWORK: 微信机器人[" + property.name() + "]推送消息成功!");
} else {
listener.error(robotResponse.errorMessage());
if (Objects.nonNull(robotResponse)) {
if (robotResponse.isOk()) {
listener.getLogger().println("WXWORK: 微信机器人[" + property.name() + "]推送消息成功!");
} else {
listener.error("WXWORK: 微信机器人[" + property.name() + "]推送消息失败:" + robotResponse.errorMessage());
}
}
}

@Override
public StepExecution start(StepContext context) throws Exception {
return new WXWorkStepExecution(this, context);
}

@Override
public StepDescriptor getDescriptor() {
return new WXWorkStepDescriptor();
}

/**
* 执行
*/
static class WXWorkStepExecution extends StepExecution {
private static final long serialVersionUID = 1L;
private final transient WXWorkStep step;

public WXWorkStepExecution(WXWorkStep step, @NonNull StepContext context) {
super(context);
this.step = step;
}

@Override
public boolean start() throws Exception {
StepContext context = this.getContext();

Run<?, ?> run = context.get(Run.class);
FilePath workspace = context.get(FilePath.class);
EnvVars envVars = context.get(EnvVars.class);
TaskListener listener = context.get(TaskListener.class);

try {
this.step.send(run, workspace, envVars, listener);
context.onSuccess(true);
} catch (Exception e) {
context.onFailure(e);
}

return true;
}
}

@Symbol({"wxwork", "wxWork"})
@Extension
public final static class WXWorkDescriptorImpl extends BuildStepDescriptor<Builder> {
public static class WXWorkStepDescriptor extends StepDescriptor {

@Override
public Set<? extends Class<?>> getRequiredContext() {
return new HashSet<Class<?>>() {{
add(Run.class);
add(TaskListener.class);
}};
}

@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return false;
public String getFunctionName() {
return "wxwork";
}

@NonNull
Expand Down
Loading

0 comments on commit 8dcb836

Please sign in to comment.