Jenkins Post Build Task access jenkins environment variables like JOB_NAME - jenkins-plugins

I want to get the job details in Post Build Task plugin and pass the details to the batch/ powershell for further processing.
I am not able to access the Jenkins environment variables like JOB_NAME, JOB_ID etc.
In Post Build Plugin task
Log text "BUILD SUCCESSFUL"
OPTION
Script Block:
run.bat $JOB_NAME-$JOB_ID
I need to pass the $JOB_NAME-$JOB_ID to the script.

Build Parameters are accesses as $Name in the Execute shell and post build section.
You can use
$JOB_NAME
$BUILD_NUMBER
for name of job and build number which is same as JOB_ID.
Also Set "Jenkins user build variables" to get more info regarding Environment variables.

Generally the sintax for accessing variables is the following:
${VARIABLE}
but in some cases, especially when you are trying to access variables using during the build, this sintax can be used:
${ENV, var="VARIABLE"}
That in my case is working also when you have a parameterized build.

Related

Jenkins parameter is getting replaced by a white space

I have a parameterized build (copied from an existing job) in Jenkins. While building, my parameter is getting replaced by a white space. Here I am sharing what I have.
Parameter Name: BuildDate (I tried using %% and ${} but no luck)
Windows batch command:
copy \\Network_drive\dir1\dir2\dir3%BuildDate%\filename*.zip .
Output from Jenkins console:
c:\jenkins\workspace\my_build_job_name>copy \\Network_drive\dir1\dir2\dir3\ \filename*.zip .
\\Network_drive\dir1\dir2\dir3\ \filename*.zip
The system cannot find the path specified. 0 file(s) copied
c:\jenkins\workspace\my_build_job_name>exit 1
Build step 'Execute Windows batch command' marked build as failure
Notifying upstream projects of job completion Finished: FAILURE*
Note: We have a Windows/Linux mixed environment. Whenever we want to run shell scripts, we use Cygwin, batch should directly run.
Finally it turned out that one of the parameters was not populating due to hitting "rebuild last". Re-running the whole pipeline did it work. I still wonder why it was not populating but its all hood now!!!

manage Jenkins build status

I'd like to run batch using jenkins. And the status of build depends of number of files created in a specific folder. My question is how could I manage Jenkins build status depending of number of files created?
You can execute a shell script to count the files and return 1, if the count isn't expected.
Another way would be to use the Text-finder Plugin searching for a pattern in the console log.
Groovy Postbuild Plugin is another alternative:
buildUnstable() - sets the build result to UNSTABLE.
If you like to use the CLI you can use the following command:
java -jar jenkins-cli.jar -s http://...:8080/ set-build-result
Sets the result of the current build. Works only if invoked from within a build.

Jenkins -Gradle -Java

I am trying to parameterize my build in Jenkins by taking two variables and pass it to gradle build file which should finally be used by my java file. How could I achieve this?
See Jenkins, Parameterized Build:
Sometimes, it is useful/necessary to have your builds take several "parameters".
[...]
The parameters are available as environment parametersvariables. So e.g. a shell ($FOO, %FOO%) or Ant ( ${env.FOO} ) can access these values.
[Correction by me.]
Those variables are available throughout the build so your Gradle build file can use them.

Post-build arguments to batch script fails to convert build parameter

I'm using TFS 2013 and am building an Azure Cloud Service project that I want to package with Nuget so that I can publish to Octopus Deploy. I can't use octopack, because at the moment this is not supported. I'm trying to pass TFS parameters into a post-build batch script so that I can run nuget with -version parameters (which should change with each build).
The problem I'm having is that the batch script does not recognise the TFS build parameters. for example, in the script I want to pass an argument version $(TeamProject)-1.0.0$(Rev:.r), that would give the script the version to set in the package name.
The full nuget package call in the script is:
%nugetPath% push %packagePath%\Veedyo.%version%.nupkg
Passing this into the post-build script path works:
$/Application1/MAIN/Source/.NET/Application1.Package/package.cmd
This is because the source control path is translated into an actual path just after the build completes (and I can see the real path in the log).
However, the Post-build script arguments property in the build, doesn't convert this "$(TeamProject)"-1.0.0"$(Rev:.r)" to the desired value. this leads to an error executing the batch script:
Exception Message: TF270015: 'package.cmd' returned an unexpected exit code. Expected '0'; actual '1'. See the build logs for more details
So, does anyone have any idea how to convert add build parameter to the post-build script arguments property?
These macros are not available when you run the script. You can use one the the TF_BUILD environment variables listed here.
Probably, you are looking for TF_BUILD_BUILDNUMBER, or maybe you have to extract the data you are looking for; in this latter case Powershell can be simpler to use than cmd.exe interpreter.

Jenkins commit a file after successful build

I am using Jenkins, Ant , Flex and Java for my web application.
Currently I update a build version file in Flex src and commit it before starting Jenkins build.
I want to avoid this manual process and let script do this for me.
Contents of file:
Build=01_01_2013_10:43
Release=2.01
Question1:
I want to update this file contents and compile my code and then commit this file back to svn. So that SVN has latest build version number.
How do I commit this changed file to SVN. Would be great if commit happens after successful build.
Question2: I want to send an email to all developers an hour before build starts. "Please commit your changes. Build will start in 1 hr." Can I set up a delay between email and (actual svn export + ant build).
or
Do I have to schedule 2 jobs an hour apart. One to send email and one to do build.
You can use the subclipse svn ant integration to commit changed files to SVN including authentication:
<svnSetting
svnkit="true"
username="bingo"
password="bongo"
id="svn.settings"
/>
<svn refid="svn.settings">
<commit file="your.file" />
</svn>
To get username and password to the build file you have different options. One would be to use a parametrized build, where you define user name and password as build parameters which can be evaluated in the build file.
username="${parameter.svn.username}"
password="${parameter.svn.password}"
A second option is using a the jenkins config file provider plugin. With this you can also use the parameters like for the parametrized build, but you import the credentials from the provided config file, e.g. a properties file can be imported via
<property file="config.file" />
Actually you can also use ant's exec task to execute your subversion commit the file.
For sending an e-mail one hour before actually building, you should setup two jobs, which are scheduled one hour apart. But I don't think this is good practice to notify before building, consider to build more often maybe even per commit to svn.
You can also use the Post build Task plugin (https://wiki.jenkins-ci.org/display/JENKINS/Post+build+task) to execute svn as a shell script (svn must be installed and authenticated from the shell once for the user that runs Jenkins).
Then the svn commit runs as a post build action. The plugin has an option (checkbox) to run the script only if the previous build/steps were successful.
The plugins is also mentioned here: Execute Shell Script after post build in Jenkins

Resources