Skip to content

Commit 3af1a6a

Browse files
authored
Merge pull request #31 from jenkinsci/jenkinsfile
chore: add required Jenkinsfile
2 parents fea34ab + b0ecc04 commit 3af1a6a

File tree

7 files changed

+153
-29
lines changed

7 files changed

+153
-29
lines changed

Jenkinsfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
buildPlugin(
2+
forkCount: '1C', // run this number of tests in parallel for faster feedback. If the number terminates with a 'C', the value will be multiplied by the number of available CPU cores
3+
useContainerAgent: true, // Set to `false` if you need to use Docker for containerized tests
4+
configurations: [
5+
[platform: 'linux', jdk: 21],
6+
[platform: 'windows', jdk: 17],
7+
])

src/main/java/com/lookout/jenkins/EnvironmentScript.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ private Environment generateEnvironment(AbstractBuild<?, ?> build,
120120
// First we create the script in a temporary directory.
121121
FilePath ws = build.getWorkspace(), scriptFile = null;
122122

123+
if (ws == null) {
124+
listener.error(Messages.EnvironmentScriptWrapper_WorkspaceIsNull());
125+
return null;
126+
}
127+
123128
ByteArrayOutputStream commandOutput = new ByteArrayOutputStream();
124129
int returnCode = -1;
125130
try {
@@ -132,7 +137,9 @@ private Environment generateEnvironment(AbstractBuild<?, ?> build,
132137
}
133138

134139
// Make sure prefix will always be more than 3 letters
135-
final String prefix = "env-" + build.getProject().getName();
140+
// Replace the equals sign with an underscore because Windows doesn't accept
141+
// this
142+
final String prefix = "env-" + build.getProject().getName().replace("=", "_");
136143
// Create a file in the system temporary directory with our script in it.
137144
scriptFile = ws.createTextTempFile(prefix, extension, script, false);
138145

@@ -295,7 +302,7 @@ public ListBoxModel doFillScriptTypeItems() {
295302
ListBoxModel items = new ListBoxModel(
296303
new ListBoxModel.Option(Commands.UNIX_SCRIPT_DISPLAY_NAME, Commands.UNIX_SCRIPT),
297304
new ListBoxModel.Option(Commands.BATCH_SCRIPT_DISPLAY_NAME, Commands.BATCH_SCRIPT),
298-
new ListBoxModel.Option(Commands.PROWER_SHELL_DISPLAY_NAME, Commands.POWER_SHELL));
305+
new ListBoxModel.Option(Commands.POWER_SHELL_DISPLAY_NAME, Commands.POWER_SHELL));
299306
return items;
300307
}
301308
}

src/main/java/com/lookout/jenkins/commands/Commands.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class Commands {
66
public final static String UNIX_SCRIPT_DISPLAY_NAME = "Unix script";
77

88
public final static String POWER_SHELL = "powerShell";
9-
public final static String PROWER_SHELL_DISPLAY_NAME = "Powershell script";
9+
public final static String POWER_SHELL_DISPLAY_NAME = "Powershell script";
1010

1111
public final static String BATCH_SCRIPT = "batchScript";
1212
public final static String BATCH_SCRIPT_DISPLAY_NAME = "Batch script";
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
EnvironmentScriptWrapper.UnableToProduceScript=Unable to produce a script file
22
EnvironmentScriptWrapper.UnableToParseScriptOutput=Unable to parse output from script
3-
EnvironmentScriptWrapper.UnableToExecuteScript=Unable to execute script, return code {0}
3+
EnvironmentScriptWrapper.UnableToExecuteScript=Unable to execute script, return code {0}
4+
EnvironmentScriptWrapper.WorkspaceIsNull=Workspace is null. Cannot generate environment

src/test/java/com/lookout/jenkins/EnvironmentScriptMatrixTest.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.jvnet.hudson.test.JenkinsRule;
1414

1515
import hudson.FilePath;
16+
import hudson.Functions;
1617
import hudson.matrix.Axis;
1718
import hudson.matrix.AxisList;
1819
import hudson.matrix.MatrixRun;
@@ -42,6 +43,11 @@ public MatrixTestJob(String script, boolean onlyRunOnParent) throws Exception {
4243
// race conditions when concurrently updating the 'counter' file.
4344
project.setExecutionStrategy(new DefaultMatrixExecutionStrategyImpl(true, null, null, null));
4445

46+
String scriptType = UNIX_SCRIPT;
47+
if (Functions.isWindows()) {
48+
scriptType = BATCH_SCRIPT;
49+
}
50+
4551
project.setAxes(new AxisList(new Axis("axis", "value1", "value2")));
4652
project.getBuildWrappersList()
4753
.add(new EnvironmentScript(script, scriptType, onlyRunOnParent, hideGeneratedValue));
@@ -52,28 +58,45 @@ public MatrixTestJob(String script, boolean onlyRunOnParent) throws Exception {
5258
}
5359
}
5460

55-
final static String SCRIPT_COUNTER = "file='%s/counter'\n"
61+
final static String SCRIPT_COUNTER_UNIX = "file='%s/counter'\n"
5662
+ "if [ -f $file ]; then\n"
5763
+ " i=$(($(cat $file)+1))\n"
5864
+ "else\n"
5965
+ " i=1\n"
6066
+ "fi\n"
61-
+ "echo 1 >was_run\n"
62-
+ "echo $i >$file\n"
67+
+ "echo 1 > was_run\n"
68+
+ "echo $i > $file\n"
69+
+ "echo seen=yes";
70+
71+
final static String SCRIPT_COUNTER_BATCH = "@echo off\r\n"
72+
+ "set file=%s\\counter\r\n"
73+
+ "if exist %%file%% (\r\n"
74+
+ " set /p i=< %%file%%\r\n"
75+
+ " set /a i+=1\r\n"
76+
+ ") else (\r\n"
77+
+ " set i=1\r\n"
78+
+ ")\r\n"
79+
+ "echo 1 > was_run\r\n"
80+
+ "echo %%i%% > %%file%%\r\n"
6381
+ "echo seen=yes";
6482

6583
// Generate a random directory that we pass to the shell script.
6684
File tempDir;
6785

6886
String script;
69-
String scriptType = "unixScript";
87+
final static String UNIX_SCRIPT = "unixScript";
88+
final static String BATCH_SCRIPT = "batchScript";
7089
boolean hideGeneratedValue = Boolean.TRUE;
7190

7291
@Before
7392
public void setUp() throws IOException {
7493
// Generate a random directory that we pass to the shell script.
7594
tempDir = Files.createTempDirectory("tmp").toFile();
76-
script = String.format(SCRIPT_COUNTER, tempDir.getPath());
95+
String scriptCounter = SCRIPT_COUNTER_UNIX;
96+
if (Functions.isWindows()) {
97+
scriptCounter = SCRIPT_COUNTER_BATCH;
98+
}
99+
script = String.format(scriptCounter, tempDir.getPath());
77100
}
78101

79102
@Test

src/test/java/com/lookout/jenkins/EnvironmentScriptMultiMatrixTest.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.nio.file.Files;
1515

1616
import hudson.FilePath;
17+
import hudson.Functions;
1718
import hudson.matrix.Axis;
1819
import hudson.matrix.AxisList;
1920
import hudson.matrix.MatrixRun;
@@ -43,6 +44,11 @@ public MultiMatrixTestJob(String script, boolean onlyRunOnParent) throws Excepti
4344
// race conditions when concurrently updating the 'counter' file.
4445
project.setExecutionStrategy(new DefaultMatrixExecutionStrategyImpl(true, null, null, null));
4546

47+
String scriptType = UNIX_SCRIPT;
48+
if (Functions.isWindows()) {
49+
scriptType = BATCH_SCRIPT;
50+
}
51+
4652
project.setAxes(new AxisList(new Axis("axis", "value1", "value2")));
4753
project.getBuildWrappersList()
4854
.add(new EnvironmentScript(script, scriptType, onlyRunOnParent, hideGeneratedValue));
@@ -53,28 +59,45 @@ public MultiMatrixTestJob(String script, boolean onlyRunOnParent) throws Excepti
5359
}
5460
}
5561

56-
final static String SCRIPT_COUNTER = "file='%s/counter'\n"
62+
final static String SCRIPT_COUNTER_UNIX = "file='%s/counter'\n"
5763
+ "if [ -f $file ]; then\n"
5864
+ " i=$(($(cat $file)+1))\n"
5965
+ "else\n"
6066
+ " i=1\n"
6167
+ "fi\n"
62-
+ "echo 1 >was_run\n"
63-
+ "echo $i >$file\n"
68+
+ "echo 1 > was_run\n"
69+
+ "echo $i > $file\n"
70+
+ "echo seen=yes";
71+
72+
final static String SCRIPT_COUNTER_BATCH = "@echo off\r\n"
73+
+ "set file=%s\\counter\r\n"
74+
+ "if exist %%file%% (\r\n"
75+
+ " set /p i=< %%file%%\r\n"
76+
+ " set /a i+=1\r\n"
77+
+ ") else (\r\n"
78+
+ " set i=1\r\n"
79+
+ ")\r\n"
80+
+ "echo 1 > was_run\r\n"
81+
+ "echo %%i%% > %%file%%\r\n"
6482
+ "echo seen=yes";
6583

6684
// Generate a random directory that we pass to the shell script.
6785
File tempDir;
6886

6987
String script;
70-
String scriptType = "unixScript";
88+
final static String UNIX_SCRIPT = "unixScript";
89+
final static String BATCH_SCRIPT = "batchScript";
7190
boolean hideGeneratedValue = Boolean.TRUE;
7291

7392
@Before
7493
public void setUp() throws IOException {
7594
// Generate a random directory that we pass to the shell script.
7695
tempDir = Files.createTempDirectory("tmp").toFile();
77-
script = String.format(SCRIPT_COUNTER, tempDir);
96+
String scriptCounter = SCRIPT_COUNTER_UNIX;
97+
if (Functions.isWindows()) {
98+
scriptCounter = SCRIPT_COUNTER_BATCH;
99+
}
100+
script = String.format(scriptCounter, tempDir);
78101
}
79102

80103
@Test

src/test/java/com/lookout/jenkins/EnvironmentScriptTest.java

Lines changed: 78 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import static org.junit.Assert.*;
44

5+
import java.io.File;
56
import java.nio.charset.Charset;
67
import java.util.List;
78

89
import hudson.EnvVars;
10+
import hudson.Functions;
911
import hudson.model.FreeStyleBuild;
1012
import hudson.model.TaskListener;
1113
import hudson.model.FreeStyleProject;
@@ -39,33 +41,54 @@ public TestJob(String script, String scriptType, boolean hideEnvironmentVariable
3941
}
4042

4143
final static String UNIX_SCRIPT = "unixScript";
44+
final static String BATCH_SCRIPT = "batchScript";
4245

4346
final static String SCRIPT_SIMPLE_VARIABLES = "echo var1=one\n"
4447
+ "echo var2=two\n"
4548
+ "echo var3=three";
4649

47-
final static String SCRIPT_DEPENDENT_VARIABLES = "echo var1=one\n"
50+
final static String SCRIPT_DEPENDENT_VARIABLES_UNIX = "echo var1=one\n"
4851
+ "echo var2='$var1 two'\n"
4952
+ "echo var3='yo $var4'\n"
5053
+ "echo var4='three ${var2}'";
5154

52-
final static String SCRIPT_OVERRIDDEN_VARIABLES = "echo var1=one\n"
55+
final static String SCRIPT_DEPENDENT_VARIABLES_BATCH = "echo var1=one\n"
56+
+ "echo var2=$var1 two\n"
57+
+ "echo var3=yo $var4\n"
58+
+ "echo var4=three ${var2}";
59+
60+
final static String SCRIPT_OVERRIDDEN_VARIABLES_UNIX = "echo var1=one\n"
5361
+ "echo var1+something='not one'\n"
5462
+ "echo var2+something='two'";
5563

64+
final static String SCRIPT_OVERRIDDEN_VARIABLES_BATCH = "echo var1=one\n"
65+
+ "echo var1+something=not one\n"
66+
+ "echo var2+something=two";
67+
5668
final static String SCRIPT_UTF8 = "echo UTFstr=mąż";
5769

58-
final static String SCRIPT_SHEBANG = "#!/bin/cat\n"
70+
final static String SCRIPT_SHEBANG_UNIX = "#!/bin/cat\n"
5971
+ "hello=world";
6072

73+
// batch script does not have shebang
74+
final static String SCRIPT_SHEBANG_BATCH = "echo hello=world";
75+
6176
public void testWithEmptyScript() throws Exception {
62-
TestJob job = new TestJob("", UNIX_SCRIPT, true);
77+
String scriptType = UNIX_SCRIPT;
78+
if (Functions.isWindows()) {
79+
scriptType = BATCH_SCRIPT;
80+
}
81+
TestJob job = new TestJob("", scriptType, true);
6382
assertEquals(Result.SUCCESS, job.build.getResult());
6483
}
6584

6685
@Test
6786
public void testWithSimpleVariables() throws Exception {
68-
TestJob job = new TestJob(SCRIPT_SIMPLE_VARIABLES, UNIX_SCRIPT, true);
87+
String scriptType = UNIX_SCRIPT;
88+
if (Functions.isWindows()) {
89+
scriptType = BATCH_SCRIPT;
90+
}
91+
TestJob job = new TestJob(SCRIPT_SIMPLE_VARIABLES, scriptType, true);
6992
assertEquals(Result.SUCCESS, job.build.getResult());
7093

7194
EnvVars vars = job.build.getEnvironment(job.listener);
@@ -76,7 +99,13 @@ public void testWithSimpleVariables() throws Exception {
7699

77100
@Test
78101
public void testWithDependentVariables() throws Exception {
79-
TestJob job = new TestJob(SCRIPT_DEPENDENT_VARIABLES, UNIX_SCRIPT, true);
102+
String scriptType = UNIX_SCRIPT;
103+
String script = SCRIPT_DEPENDENT_VARIABLES_UNIX;
104+
if (Functions.isWindows()) {
105+
scriptType = BATCH_SCRIPT;
106+
script = SCRIPT_DEPENDENT_VARIABLES_BATCH;
107+
}
108+
TestJob job = new TestJob(script, scriptType, true);
80109
assertEquals(Result.SUCCESS, job.build.getResult());
81110

82111
EnvVars vars = job.build.getEnvironment(job.listener);
@@ -87,41 +116,67 @@ public void testWithDependentVariables() throws Exception {
87116
}
88117

89118
@Test
90-
public void testWithOverridenVariables() throws Exception {
91-
TestJob job = new TestJob(SCRIPT_OVERRIDDEN_VARIABLES, UNIX_SCRIPT, true);
119+
public void testWithOverriddenVariables() throws Exception {
120+
String scriptType = UNIX_SCRIPT;
121+
String script = SCRIPT_OVERRIDDEN_VARIABLES_UNIX;
122+
if (Functions.isWindows()) {
123+
scriptType = BATCH_SCRIPT;
124+
script = SCRIPT_OVERRIDDEN_VARIABLES_BATCH;
125+
}
126+
TestJob job = new TestJob(script, scriptType, true);
92127
assertEquals(Result.SUCCESS, job.build.getResult());
93128

94129
EnvVars vars = job.build.getEnvironment(job.listener);
95-
assertEquals("not one:one", vars.get("var1"));
130+
assertEquals("not one" + File.pathSeparatorChar + "one", vars.get("var1"));
96131
assertEquals("two", vars.get("var2"));
97132
}
98133

99134
@Test
100135
public void testReadingFileFromSCM() throws Exception {
101-
TestJob job = new TestJob("cat envs", UNIX_SCRIPT, true);
136+
String scriptType = UNIX_SCRIPT;
137+
String script = "cat envs";
138+
if (Functions.isWindows()) {
139+
scriptType = BATCH_SCRIPT;
140+
script = "type envs";
141+
}
142+
TestJob job = new TestJob(script, scriptType, true);
102143
assertEquals(Result.SUCCESS, job.build.getResult());
103144
assertEquals("bar", job.build.getEnvironment(job.listener).get("foo_var"));
104145
}
105146

106147
@Test
107148
public void testWorkingDirectory() throws Exception {
108-
TestJob job = new TestJob("echo hi >was_run", UNIX_SCRIPT, true);
149+
String scriptType = UNIX_SCRIPT;
150+
if (Functions.isWindows()) {
151+
scriptType = BATCH_SCRIPT;
152+
}
153+
TestJob job = new TestJob("echo hi >was_run", scriptType, true);
109154

110155
// Make sure that the $PWD of the script is $WORKSPACE.
111156
assertTrue(job.build.getWorkspace().child("was_run").exists());
112157
}
113158

114159
@Test
115160
public void testWithShebang() throws Exception {
116-
TestJob job = new TestJob(SCRIPT_SHEBANG, UNIX_SCRIPT, true);
161+
String scriptType = UNIX_SCRIPT;
162+
String script = SCRIPT_SHEBANG_UNIX;
163+
if (Functions.isWindows()) {
164+
scriptType = BATCH_SCRIPT;
165+
script = SCRIPT_SHEBANG_BATCH;
166+
}
167+
TestJob job = new TestJob(script, scriptType, true);
117168

118169
assertEquals(Result.SUCCESS, job.build.getResult());
119170
EnvVars vars = job.build.getEnvironment(job.listener);
120171
assertEquals("world", vars.get("hello"));
121172
}
122173

123174
public void testUTFHandling() throws Exception {
124-
TestJob job = new TestJob(SCRIPT_UTF8, UNIX_SCRIPT, true);
175+
String scriptType = UNIX_SCRIPT;
176+
if (Functions.isWindows()) {
177+
scriptType = BATCH_SCRIPT;
178+
}
179+
TestJob job = new TestJob(SCRIPT_UTF8, scriptType, true);
125180
assertEquals(Result.SUCCESS, job.build.getResult());
126181

127182
EnvVars vars = job.build.getEnvironment(job.listener);
@@ -130,7 +185,11 @@ public void testUTFHandling() throws Exception {
130185

131186
@Test
132187
public void testHideEnvironmentVariablesValues() throws Exception {
133-
TestJob job = new TestJob(SCRIPT_SIMPLE_VARIABLES, UNIX_SCRIPT, true);
188+
String scriptType = UNIX_SCRIPT;
189+
if (Functions.isWindows()) {
190+
scriptType = BATCH_SCRIPT;
191+
}
192+
TestJob job = new TestJob(SCRIPT_SIMPLE_VARIABLES, scriptType, true);
134193
assertEquals(Result.SUCCESS, job.build.getResult());
135194
List<String> logs = job.build.getLog(10);
136195

@@ -141,7 +200,11 @@ public void testHideEnvironmentVariablesValues() throws Exception {
141200

142201
@Test
143202
public void testShowEnvironmentVariablesValues() throws Exception {
144-
TestJob job = new TestJob(SCRIPT_SIMPLE_VARIABLES, UNIX_SCRIPT, false);
203+
String scriptType = UNIX_SCRIPT;
204+
if (Functions.isWindows()) {
205+
scriptType = BATCH_SCRIPT;
206+
}
207+
TestJob job = new TestJob(SCRIPT_SIMPLE_VARIABLES, scriptType, false);
145208
assertEquals(Result.SUCCESS, job.build.getResult());
146209
List<String> logs = job.build.getLog(10);
147210

0 commit comments

Comments
 (0)