Reputation: 147
I have this 'Execute system Groovy Script' in my Jenkins job (not pipeline)...
import hudson.EnvVars
import hudson.model.Environment
def process_type = build.properties.environment.PROCESSTYPE.toString();
def SDATE = build.properties.environment.START_DATE.toString();
def EDATE = build.properties.environment.END_DATE.toString();
println "Old Dates :"
println "$SDATE"
println "$EDATE"
if(process_type == "Nightly") {
def today = new Date()
def sevenDaysAgo = today - 7
def START_DATE2 = sevenDaysAgo.format("yyyy-MM-dd")
def END_DATE2 = today.format("yyyy-MM-dd")
String START_DATE = START_DATE2.toString()
String END_DATE = END_DATE2.toString()
println "New Dates :"
println "$START_DATE"
println "$END_DATE"
String vars = [START_DATE: "$START_DATE", END_DATE: "$END_DATE"]
}
I may have more in it than I need, as using Groovy inside of a Jenkins job is new to me and I have been trying all kinds of things to get this to work. Anyway, this Jenkins job can be run 'Nightly, and then would use a seven day date range, or it can be supplied with a date range. If 'Nightly', I want to set the 7 day date range (overwriting the two default dates), and then use the new dates in the next build step, which is a 'Execute Windows Batch command", and uses the arguments...'%START_DATE%' '%END_DATE%'.
The code above gets executed, but the windows step is still using the old default dates. So,how do I change the environmental variable in the groovy step, and pass it back to Jenkins, so that the windows batch step gets the new values?
Upvotes: 0
Views: 536
Reputation: 147
Still not sure that I am not using more API's than I need, but I have the following code working. It appears that you cannot modify the values of an already existing Job Parameter, so I had to create a new one to be used in the next job step. Here is the code...
import hudson.EnvVars
import hudson.model.Environment
def process_type = build.properties.environment.PROCESSTYPE.toString();
def SDATE = build.properties.environment.START_DATE.toString();
def EDATE = build.properties.environment.END_DATE.toString();
println "Old Dates :"
println "$SDATE"
println "$EDATE"
EnvVars envVars = build.getEnvironment(listener);
if(process_type == "Nightly") {
def today = new Date()
def sevenDaysAgo = today - 7
def START_DATE2 = sevenDaysAgo.format("yyyy-MM-dd")
def END_DATE2 = today.format("yyyy-MM-dd")
String START_DATE = START_DATE2.toString()
String END_DATE = END_DATE2.toString()
println "New Dates :"
println "$START_DATE"
println "$END_DATE"
File file = new File(envVars.get('WORKSPACE') + "\\Dates.txt")
file.write "NEW_START_DATE=" + "$START_DATE" + "\r\n"
file << "NEW_END_DATE=" + "$END_DATE" + "\r\n"
println file.text
} else
{
// The next step 'Inject Environment variables', requires this file to exist
// if not a nightly run use date range provided in parameters
println "New Old Dates :"
println "$SDATE"
println "$EDATE"
File file = new File(envVars.get('WORKSPACE') + "\\Dates.txt")
file.write "NEW_START_DATE=" + "$SDATE" + "\r\n"
file << "NEW_END_DATE=" + "$EDATE" + "\r\n"
println file.text
}
Upvotes: 0