Skip to content

Commit e44ee35

Browse files
authored
Merge pull request #286 from FlowCI/feature/285
api run docker not start docker directly build
2 parents 5a53dd4 + 9a08dda commit e44ee35

File tree

7 files changed

+116
-4
lines changed

7 files changed

+116
-4
lines changed

docker/app-api.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ jfiglet.message = FlowCi Started Success
3838
## plugin repos url
3939
plugins.repository = https://raw.githubusercontent.com/flowci/plugins/master/repository.json
4040

41+
api.run.indocker = true
42+
4143
task.job.toggle.execution_timeout = true
4244
## 6s expire job
4345
task.job.toggle.execution_create_session_duration = 1800

platform-api/src/main/resources/app-default.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ api.user.expire = 604800
2525
## plugin setting
2626
plugins.repository = https://raw.githubusercontent.com/flowci/plugins/master/repository.json
2727

28+
api.run.indocker = false
29+
2830
### domain ###
2931
domain.api = http://localhost:8088
3032
domain.web = http://localhost:3000

platform-api/src/test/resources/app-test.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ api.user.expire = 60000
2424

2525
## plugin setting
2626
plugins.repository = https://raw.githubusercontent.com/yunheli/plugins/master/repository.json
27+
api.run.indocker = false
2728

2829
### domain ###
2930
domain.api = http://localhost:8080

platform-plugin/src/main/java/com/flow/platform/plugin/service/PluginServiceImpl.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@
3030
import com.flow.platform.plugin.event.PluginRefreshEvent.Status;
3131
import com.flow.platform.plugin.event.PluginStatusChangeEvent;
3232
import com.flow.platform.plugin.exception.PluginException;
33+
import com.flow.platform.plugin.util.CmdUtil;
3334
import com.flow.platform.plugin.util.YmlUtil;
3435
import com.flow.platform.plugin.util.docker.Docker;
36+
import com.flow.platform.util.CommandUtil.Unix;
3537
import com.flow.platform.util.ExceptionUtil;
3638
import com.flow.platform.util.Logger;
3739
import com.flow.platform.util.git.GitException;
@@ -55,6 +57,7 @@
5557
import org.eclipse.jgit.api.Git;
5658
import org.eclipse.jgit.revwalk.RevCommit;
5759
import org.springframework.beans.factory.annotation.Autowired;
60+
import org.springframework.beans.factory.annotation.Value;
5861
import org.springframework.scheduling.annotation.Scheduled;
5962
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
6063
import org.springframework.stereotype.Service;
@@ -83,6 +86,9 @@ public class PluginServiceImpl extends ApplicationEventService implements Plugin
8386

8487
private final static String TMP = "tmp";
8588

89+
@Value("${api.run.indocker}")
90+
private boolean runInDocker;
91+
8692
// git clone folder
8793
@Autowired
8894
private Path gitWorkspace;
@@ -447,7 +453,8 @@ public void exec(Plugin plugin) {
447453
try {
448454

449455
// only build and image all in value to pull image
450-
if (!Strings.isBlank(plugin.getPluginDetail().getBuild()) && !Strings.isBlank(plugin.getPluginDetail().getImage())) {
456+
if (!Strings.isBlank(plugin.getPluginDetail().getBuild()) && !Strings
457+
.isBlank(plugin.getPluginDetail().getImage())) {
451458

452459
LOGGER.traceMarker("BuildProcessor", "Start build code");
453460

@@ -457,7 +464,12 @@ public void exec(Plugin plugin) {
457464
JGitUtil.checkout(cachePath, latestGitTag);
458465

459466
// first pull image and build
460-
dockerPullAndBuild(plugin);
467+
if (!runInDocker) {
468+
dockerPullAndBuild(plugin);
469+
} else {
470+
// if run in docker only build
471+
build(plugin);
472+
}
461473

462474
// second detect outputs
463475
detectBuildArtifacts(plugin);
@@ -475,6 +487,12 @@ public void exec(Plugin plugin) {
475487
}
476488
}
477489

490+
private void build(Plugin plugin) {
491+
Path cachePath = gitCachePath(plugin);
492+
String cmd = "cd " + cachePath.toString() + Unix.LINE_SEPARATOR + plugin.getPluginDetail().getBuild();
493+
CmdUtil.exeCmd(cmd);
494+
}
495+
478496
private void dockerPullAndBuild(Plugin plugin) {
479497
Path cachePath = gitCachePath(plugin);
480498
Docker docker = new Docker();
@@ -565,7 +583,8 @@ private class PushProcessor implements Processor {
565583
public void exec(Plugin plugin) {
566584
LOGGER.traceMarker("PushProcessor", "Push tags to local");
567585

568-
if (!Strings.isBlank(plugin.getPluginDetail().getImage()) && !Strings.isBlank(plugin.getPluginDetail().getBuild())) {
586+
if (!Strings.isBlank(plugin.getPluginDetail().getImage()) && !Strings
587+
.isBlank(plugin.getPluginDetail().getBuild())) {
569588
return;
570589
}
571590

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2017 flow.ci
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.flow.platform.plugin.util;
18+
19+
import com.flow.platform.plugin.exception.PluginException;
20+
import com.flow.platform.util.CommandUtil.Unix;
21+
import com.flow.platform.util.Logger;
22+
import java.io.BufferedReader;
23+
import java.io.InputStreamReader;
24+
25+
/**
26+
* @author yh@firim
27+
*/
28+
public class CmdUtil {
29+
30+
private final static Logger LOGGER = new Logger(CmdUtil.class);
31+
32+
public static void exeCmd(String shell) {
33+
BufferedReader br;
34+
try {
35+
Process process;
36+
ProcessBuilder pb = new ProcessBuilder(Unix.CMD_EXECUTOR, "-c", shell);
37+
pb.environment();
38+
39+
// Merge error stream to standard stream
40+
pb.redirectErrorStream(true);
41+
42+
// start
43+
process = pb.start();
44+
if (process != null) {
45+
br = new BufferedReader(
46+
new InputStreamReader(process.getInputStream()), 1024);
47+
48+
// wait process finish
49+
process.waitFor();
50+
} else {
51+
throw new PluginException("Plugin running process is not start");
52+
}
53+
54+
String line;
55+
while (br != null && (line = br.readLine()) != null) {
56+
// show running log
57+
LOGGER.debug(line);
58+
}
59+
} catch (Exception e) {
60+
LOGGER.error("Exec cmd error", e);
61+
} finally {
62+
}
63+
}
64+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.flow.platform.plugin.test.util;
2+
3+
import com.flow.platform.plugin.test.TestBase;
4+
import com.flow.platform.plugin.util.CmdUtil;
5+
import org.junit.Test;
6+
import org.springframework.beans.factory.annotation.Value;
7+
8+
/**
9+
* @author yh@firim
10+
*/
11+
public class ShellUtilTest extends TestBase{
12+
13+
@Value("${api.run.indocker}")
14+
private boolean runInDocker;
15+
16+
17+
@Test
18+
public void should_exec_cmd_success() {
19+
CmdUtil.exeCmd("echo helloworld");
20+
CmdUtil.exeCmd("pwd");
21+
CmdUtil.exeCmd("mvn");
22+
}
23+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
api.git.cache = /tmp/flow-ci-ut/git-cache
22
api.git.workspace = /tmp/flow-ci-ut/git-clone
3-
plugins.repository = http://localhost:8080/repos/plugin.json
3+
plugins.repository = http://localhost:8080/repos/plugin.json
4+
api.run.indocker = false

0 commit comments

Comments
 (0)