TeamCity Current Date variable in MMdd format

岁酱吖の 提交于 2019-12-03 04:52:39

问题


In TeamCity is there an easy way to get a variable for the current date in the format MMdd (eg 0811 for 8-Aug)?

My google-fu did not turn up an existing plugins. I looked into writing a plugin, but not having a jdk installed, that looks time consuming.


回答1:


The Groovy Plugin for TeamCity provides build start date/time properties:

Provides build properties:

system.build.start.date / env.BUILD_START_DATE

system.build.start.time / env.BUILD_START_TIME

This blog post has installation / configuration instructions for the Groovy plugin, as well an example of customizing the date/time format.




回答2:


This is quite easy to do with a PowerShell build step (no plugin required) using the following source code:

echo "##teamcity[setParameter name='env.BUILD_START_TIME' value='$([DateTime]::Now)']"

or (for UTC):

echo "##teamcity[setParameter name='env.BUILD_START_TIME' value='$([DateTime]::UtcNow)']"

This uses TeamCity's Service Message feature that allows you to interact with the build engine at runtime e.g. set build parameters.

You can then reference this build parameter from other places in TeamCity using the syntax %env.BUILD_START_TIME%

The advantage of this approach is you don't need to use a plugin. The disadvantage is you need to introduce a build step.




回答3:


For Unix based build agents I propose next custom script as one of build commands:

export current_build_date_format="+%%Y.%%m.%%d"
export current_build_date="$(date $current_build_date_format)"
echo "##teamcity[setParameter name='env.current_build_date' value='$current_build_date']"

You have to make double % sign to avoid interpretation for date executable command line argument FORMAT string (see %Y.%m.%d) as already existing TeamCity variable.




回答4:


You can also try Date Build Number plug-in. It povides additional var in build number format rather than build property.




回答5:


To add a dated folder to my build in TeamCity I added the following to my custom script. What had me stuck was the double % sign in the date string. D'oh

TARGET_DIR=/Users/admin/build/daily
TARGET=$(date "+%%Y-%%m-%%d")

if [ ! -d ${TARGET_DIR} ]; then
  mkdir -vp ${TARGET_DIR}/
fi
mv -v build.dmg ${TARGET_DIR}/build_${TARGET}.dmg



回答6:


If you only want to have one-line bash command in a build step, just use as below.

echo "##teamcity[setParameter name='build.timestamp' value='$(date +%%m%%d)']"

(double % symbol is for TeamCity own escape rule to use % character)

It will set a MMdd parameter value right after the execution during runtime so very useful to put at any build step. Then, you can retrieve a parameter value afterward.

Note that you should create build.timestamp parameter firstly to TeamCity project.

A step further, I made a simple bash script to have bash date format timestamp. This script will set timestamp to whatever bash supported datetime format and parameter name to TeamCity.

name=""  # TeamCity parameter name
format="%Y-%m-%dT%H:%M:%S%z"  # ISO8601 format by default
result=""  # a TeamCity parameter value to be set

for ARGUMENT in "$@"
do
    KEY=$(echo "$ARGUMENT" | cut -f1 -d=)
    VALUE=$(echo "$ARGUMENT" | cut -f2 -d=)

    case "$KEY" in
            name)              name=${VALUE} ;;
            format)     format=${VALUE} ;;
            *)
    esac
done

result=$(date "+$format")

echo "##teamcity[setParameter name='$name' value='$result']"

Below usage will set ISO8601 format timestamp to build.timestamp parameter.

./teamcity_datetime.sh name=build.timestamp

If you want to set only MMdd, the execution could be as below.

./teamcity_datetime.sh name=build.timestamp format="%%m%%d"


来源:https://stackoverflow.com/questions/7019954/teamcity-current-date-variable-in-mmdd-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!