简体   繁体   English

AWS codebuild 将时间戳定义为全局变量

[英]AWS codebuild define timestamp as a global variable

i am trying to define timestamp as a global variable but it doesn't seem to work.我试图将时间戳定义为全局变量,但它似乎不起作用。 When i try to echo the time stamp value i get $(date +%Y-%m-%d-%H-%M) instead of the date and time value.当我尝试回显时间戳值时,我得到 $(date +%Y-%m-%d-%H-%M) 而不是日期和时间值。 I want to be able to get something like this when i echo out.当我回声时,我希望能够得到这样的东西。 2022-07-20-23-24 2022-07-20-23-24

 version: 0.2 env: variables: TIMESTAMP: $(date +%Y-%m-%d-%H-%M) parameter-store: USERNAME: "username" PASSWORD: "secret" Phases: install: runtime-version: python:3.8 pre_build: commands: - echo '$TIMESTAMP' build: commands: - echo '$TIMESTAMP'

The only way I know of to make it work, is by saving timestamp in a tmp file.我知道使它工作的唯一方法是将时间戳保存在 tmp 文件中。 For example:例如:

version: 0.2
env:
  parameter-store:
    USERNAME: "username"
    PASSWORD: "secret"
   
Phases:
  install:
    runtime-version:
    python:3.8  
  
  pre_build:
    commands:
      - echo $(date +%Y-%m-%d-%H-%M) > /tmp/timestamp
      - echo $(cat /tmp/timestamp)
  
  build:
    commands:
      - echo $(cat /tmp/timestamp)

Try setting the variable during the install phase.尝试在安装阶段设置变量。

echo-datetime.sh回声-datetime.sh

#!/bin/bash

echo "$DATETIME"

buildspec.yml构建规范.yml

version: 0.2

phases:
    
  install:
    commands:
      - DATETIME=$(date +"%Y-%m-%d-%H-%M-%S")

  pre_build:
    commands:
      - echo $DATETIME
      - chmod +x ./echo-datetime.sh

  build:
    commands:
      - ./echo-datetime.sh
        
  post_build:
    commands:
      - echo $DATETIME

Partial log from CodeBuild来自 CodeBuild 的部分日志

[Container] 2023/01/13 14:52:44 Entering phase INSTALL
[Container] 2023/01/13 14:52:44 Running command DATETIME=$(date +"%Y-%m-%d-%H-%M-%S")

[Container] 2023/01/13 14:52:44 Phase complete: INSTALL State: SUCCEEDED
[Container] 2023/01/13 14:52:44 Phase context status code:  Message: 
[Container] 2023/01/13 14:52:44 Entering phase PRE_BUILD
[Container] 2023/01/13 14:52:44 Running command echo $DATETIME
2023-01-13-14-52-44

[Container] 2023/01/13 14:52:44 Running command chmod +x ./echo-datetime.sh

[Container] 2023/01/13 14:52:44 Phase complete: PRE_BUILD State: SUCCEEDED
[Container] 2023/01/13 14:52:44 Phase context status code:  Message: 
[Container] 2023/01/13 14:52:44 Entering phase BUILD
[Container] 2023/01/13 14:52:44 Running command ./echo-datetime.sh
2023-01-13-14-52-44

[Container] 2023/01/13 14:52:44 Phase complete: BUILD State: SUCCEEDED
[Container] 2023/01/13 14:52:44 Phase context status code:  Message: 
[Container] 2023/01/13 14:52:44 Entering phase POST_BUILD
[Container] 2023/01/13 14:52:44 Running command echo $DATETIME
2023-01-13-14-52-44

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM