MMdd 格式的 TeamCity 当前日期变量

发布于 2024-11-29 11:49:51 字数 126 浏览 1 评论 0 原文

在 TeamCity 中,是否有一种简单的方法可以获取 MMdd 格式的当前日期变量(例如 0811 表示 8 月 8 日)?

我的 google-fu 没有找到现有的插件。我想写一个插件,但没有安装jdk,看起来很耗时。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

请爱~陌生人 2024-12-06 11:49:51

使用以下源代码通过 PowerShell 构建步骤(无需插件)可以轻松完成此操作:

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

或(对于 UTC):

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

这使用 TeamCity 的 服务消息功能,允许您在运行时与构建引擎交互,例如设置构建参数。

然后,您可以使用语法 %env.BUILD_START_TIME% 从 TeamCity 中的其他位置引用此构建参数。

这种方法的优点是您不需要使用插件。缺点是您需要引入构建步骤。

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.

め七分饶幸 2024-12-06 11:49:51

对于基于 Unix 的构建代理,我建议下一个自定义脚本作为构建命令之一:

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']"

您必须使用双 % 符号以避免解释 日期 可执行命令行参数 格式字符串(请参阅 %Y.%m.%d)作为已存在的 TeamCity 变量。

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.

煞人兵器 2024-12-06 11:49:51

TeamCity 的 Groovy 插件 提供构建开始日期/时间属性:

提供构建属性:

system.build.start.date / env.BUILD_START_DATE

system.build.start.time / env.BUILD_START_TIME

此博客帖子有 Groovy 插件的安装/配置说明,以及自定义日期/时间格式的示例。

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.

迎风吟唱 2024-12-06 11:49:51

这是一个老问题,但对于那些寻找解决方案的人来说,现在有一个可用的系统参数。

system.buildStartTime

您需要在配置中声明它(直到运行时才可用)才能运行。我将我的设置为值[自动填充]

正如您可以猜到的,这个时间设置为构建开始时间,因此这对于某些需求来说可能并不理想。但它既简单又可靠。

An old question, but for those looking for a solution now there is a system parameter available.

system.buildStartTime

You need to declare it in config (it's not available until runtime) in order to run. I set mine to value [Filled Automatically]

As you can guess, this time is set to the build start time, so that may not be ideal for some needs. But it's easy and reliable.

递刀给你 2024-12-06 11:49:51

您还可以尝试日期版本号插件。它以构建号格式提供附加变量,而不是构建属性。

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

玩套路吗 2024-12-06 11:49:51

日期版本号插件类似https://stackoverflow.com/a/15020273/1811525">这个答案,存在一个名为格式化日期参数。它提供了一个可自定义的参数build.formatted.timestamp,可以在字段或其他参数中开箱即用。无需单独的构建步骤。

Similar to the Date Build Number plugin mentioned in this answer, there exists a derived plugin called Formatted Date Parameter. It provides a customizable parameter build.formatted.timestamp that can be used out of the box in fields or other parameters. No need for a separate build step.

梦途 2024-12-06 11:49:51

为了将日期文件夹添加到我在 TeamCity 中的构建中,我将以下内容添加到我的自定义脚本中。让我陷入困境的是日期字符串中的双%符号。多哦

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

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
通知家属抬走 2024-12-06 11:49:51

如果您只想在构建步骤中使用一行 bash 命令,请按如下所示使用。

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

(双 % 符号是 TeamCity 自己的转义规则使用 % 字符)

它将在运行时执行后立即设置 MMdd 参数值,因此在任何构建中都非常有用步。然后,您可以检索参数值。

请注意,您应该首先为 TeamCity 项目创建 build.timestamp 参数。

更进一步,我制作了一个简单的 bash 脚本,以具有 bash 日期格式 时间戳。此脚本将时间戳设置为任何 bash 支持的日期时间格式,并将参数名称设置为 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']"

下面的用法将 ISO8601 格式时间戳设置为 build.timestamp 参数。

./teamcity_datetime.sh name=build.timestamp

如果您只想设置MMdd,则执行如下。

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

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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文