Groovy:如何在变量中保存函数运行的结果?

时间:2016-10-18 10:46:25

标签: jenkins groovy jenkins-pipeline

我正在Groovy编写一个Jenkins管道脚本,我正在为Mixpanel设置注释,为此我必须提供一些细节,例如“RELEASE_DATE”,这里是Jenkinsfile的相关部分:

 MP_API_KEY = "XXXXXXXXXX"
 MP_API_SECRET = "XXXXXXXXXXX"
 MP_EXPIRE = "1588896000"
 MP_APP_PLATFORM = "Android"
 MP_BASE_URL = "http://mixpanel.com/api/2.0/annotations/create?"
 def get_current_time_date() {
     Date date = new Date();   // given date
     Calendar calendar = GregorianCalendar.getInstance(); // creates a new calendar instance
     calendar.setTime(date);   // assigns calendar to given date 
     HOUR24 = calendar.get(Calendar.HOUR_OF_DAY); // gets hour in 24h format
     timem = calendar.get(Calendar.MINUTE);
     times = calendar.get(Calendar.SECOND);
     newdate = date.format( 'yyyy-MM-dd' );
     println newdate +' '+ HOUR24 + ':' + timem + ':' + times
 }
 RELEASE_DATE = get_current_time_date()
 MP_RELEASE_NOTES = ""
 DESCRIPTION = "${MP_APP_PLATFORM} v${MP_VERSION_NAME} ${MP_RELEASE_NOTES}"
 REQUEST_URL = "api_key=${MP_API_KEY}&date=${RELEASE_DATE}&description='${DESCRIPTION}'&expire=${MP_EXPIRE}"

我正在尝试使用“get_current_time_date”函数的结果填充RELEASE_DATE变量,但无济于事。

所以我尝试过的事情是:

RELEASE_DATE = get_current_time_date
def RELEASE_DATE = get_current_time_date
def RELEASE_DATE = new get_current_time_date
def RELEASE_DATE = { -> get_current_time_date() }

运行函数的示例结果:

2016-10-18 12:44:35

1 个答案:

答案 0 :(得分:1)

试一试。

 def get_current_time_date() {
     ...your code..
     def result = newdate +' '+ HOUR24 + ':' + timem + ':' + times
     println result
     result
 }
 RELEASE_DATE = get_current_time_date()