Jenkins job for remote deployment - multiple environments - jenkins

I'm trying to create a generic Jenkins job for deploying different projects from different GIT repos and branches to different application servers (in any combination).
I have 2 string build parameters for the repo and for the branch, and a small shell script in a pre-build step which based on the build params creates a deploy.properties file with properties URL and PROFILE.
Another pre-build step is the Inject environment variables which uses the deploy.properties file previously created.
I'm to use the URL property in the Jenkins Deploy Plugin in the following way: Tomcat URL field - $URL.
Also, in the build section, I'm using the PROFILE property: clean install -P$PROFILE .
The problem is that the placeholders or not replaced by the values I've set in the shell script. Not that is I do another post-build action and I'm echoing the same placeholders, the values are replaced and it seems to work. Other check I've done is the Environment Variables section from a given Build and the variable values are there, so the injection works.
Any ideas?

Try the below and make sure you don't have the cmd in single quotes or anything.
clean install -P${PROFILE}

Related

Jenkins pipeline to change appssettings.json file and build according to the environment

I had a requirment to build a console application, but i need to change some values in appssettings.json file according to the environment and then build it. I am new to jenkins and want to know how to acheive this.
for dev change values in json file and build it -> for test again change the json values and build it -> till prod
This can be done in multiple ways for example (the common idea between these is to check the incoming branch):
You might find better ways to do it but you can use this as a start.
Using bash, jq, sponge through sh step:
Create a json file as a template like the following (consider keeping this file in a version control to clone every build)
# settings.json
{
environment: 'ENVIRONMENT_NAME',
appVersion: 'APP_VERSION'
}
Check the branch name value through if condition and update the template according to the branch value
jq '.environment = "branch_name"' settings.json|sponge settings.json
Use the customized settings.json in your application's code
Using Config File Provider Plugin which can be used inside the Jenkins pipeline as the following (also update it based on the branch name)
configFileProvider([configFile(fileId: 'FILE_ID', targetLocation: 'FILE_LOCATION')]) {}
Check if the application framework can make use of environment variables.

jenkins parameter From Properties file

I have 3 Jenkins jobs to be run in serial.
Run a Ant File
Run another ANT File
Run a command line
All the above jobs use a file path which is set in a properties file.
Ex Job 1 , Executes ANT file placed in file path location
Job 2 , Executes another file placed in same file path location
Job 3 , Executes command line to do SVN update in same file path location
I need to parameterize the file path in all three builds from properties file.
Can anyone help me with possible approach?
Thanks In Advance
This answer could be a little high level. You can use Jenkins Pipeline as a code for this approach instead of using 3 freestyle jobs.
You can create 3 stages which performs these 3 steps. Pipeline as a code supports reading of properties from different file types (json, yaml etc.)
Look for the "EnvInject" plugin. This lets you inject properties into your build as environment variables; these assignments survive build step boundaries.
If the property file is checked in, you can load it in the Build Environment section before the build steps start executing. If the property file is generated during the build sequence, you can add a build step between where the property file is created and where it is used.
Once set, if the property file contains "FOO=/path/to/folder" then in configuring Jenkins things you would refer to $FOO or ${FOO} (for example, an Ant build step might specify "${FOO}/build.xml"; in Windows batch script execution FOO shows up as an environment variable and is referenced by %FOO% (i.e., "#echo Some_Useful_Piece_Of_Data > %FOO%\data.txt"
More information can be found here: https://wiki.jenkins.io/display/JENKINS/EnvInject+Plugin

Build Task to Conditionally Copy Files to a Directory

I am completely new to vNext Tasks & need some help. I have some Custom Configuration values defined in my Solutions, as follows:
DEV
DEV.HA (HA stands for High Availability)
QA
QA.HA
UAT
UAT.HA
PROD
PROD.HA
I have Service Account certificates in a set of folders separated by-environment. These need to be copied into a KEYS folder to replace the default files there.
FOR INSTANCE:
DEV & DEV.HA would get from one source-folder
QA & QA.HA would get from a different source-folder
...and so on.
How do you do conditional copying in vNext?
Which vNext task is best for that?
Either stick the "variable part" in a variable. This would be possible if you have naming logic that allows you to do configuration by convention.
Or put the logic in a powershell script or shell script and run that to set the values of the variables. There are special logging commands you can use to set the value of a variable from your script:
write-host ##vso[task.setvariable variable=YourVariableName;]QA
Then use the variable either in the standard Copy Files task or stick the copy files logic in Powershell as well.

Getting CFBundleVersion from within Jenkins to use it as a variable, something like ${APP_VERSION}

What I'd like to do is to be able to add a tag to commits which Jenkins is building from. Right now I tag commits with the Jenkins build number but I want to also add in the app version as listed in the Info.plist CFBundleVersion in front of that.
What I want to know is, how I can grab that value using Jenkins or otherwise and be able to use that as a parameter/variable within Jenkins?
I've seen references to using plistbuddy to set this value so I would assume there's a way to use that to get the same value. Though how and how to get that to where I can use it in Jenkins I don't know.
For further clarification I am using Git Publisher in Jenkins to create a tag and push it with this format
jenkinsbuild-$BUILD_NUMBER
This results in a tag on the commit in git like this - jenkinsbuild-303
What I want, assuming my app is currently at version 3.5 is a tag that reads - jenkinsbuild-3.5-303
I managed to piece together a solution from the answer which #agy linked to. Here's what I did:
In the Build section for the Jenkins configuration, I added the following two lines to an 'Execute Shell' step after the Xcode step:
APP_VERSION=$(/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" "path to your plist")
echo APP_VERSION=$APP_VERSION > appversion.properties;
After that, I added an 'Inject Environment Variables' step (I think this is part of the EnvInject plugin), to which I added "appversion.properties" to the Properties File Path field.
After this is done, APP_VERSION is now available as an environment variable in subsequent shell command steps.

Jenkins deploy plugin - pass parameters from properties file

I'm using Jenkins and deploy plugin, with which I'm deploying to tomcat server. How can I pass parameters from properties file to this deploy plugin?
for example I want to pass my property app.server.url to Tomcat URL field.
I tried to pass $app.server.url also %app.server.url%, but that doesn't work.
I am assuming you have a properties file in the format app.server.url=somevalue
You then can inject these properties into the Jenkins environment using EnvInjec Plugin. Use Inject environment variables for your job build step, and just specify your file path (leave the content field blank). After that, you can reference it like this $app.server.url for *nix, or %app.server.url% for Windows. However this only works on the shell level. On the plugin properties level, a lot of plugins are expecting properties only in *nix-style format.
The further problem is that *nix-style variables do not allow dots .. So $app.server.url is not valid on *nix. I do not know if it will work on plugin-level on Windows though.
So after setting up EnvInject plugin, try the following two:
Try using $app.server.url in your deploy plugin.
Else change the property file to something like app_server_url=somevalue, and then try $app_server_url in deploy plugin.

Resources