Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

cmd - Build in jenkins depending on git branch

Good day everyone. I am trying to solve a problem with a build depending on the git branch.

Expected Result: After "if = master" the master folder should be created. If it is "if! = Master" then various. I suspect that the condition does not work at all, because the folder is always created.

I tried like this:

SET branch=%GIT_BRANCH:*/=%
if ($ branch -eq "test") {
    Write-Output "Branch test"
}
if ($ branch = "master") {
   MD "C:  Jenkins  workspace  Api  Api_DEV  API_Build_Master_Test  Master"
}
else {
   MD "C:  Jenkins  workspace  Api  Api_DEV  API_Build_Master_Test  Various"
}

But this method does not work at all, it gives the following error C: Jenkins workspace Api Api_DEV API_Build_Master_Test> SET branch = test-eq was unexpected at this time. I make steps for windows in jenkins, that is, I execute under cmd

question from:https://stackoverflow.com/questions/65845066/build-in-jenkins-depending-on-git-branch

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The code in the question seems to be a mix of cmd and PowerShell, both with invalid syntax for either language. The first line appears to remove everything before the first SOLIDUS (forward slash) character in the GIT_BRANCH variable.

What happens with this?

SET "branch=%GIT_BRANCH:*/ =%"
if "%branch%" == "test" (ECHO Branch test)

if ("%branch%" == "master") (
    MKDIR "C:JenkinsworkspaceApiApi_DEVAPI_Build_Master_TestMaster"
) else (
    MKDIR "C:JenkinsworkspaceApiApi_DEVAPI_Build_Master_TestVarious"
)

UPDATE:

SET "branch=%GIT_BRANCH:*/ =%"
if "%branch%" == "test" (ECHO Branch test)

ECHO branch is set to ===%branch%===

SET "BUILD_ROOT=C:JenkinsworkspaceApiApi_DEVAPI_Build_Master_Test"

if ("%branch%" == "master") (
    IF NOT EXIST "%BUILD_ROOT%Master" (MKDIR "%BUILD_ROOT%Master")
) else (
    IF NOT EXIST "%BUILD_ROOT%Various" (MKDIR "%BUILD_ROOT%Various")
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...