skip to Main Content

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. I want to be able to get something like this when i echo out. 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'

2

Answers


  1. The only way I know of to make it work, is by saving timestamp in a tmp file. 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)
    
    Login or Signup to reply.
  2. Try setting the variable during the install phase.

    echo-datetime.sh

    #!/bin/bash
    
    echo "$DATETIME"
    

    buildspec.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

    [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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search