SWC
SWC

Reputation: 141

AWS CodeBuild - How to assign dynamic variable values in buildspec.yml?

I want to name the artifact produced by my CodeBuild according to the time it was produced.

I've tried the following, but when the artifact gets uploaded to S3, the name is literally the expression, rather than the evaluated value of the expression that I've set.

Is is possible to set the env:variables to dynamic values, and if so, how? If not, is there a better way to do what I'm attempting here?

version: 0.2

env:
  variables:
    ARTIFACT_NAME: '[string]::Format("v{0}.zip", (Get-Date).ToString("yyyyMMdd-HHmmss"))'

... build commands ...

artifacts:
  Name:$Env:ARTIFACT_NAME

Update: I adjusted the artifact section to the following and omitted the variable altogether, per suggestion below, but continue to get an error when doing so.

artifacts:
  files:
    - '**/*'
  discard-paths: no
  base-directory: publish/release
  name: version-$(date +%Y%m%d-%H%M%S).zip

Results in error:

[Container] 2018/08/10 15:59:26 Updating artifact name as  Get-Date : Cannot bind parameter 'Date'. 
Cannot convert value "+%Y%m%d-%H%M%S" to type "System.DateTime". 
Error: "String was not recognized as a valid DateTime."
At C:\codebuild\tmp\script.ps1:5 char:21
+ echo version-$(date +%Y%m%d-%H%M%S).zip
+                     ~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommand

version-.zip

Upvotes: 3

Views: 6351

Answers (3)

Yoshi
Yoshi

Reputation: 29

Does the following work for you?

name: myname-`date '+%y%m%d-%H%M%S'`

Upvotes: 0

Linghao Zhu
Linghao Zhu

Reputation: 736

According to https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-date?view=powershell-6 You can try $(Get-Date -UFormat "%Y%m%d-%H%M%S") in PowerShell.

Upvotes: 1

Nune Isabekyan
Nune Isabekyan

Reputation: 549

As per docs you can achieve that with the following syntax:

version: 0.2         
phases:
  build:
    commands:
     - rspec HelloWorld_spec.rb
artifacts:
  files:
   - '**/*'
  name: myname-$(date +%Y-%m-%d) ```

Upvotes: 2

Related Questions