How can i make Jenkins do the following:
When i create a WAR file i normally run the following command:
mvn clean install -Dapp.env=SOME_ENVIRONMENT
Which loads the specific properties for that environment. I've added a build parameter in Jenkins, but this has had no effect.
When you add a new job or modifying an existing job you can put this directly into Goals and options under 'Build':
clean install -Dapp.env=SOME_ENVIRONMENT
Related
I am setting up Jenkins job and I need to make it parametrized such that it can take below parameters and pass it to my build script.
Parameters -
Entry point: E1/E2 (default : E1)
Protocols: ABC, DEF, … (default : all)
Build script :
cd ${WORKSPACE}/myworkSpace
mvn clean install -P **E1** -Dformat.type=**ABC**
mvn clean install -P **E2** -Dformat.type=**DEF**
I tried with $Parameter but it dint work for me. Please suggest what is correct approach to do this parametrization.
From your comments, I understand that you have defined two parameters from the UI, and you were able to add a default value.
entry_point=E1
format=ABC
In your build's UI, you have a "Execute Shell" section, to re-use these parameters:
cd ${WORKSPACE}/myworkSpace
mvn clean install -P $entry_point -Dformat.type=$format
When executed with the default commands, it will produce the following command:
mvn clean install -P E1 -Dformat.type=ABC
Your variable name does not need to be in uppercase. But the name should be identical.
If you need more details about the fancy usage of this awesome plugin, you can have a look there:
https://wiki.jenkins.io/display/JENKINS/Parameterized+Build
I am building a gradle project and the source code is in git. After checking our repo to jenkins' workspace, how do i have jenkins go to a sub-directory and do the build?
I tried adding shell commands but cd will not work as it executes script on a separate shell.
If you are building a gradle project, perhaps you should use the "Invoke gradle script" step rather than a shell script?
As part of the gradle build script, it has an option to specify the "Root Build script", which will let allow you to specify a subdirectory if you wish.
See the Gradle plugin for more information.
I'm looking at a Jenkins job and trying to understand it.
I have an Execute shell command box in my Build section:
> mkdir mydir
> cd mydir
>
> svn export --force https://example.com/repo/mydir .
When Jenkins is done executing that command, and moves on to the next build step, what is its working directory?
workspece-root/ or workspace-root/mydir ?
As the next step, I have Invoke top-level Maven targets (still in the Build section).
What I really want to know is: why does that execute successfully?
Is it because Jenkins automatically moves back to the workspace-root/ folder after executing a shell command box, or is it because the next job is a "top-level" job, and Jenkins therefore changes back to the workspace-root/?
Each build step is a separate process that Jenkins spawns off. They don't share anything, neither current directory, nor environment variables set/changed within the build step. Each new build step starts by spawning a new process off the parent process (the one running Jenkins)
It's not that Jenkins "move back" to $WORKSPACE. It's that Jenkins discards the previous session.
I lately saw that if you print the CWD , I would get the Project_NAME.
E.g
D:\jenkins\workspace\My_Project
Any script you might be running wont be found. Hence we can do a "CD path" before we start out scripts.
Slav's explanation is very good and I thought of complementing it by providing a real world example that shows how multiple Windows batch commands look like even if they work in the same directory:
Command 1
REM #ensures that all npm packages are downloaded
cd "%WORKSPACE%"
npm install
Command 2
REM #performs a prod-mode build of the project
cd "%WORKSPACE%"
ng build --prod --aot=true -environment=pp
So, each one ensure that current working directory points to the current project directory.
I want to pass a dynamic parameter in Jenkins in a scheduled job (this build runs every day at 3:00 am)
This works if I executed it in my linux command line:
mvn package -DintegrationTag=$(date +%d-%m-%y)
or
mvn package -DintegrationTag="$(date +%d-%m-%y)"
or
mvn package -DintegrationTag="$(date +"%d-%m-%y")"
with these 3 options this is what is executed, for example (this is what I want to do in Jenkins):
mvn package -DintegrationTag=16-09-2013
but any of these sentences, do not work in my Jenkins goals and options (because the dynamic parameter).
Is there any way to do it?
The solution:
Content of the file which constains the script:
echo "NOW=`date +%d-%m-%y`"> env.properties
Path of the properties file:
env.properties
In project, goals and options:
clean test package -DintegrationTag=$NOW
Inject environment variables to the build process = true
In a Build "execute shell" section add this
NOW=`date +%d-%m-%y`
mvn package -DintegrationTag=$NOW
Another option can be to execute a top level maven target in jenkins.
The first two steps of injecting the required variable value into the build environment remains same as the answer given by #Iker below.
In the third step, give goal as
clean test packageand then in Properties section within the 'Advanced' tab, giveintegrationTag=$<your variable name>
Note that this solution is useful when one creates a free style project in jenkins. For maven 2/3 projects,solution by #Iker is good:)
I have a sbt project with 4 modules: module-a, module-b, module-c, module-d.
Each module can be packaged as a WAR. I want to set up a deployment on Jenkins that would build only one of the 4 modules and deploy it to a container.
In detail, I want to have 4 Jenkins jobs - job-a, job-b, job-c, job-d, each building only the defined module (a to d).
For now, I am using clean update test package as the command for the Jenkins sbt build, but this results in packaging all 4 modules that is not necessary.
I already tried project -module-a clean update test package but with no luck.
You may also like to execute project-scoped clean and test tasks as follows:
sbt module-a/clean module-a/test
The solution is slightly shorter and clearer as to what project the following commands apply to.
You don't need to execute update task since it's implicitly executed by test as described in inspect tree test.
There's a way to make it cleaner with an alias. Use the following in the build.sbt:
addCommandAlias("jenkinsJob4ModuleA", "; module-a/clean; module-a/test")
With the alias, execute jenkinsJob4ModuleA to have the same effect as the above solution.
Quote the argument to project, i.e. project module-a, and don't use a dash before the name of the submodule.
The entire command line for the Jenkins job would than be as follows:
./sbt "project module-a" clean update test