更改时间戳变量格式或进行替换

发布于 2025-01-14 12:56:30 字数 193 浏览 0 评论 0原文

我目前正在努力设置 ci/cd 管道来推送 nuget 包。

我想使用内置的 CI_COMMIT_TIMESTAMP 作为版本后缀,但其 ISO 8601 格式对此无效。

示例 ISO 8601 (UTC):2022-03-15T18:34:43Z

至少需要替换冒号。

是否可以对其进行不同的格式或进行某种文本替换?

I am currently working on setting up ci/cd pipeline for pushing nuget packages.

I want to use the built-in CI_COMMIT_TIMESTAMP for version suffix however its ISO 8601 format is not valid for this.

Example ISO 8601 (UTC): 2022-03-15T18:34:43Z

Will need to at least replace colon.

Is it possible to format it differently or do some sort of text replacement?

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

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

发布评论

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

评论(1

清晨说晚安 2025-01-21 12:56:30

您无法更改变量的呈现方式,但可以在作业中重新格式化该值。

unix date 命令可以做到这一点。例如,您可以将任何有效格式声明为所需的输出格式。

MY_JOB:
  variables:
    DESIRED_FORMAT: "%Y-%m-%dT%H-%M-%S"
  script:
    - nuget_format="$(date -d "$CI_COMMIT_TIMESTAMP" +"$DESIRED_FORMAT")"
    - echo "$nuget_format" 

这将产生如下输出:

2022-03-15T23-43-17

另一种方法是使用 sed 将出现的 : 替换为 -

script:
  - nuget_time_format="$(sed "s/:/-/g" <<< $CI_COMMIT_TIMESTAMP)"
  - echo "$nuget_time_format"

You can't change how the variable is presented, but you can reformat the value in your job.

The unix date command can do this. For example, you can declare any valid format as the desired output format.

MY_JOB:
  variables:
    DESIRED_FORMAT: "%Y-%m-%dT%H-%M-%S"
  script:
    - nuget_format="$(date -d "$CI_COMMIT_TIMESTAMP" +"$DESIRED_FORMAT")"
    - echo "$nuget_format" 

This will have an output like:

2022-03-15T23-43-17

An alternative may be to use sed to replace the occurrences of : with -.

script:
  - nuget_time_format="$(sed "s/:/-/g" <<< $CI_COMMIT_TIMESTAMP)"
  - echo "$nuget_time_format"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文