Jenkins build step doesn't execute as it should.

I try to download my project from GitHub repository using Jenkins and then to build. Later I will join tomcat server and automatically host on localhost or aws. Currently something is wrong. I have this script:
@echo off
SETLOCAL

echo Removing old resources...
cd Maitinimas-back\src\main\resources\public
del /Q *.*

echo Building react app...
cd ..\..\..\..\..\Maitinimas-front\

echo Checking Node.js version...
node -v
IF ERRORLEVEL 1 (
echo Node.js is not installed or not found in PATH. Exiting...
exit /B 1
)

echo Checking npm version...
npm -v
IF ERRORLEVEL 1 (
echo npm is not installed or not found in PATH. Exiting...
exit /B 1
)

echo Installing dependencies...
npm install
IF ERRORLEVEL 1 (
echo Npm install failed. Exiting...
exit /B 1
)

npm run build
IF ERRORLEVEL 1 (
echo Npm build failed. Exiting...
exit /B 1
) ELSE (
echo Npm build completed successfully.
)

echo Copying build to target...
cd ..\Maitinimas-back\src\main\resources\public\
xcopy /E /Y ..\..\..\..\..\Maitinimas-front\build\* .\

echo Maven clean package...
cd ..\..\..\..
mvn clean package
IF ERRORLEVEL 1 (
echo Maven clean package failed. Exiting...
exit /B 1
)

echo Build script completed successfully.
ENDLOCAL
@echo off
SETLOCAL

echo Removing old resources...
cd Maitinimas-back\src\main\resources\public
del /Q *.*

echo Building react app...
cd ..\..\..\..\..\Maitinimas-front\

echo Checking Node.js version...
node -v
IF ERRORLEVEL 1 (
echo Node.js is not installed or not found in PATH. Exiting...
exit /B 1
)

echo Checking npm version...
npm -v
IF ERRORLEVEL 1 (
echo npm is not installed or not found in PATH. Exiting...
exit /B 1
)

echo Installing dependencies...
npm install
IF ERRORLEVEL 1 (
echo Npm install failed. Exiting...
exit /B 1
)

npm run build
IF ERRORLEVEL 1 (
echo Npm build failed. Exiting...
exit /B 1
) ELSE (
echo Npm build completed successfully.
)

echo Copying build to target...
cd ..\Maitinimas-back\src\main\resources\public\
xcopy /E /Y ..\..\..\..\..\Maitinimas-front\build\* .\

echo Maven clean package...
cd ..\..\..\..
mvn clean package
IF ERRORLEVEL 1 (
echo Maven clean package failed. Exiting...
exit /B 1
)

echo Build script completed successfully.
ENDLOCAL
I call it using call build-script.bat in Jenkins step Execute Windows batch command. But Instead of executing all the script Jenkins finishes it prematurely:
47 Replies
JavaBot
JavaBot2mo ago
This post has been reserved for your question.
Hey @Tomasm21! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Tomasm21
Tomasm21OP2mo ago
Started by user Tomasm21

Running as SYSTEM
Building in workspace C:\ProgramData\Jenkins\.jenkins\workspace\Maitinimas-App
[WS-CLEANUP] Deleting project workspace...
[WS-CLEANUP] Deferred wipeout is used...
[WS-CLEANUP] Done
The recommended git tool is: NONE
using credential jenkins.admin
Cloning the remote Git repository
Cloning repository https://github.com/m21tomas/Catering-establishments-information-system.git
> git.exe init C:\ProgramData\Jenkins\.jenkins\workspace\Maitinimas-App # timeout=10
Fetching upstream changes from https://github.com/m21tomas/Catering-establishments-information-system.git
> git.exe --version # timeout=10
> git --version # 'git version 2.40.0.windows.1'
using GIT_ASKPASS to set credentials jenkins.admin
> git.exe fetch --tags --force --progress -- https://github.com/m21tomas/Catering-establishments-information-system.git +refs/heads/*:refs/remotes/origin/* # timeout=10
> git.exe config remote.origin.url https://github.com/m21tomas/Catering-establishments-information-system.git # timeout=10
> git.exe config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
Avoid second fetch
> git.exe rev-parse "refs/remotes/origin/main^{commit}" # timeout=10
Checking out Revision de6caea6b96fb6618359a139f3ee372e405e701d (refs/remotes/origin/main)
> git.exe config core.sparsecheckout # timeout=10
> git.exe checkout -f de6caea6b96fb6618359a139f3ee372e405e701d # timeout=10
Commit message: "Add build-script batch script"
First time build. Skipping changelog.
[Maitinimas-App] $ cmd /c call C:\WINDOWS\TEMP\jenkins5630739311533059196.bat

C:\ProgramData\Jenkins\.jenkins\workspace\Maitinimas-App>call build-script.bat
Removing old resources...
Building react app...
Checking Node.js version...
v18.14.0
Checking npm version...
9.3.1
Finished: SUCCESS
Started by user Tomasm21

Running as SYSTEM
Building in workspace C:\ProgramData\Jenkins\.jenkins\workspace\Maitinimas-App
[WS-CLEANUP] Deleting project workspace...
[WS-CLEANUP] Deferred wipeout is used...
[WS-CLEANUP] Done
The recommended git tool is: NONE
using credential jenkins.admin
Cloning the remote Git repository
Cloning repository https://github.com/m21tomas/Catering-establishments-information-system.git
> git.exe init C:\ProgramData\Jenkins\.jenkins\workspace\Maitinimas-App # timeout=10
Fetching upstream changes from https://github.com/m21tomas/Catering-establishments-information-system.git
> git.exe --version # timeout=10
> git --version # 'git version 2.40.0.windows.1'
using GIT_ASKPASS to set credentials jenkins.admin
> git.exe fetch --tags --force --progress -- https://github.com/m21tomas/Catering-establishments-information-system.git +refs/heads/*:refs/remotes/origin/* # timeout=10
> git.exe config remote.origin.url https://github.com/m21tomas/Catering-establishments-information-system.git # timeout=10
> git.exe config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
Avoid second fetch
> git.exe rev-parse "refs/remotes/origin/main^{commit}" # timeout=10
Checking out Revision de6caea6b96fb6618359a139f3ee372e405e701d (refs/remotes/origin/main)
> git.exe config core.sparsecheckout # timeout=10
> git.exe checkout -f de6caea6b96fb6618359a139f3ee372e405e701d # timeout=10
Commit message: "Add build-script batch script"
First time build. Skipping changelog.
[Maitinimas-App] $ cmd /c call C:\WINDOWS\TEMP\jenkins5630739311533059196.bat

C:\ProgramData\Jenkins\.jenkins\workspace\Maitinimas-App>call build-script.bat
Removing old resources...
Building react app...
Checking Node.js version...
v18.14.0
Checking npm version...
9.3.1
Finished: SUCCESS
Why is that?
dan1st
dan1st2mo ago
Are you doing CI things on Windows? Why? Can you try printing the errorlevel for testing? maybe the npm -v exits for some reason?
Tomasm21
Tomasm21OP2mo ago
Yes I am. Why not?
dan1st
dan1st2mo ago
Maybe you could change it to cmd /c npm -v?
Tomasm21
Tomasm21OP2mo ago
I don't know how yet.
dan1st
dan1st2mo ago
because scripting on Windows is annoying and Windows eats up resources
Tomasm21
Tomasm21OP2mo ago
Maybe. But when I do it manually.. I mean if I clone my repository using CMD and then navigating to the directory and doing npm -v then it shows version: 9.6.4 which is different than Jenkins 9.3.1. And then tried to manually install node modules: npm install And it installed node modules. But this part in batch script file is omitted:
echo Checking npm version...
npm -v
IF ERRORLEVEL 1 (
echo npm is not installed or not found in PATH. Exiting...
exit /B 1
)

echo Installing dependencies...
npm install
IF ERRORLEVEL 1 (
echo Npm install failed. Exiting...
exit /B 1
)
echo Checking npm version...
npm -v
IF ERRORLEVEL 1 (
echo npm is not installed or not found in PATH. Exiting...
exit /B 1
)

echo Installing dependencies...
npm install
IF ERRORLEVEL 1 (
echo Npm install failed. Exiting...
exit /B 1
)
dan1st
dan1st2mo ago
so it omits these parts and continues later? I don't really know batch tbh but to me, it looks like it's just the script aborting
Tomasm21
Tomasm21OP2mo ago
It doesn;t continue if I do the whole batch file. It executes when I launch commands one by one.
dan1st
dan1st2mo ago
and maybe npm is just a batch file that does exit 0 which propagates to your script for some reason which is why I suggested replacing npm -v with cmd /c npm -v
Tomasm21
Tomasm21OP2mo ago
Jenkins shell script doesn't work even more. So I asked chatGPT to convert bash script to Windows batch.
dan1st
dan1st2mo ago
idk why Windows does that (if you exit from a batch script in a batch script, it might exit out of both scripts) but I vaguely remember something like this so try that and tell me what happens
Tomasm21
Tomasm21OP2mo ago
ok I will try it. The overall script will be this:
@echo off
SETLOCAL

echo Removing old resources...
cd Maitinimas-back\src\main\resources\public
del /Q *.*

echo Building react app...
cd ..\..\..\..\..\Maitinimas-front\

echo Checking Node.js version...
node -v
IF ERRORLEVEL 1 (
echo Node.js is not installed or not found in PATH. Exiting...
exit /B 1
)

echo Checking Node.js version...
cmd /c npm -v
echo Npm version checked: %ERRORLEVEL%

echo Installing dependencies...
npm install
echo Dependencies installed: %ERRORLEVEL%

npm run build
IF ERRORLEVEL 1 (
echo Npm build failed. Exiting...
exit /B 1
) ELSE (
echo Npm build completed successfully.
)

echo Copying build to target...
cd ..\Maitinimas-back\src\main\resources\public\
xcopy /E /Y ..\..\..\..\..\Maitinimas-front\build\* .\

echo Maven clean package...
cd ..\..\..\..
mvn clean package
IF ERRORLEVEL 1 (
echo Maven clean package failed. Exiting...
exit /B 1
)

echo Build script completed successfully.
ENDLOCAL
@echo off
SETLOCAL

echo Removing old resources...
cd Maitinimas-back\src\main\resources\public
del /Q *.*

echo Building react app...
cd ..\..\..\..\..\Maitinimas-front\

echo Checking Node.js version...
node -v
IF ERRORLEVEL 1 (
echo Node.js is not installed or not found in PATH. Exiting...
exit /B 1
)

echo Checking Node.js version...
cmd /c npm -v
echo Npm version checked: %ERRORLEVEL%

echo Installing dependencies...
npm install
echo Dependencies installed: %ERRORLEVEL%

npm run build
IF ERRORLEVEL 1 (
echo Npm build failed. Exiting...
exit /B 1
) ELSE (
echo Npm build completed successfully.
)

echo Copying build to target...
cd ..\Maitinimas-back\src\main\resources\public\
xcopy /E /Y ..\..\..\..\..\Maitinimas-front\build\* .\

echo Maven clean package...
cd ..\..\..\..
mvn clean package
IF ERRORLEVEL 1 (
echo Maven clean package failed. Exiting...
exit /B 1
)

echo Build script completed successfully.
ENDLOCAL
Right? Or maybe to highlight important place with more comments:
echo Building react app...
echo Checking Node.js version...
cmd /c npm -v
echo Npm version checked: %ERRORLEVEL%

echo Installing dependencies...
npm install
echo Dependencies installed: %ERRORLEVEL%
echo Building react app...
echo Checking Node.js version...
cmd /c npm -v
echo Npm version checked: %ERRORLEVEL%

echo Installing dependencies...
npm install
echo Dependencies installed: %ERRORLEVEL%
dan1st
dan1st2mo ago
I guess
Tomasm21
Tomasm21OP2mo ago
Now it is installing the dependencies. I guess cmd /c npm -v was the solution but right after installing all dependencies the build stopped. and exited. Perhaps I have to add cmd /c ... before all npms. Right.
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Tomasm21
Tomasm21OP2mo ago
The overall batch file that works well is now:
@echo off
SETLOCAL

echo Removing old resources...
cd Maitinimas-back\src\main\resources\public
del /Q *.*

echo Building react app...
cd ..\..\..\..\..\Maitinimas-front\

echo Checking Node.js version...
node -v
IF ERRORLEVEL 1 (
echo Node.js is not installed or not found in PATH. Exiting...
exit /B 1
)

echo Checking Node.js version...
cmd /c npm -v
echo Npm version checked: %ERRORLEVEL%

echo Installing dependencies...
cmd /c npm install
echo Dependencies installed: %ERRORLEVEL%

echo Starting Npm Build...
cmd /c npm run build
echo Npm build completed: %ERRORLEVEL%


echo Copying build to target...
cd ..\Maitinimas-back\src\main\resources\public\
cmd /c xcopy /E /Y ..\..\..\..\..\Maitinimas-front\build\* .\
echo Build copied to target: %ERRORLEVEL%

echo Maven clean package...
cd ..\..\..\..
cmd /c mvn clean package
echo Backend binary war package created: %ERRORLEVEL%

echo Build script completed successfully.
ENDLOCAL
@echo off
SETLOCAL

echo Removing old resources...
cd Maitinimas-back\src\main\resources\public
del /Q *.*

echo Building react app...
cd ..\..\..\..\..\Maitinimas-front\

echo Checking Node.js version...
node -v
IF ERRORLEVEL 1 (
echo Node.js is not installed or not found in PATH. Exiting...
exit /B 1
)

echo Checking Node.js version...
cmd /c npm -v
echo Npm version checked: %ERRORLEVEL%

echo Installing dependencies...
cmd /c npm install
echo Dependencies installed: %ERRORLEVEL%

echo Starting Npm Build...
cmd /c npm run build
echo Npm build completed: %ERRORLEVEL%


echo Copying build to target...
cd ..\Maitinimas-back\src\main\resources\public\
cmd /c xcopy /E /Y ..\..\..\..\..\Maitinimas-front\build\* .\
echo Build copied to target: %ERRORLEVEL%

echo Maven clean package...
cd ..\..\..\..
cmd /c mvn clean package
echo Backend binary war package created: %ERRORLEVEL%

echo Build script completed successfully.
ENDLOCAL
The cmd /c in front of every major command that works with either npm or mvn. Even in front of xcopy ....
dan1st
dan1st2mo ago
I think it's only necessary if the commands are batch files and there are probably also other ways to do this But I guess this means it works?
Tomasm21
Tomasm21OP2mo ago
Yes it does. Though I have another problem when the web app is hosted on Tomcat. I open it and I see that it is not found. And I really don't know why. When I launch the app locally - backend Spring Boot launching On Eclipse, and frontend React.js through Visual Code terminal npm start then the application on browser works through localhost:3000/maitinimas. But on Tomcat it should be http://localhost:2020/maitinimas because tomcat is on 2020. Actually when press the app in applications list it opens at http://localhost:2020/Maitinimas-0.0.1-SNAPSHOT/. I thought it had to be http://localhost:2020/Maitinimas, because in React.js package.json I have "homepage": "/Maitinimas/",. And in Spring application.properties I have
//....
server.servlet.context-path=/Maitinimas
//....
//....
server.servlet.context-path=/Maitinimas
//....
. The content that loads in the web app: Do you know how to solve this? Is this in your scope of knowledge?
No description
Tomasm21
Tomasm21OP2mo ago
No description
Tomasm21
Tomasm21OP2mo ago
The build data that was made in Jenkins is this:
dan1st
dan1st2mo ago
Can you try with http://localhost:2020/Maitinimas-0.0.1-SNAPSHOT/Maitinimas? If that doesn't work, can you try with server.servlet.context-path=/Maitinimas-0.0.1-SNAPSHOT and http://localhost:2020/Maitinimas-0.0.1-SNAPSHOT?
Tomasm21
Tomasm21OP2mo ago
the same Message The requested resource [/Maitinimas-0.0.1-SNAPSHOT/Maitinimas] is not available I can try to change this line in application proiperties and the rebuild Doing it.
dan1st
dan1st2mo ago
Can you show your pom.xml?
Tomasm21
Tomasm21OP2mo ago
yes hold on..
Tomasm21
Tomasm21OP2mo ago
Tomasm21
Tomasm21OP2mo ago
this version should not exist: <version>2.7.18</version> Perhaps I accidentally put the mouse pointer and by a chance pressed 8 in keyboard. And saved its 2.7.1
dan1st
dan1st2mo ago
So there isn't anything adding the frontend to the war file?
Tomasm21
Tomasm21OP2mo ago
Nevcertheless, locally the app can be launched:
No description
Tomasm21
Tomasm21OP2mo ago
It should add.
dan1st
dan1st2mo ago
What should add it? I saw nothing supposed to add it
Tomasm21
Tomasm21OP2mo ago
the build from frontend is supposed tro be copied to the \src\main\resources\public\
dan1st
dan1st2mo ago
oh Any specific reason you put it in public?
Tomasm21
Tomasm21OP2mo ago
Lets examine this part:
Starting Npm Build...

//....
Line 24:28: 'setSelectedOption' is assigned a value but never used no-unused-vars


Npm build completed: 1
Copying build to target...
..\..\..\..\..\Maitinimas-front\build\asset-manifest.json
..\..\..\..\..\Maitinimas-front\build\favicon.ico
..\..\..\..\..\Maitinimas-front\build\index.html
..\..\..\..\..\Maitinimas-front\build\logo192.png
..\..\..\..\..\Maitinimas-front\build\logo512.png
..\..\..\..\..\Maitinimas-front\build\manifest.json
..\..\..\..\..\Maitinimas-front\build\robots.txt
..\..\..\..\..\Maitinimas-front\build\static\css\main.5092e7c2.css
..\..\..\..\..\Maitinimas-front\build\static\css\main.5092e7c2.css.map
..\..\..\..\..\Maitinimas-front\build\static\js\51.0eb4a23b.chunk.js
..\..\..\..\..\Maitinimas-front\build\static\js\51.0eb4a23b.chunk.js.map
..\..\..\..\..\Maitinimas-front\build\static\js\main.58189623.js
..\..\..\..\..\Maitinimas-front\build\static\js\main.58189623.js.LICENSE.txt
..\..\..\..\..\Maitinimas-front\build\static\js\main.58189623.js.map
..\..\..\..\..\Maitinimas-front\build\static\media\defaultImg.50bdc6d095ec95623de2.jpg
..\..\..\..\..\Maitinimas-front\build\static\media\loader.03e0d80c740514c36be3e9539c53630f.svg
..\..\..\..\..\Maitinimas-front\build\static\media\maist2.b7fd303c4df627a0e774.png
17 File(s) copied
Build copied to target: 0
Starting Npm Build...

//....
Line 24:28: 'setSelectedOption' is assigned a value but never used no-unused-vars


Npm build completed: 1
Copying build to target...
..\..\..\..\..\Maitinimas-front\build\asset-manifest.json
..\..\..\..\..\Maitinimas-front\build\favicon.ico
..\..\..\..\..\Maitinimas-front\build\index.html
..\..\..\..\..\Maitinimas-front\build\logo192.png
..\..\..\..\..\Maitinimas-front\build\logo512.png
..\..\..\..\..\Maitinimas-front\build\manifest.json
..\..\..\..\..\Maitinimas-front\build\robots.txt
..\..\..\..\..\Maitinimas-front\build\static\css\main.5092e7c2.css
..\..\..\..\..\Maitinimas-front\build\static\css\main.5092e7c2.css.map
..\..\..\..\..\Maitinimas-front\build\static\js\51.0eb4a23b.chunk.js
..\..\..\..\..\Maitinimas-front\build\static\js\51.0eb4a23b.chunk.js.map
..\..\..\..\..\Maitinimas-front\build\static\js\main.58189623.js
..\..\..\..\..\Maitinimas-front\build\static\js\main.58189623.js.LICENSE.txt
..\..\..\..\..\Maitinimas-front\build\static\js\main.58189623.js.map
..\..\..\..\..\Maitinimas-front\build\static\media\defaultImg.50bdc6d095ec95623de2.jpg
..\..\..\..\..\Maitinimas-front\build\static\media\loader.03e0d80c740514c36be3e9539c53630f.svg
..\..\..\..\..\Maitinimas-front\build\static\media\maist2.b7fd303c4df627a0e774.png
17 File(s) copied
Build copied to target: 0
dan1st
dan1st2mo ago
try coping it to src/main/resources/static everything in the static directory because Spring will serve everything in static 1:1
Tomasm21
Tomasm21OP2mo ago
I was taught to copy frontend files to public. In past it worked Are you sure? Should I change batch command at copying?
dan1st
dan1st2mo ago
normally yes Sure it wasn't src/main/resources/static/public?
Tomasm21
Tomasm21OP2mo ago
from
echo Copying build to target...
cd ..\Maitinimas-back\src\main\resources\public\
cmd /c xcopy /E /Y ..\..\..\..\..\Maitinimas-front\build\* .\
echo Build copied to target: %ERRORLEVEL%
echo Copying build to target...
cd ..\Maitinimas-back\src\main\resources\public\
cmd /c xcopy /E /Y ..\..\..\..\..\Maitinimas-front\build\* .\
echo Build copied to target: %ERRORLEVEL%
to
echo Copying build to target...
cd ..\Maitinimas-back\src\main\resources\static\
cmd /c xcopy /E /Y ..\..\..\..\..\Maitinimas-front\build\* .\
echo Build copied to target: %ERRORLEVEL%
echo Copying build to target...
cd ..\Maitinimas-back\src\main\resources\static\
cmd /c xcopy /E /Y ..\..\..\..\..\Maitinimas-front\build\* .\
echo Build copied to target: %ERRORLEVEL%
dan1st
dan1st2mo ago
I mean it would also possible with public but static works without extra configuration? I guess
Tomasm21
Tomasm21OP2mo ago
I copied it from past working project.
dan1st
dan1st2mo ago
I guess that other project had extra (Spring) configuration
Tomasm21
Tomasm21OP2mo ago
so anyway.... What should I do? To change server.servlet.context-path=/Maitinimas to the server.servlet.context-path=/Maitinimas-0.0.1-SNAPSHOT? When I launch this app on docker, it works well. But I need Jenkins
dan1st
dan1st2mo ago
I'd first try without any server.servlet.context-path and use localhost:2000//Maitinimas-0.0.1-SNAPSHOT or whatever it was then you can try figuring out how to do it differently
Tomasm21
Tomasm21OP2mo ago
Tried. Was the same. ChatGPT offered server.servlet.context-path
JavaBot
JavaBot2mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
JavaBot
JavaBot2mo ago
Post Closed
This post has been closed by <@312509109863710732>.
Want results from more Discord servers?
Add your server