Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Jenkins environment variable from script

I'm trying to setup a script to increment the build number of my Xcode project. I make an API call to get the current build number, then I wanted to increment it and apply that new number as an environment variable so that the Xcode Plugin can use it.

I have the EnvInject plugin installed but I don't know how to get the var from my shell script into a Environment Variable.

APP_BUILD_NUMBER=$(curl --request GET 'https://api.domain.com/api/GetBuildNumber')
APP_BUILD_NUMBER=$((APP_BUILD_NUMBER +1))

This sets APP_BUILD_NUMBER to the value I need, but how do I assign this to a environment variable so I can access it later on in my job?

like image 801
ChickensDontClap Avatar asked May 21 '14 14:05

ChickensDontClap


People also ask

How do you declare a variable in Jenkins pipeline script?

Jenkins pipeline environment variables: You can define your environment variables in both — global and per-stage — simultaneously. Globally defined variables can be used in all stages but stage defined variables can only be used within that stage. Environment variables can be defined using NAME = VALUE syntax.

How do I set environment variables in Groovy?

Download a binary distribution of Groovy and unpack it into some folder on your local file system. Set your GROOVY_HOME environment variable to the directory where you unpacked the distribution. Add GROOVY_HOME/bin to your PATH environment variable. Set your JAVA_HOME environment variable to point to your JDK.

How can I see Jenkins environment variables?

Goto to the /job/<project>/configure screen. In "Build Environment" section check "Inject environment variables to the build process"


2 Answers

Add a build step to execute shell - in there determine APP_BUILD_NUMBER and output to file, e.g.

APP_BUILD_NUMBER=$(curl --request GET 'https://api.domain.com/api/GetBuildNumber')
APP_BUILD_NUMBER=$((APP_BUILD_NUMBER +1))
echo APP_BUILD_NUMBER=$APP_BUILD_NUMBER > build.properties

then add build step Inject environment variables and set there Properties File Path to $WORKSPACE/build.properties

after that $APP_BUILD_NUMBER is accessible in all build steps after as environment variable; e.g. in Xcode build step

like image 133
MrsTang Avatar answered Oct 19 '22 15:10

MrsTang


These are also worth considering

  • https://wiki.jenkins-ci.org/display/JENKINS/Environment+Script+Plugin.
  • https://wiki.jenkins-ci.org/display/JENKINS/EnvInject+Plugin
like image 38
Federico Avatar answered Oct 19 '22 17:10

Federico