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

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

... build commands ...


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.

    - '**/*'
  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

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

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

Environment variables in build environments, AWS CodeBuild provides several environment variables that you can use in your build commands: Set to 0 if the build is failing, or 1 if the build is succeeding. We recommend you store sensitive values in the Amazon EC2 Systems Manager To retrieve them, see the parameter-store mapping in Buildspec syntax. I know I can set environment variables in CodeBuild, but I want to use the same CodeBuild project for dev, qa, and prod environments. I don't see how I can pass an environment variable from CodePipeline that makes it all the way to the buildspec.yml. Example buildspec.yml

According to You can try $(Get-Date -UFormat "%Y%m%d-%H%M%S") in PowerShell.

AWS CodePipeline Enables Setting Environment Variables on AWS , You can now set environment variables for CodeBuild build jobs in your variables defined in the build project configuration or buildspec file. Run the AWS CLI create-project or update-project command, setting the buildspec value to the path to the alternate buildspec file relative to the value of the built-in environment variable CODEBUILD_SRC_DIR.

Does the following work for you?

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

Run a build in AWS CodeBuild, For Amazon S3, for the optional Source version value, enter the version ID for the By default, CodeBuild looks for a file named buildspec.yml in the source code Choose Add environment variable to add a new environment variable for this  Stack Exchange network consists of 176 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

How to pass environment variable to the buildspec.yml for AWS , When you create a codebuild you can pass environment variables. "value": "​Amazon-ECR-repo-name". }, The in your buildspec.yml you can refer them like regular environment variables with $IMAGE_REPO_NAME. version: 0.2. phases: Setting credentials for https git clone in AWS CodeBuild. Environment variables can be displayed in plain text using tools such as the CodeBuild console and the AWS CLI. We recommend you store sensitive values in the Amazon EC2 Systems Manager Parameter Store and then retrieve them from your buildspec.

Using environment variables in AWS CodeBuild buildspec.yml, Using environment variables in AWS CodeBuild buildspec.yml Reference environment variable using $ syntax - mvn versions:set  @payne911 honestly just save yourself some hassle and build out a regular bash script as a separate file and call it from buildspec.yml. Example if you put all your bash code into a file in your repo under /aws/ then chmod a+x before you check it in, then all you have to do is: pre_build: commands: - cp aws/* /usr/local/bin/

Use dynamic values for parameter-store in aws buildspec.yml for , Use dynamic values for parameter-store in aws buildspec.yml for For my CodeBuild projects, I have a dev, staging and production Each project has an environment variable called $BUILD_ENVIRONMENT set to dev,  For the build specification, we are going to write a custom buildspec.yml file. CodeBuild will collect your output files and store them in S3, Amazon’s file storage service.

  • I saw that example, but it doesn't include time, only date, and I'm having trouble understanding where the value is coming from. I tried expanding on what was listed by including %h%m%s, but that doesn't work, so I was attempting to use the powershell format which I am more familiar with. When I include %H%M%S, the date expression doesn't seem to evaluate at all and is just omitted. I'm expecting to have artifacts generated several times per day, so just the date shown does not work for us.
  • if by value you mean the $(date +%Y-%m-%d) - that's Shell Command Language that they support. is this really not working? ` name: myname-$(date +%Y-%m-%d-%H-%M-%S)` and how exactly is it not working?...what's the error
  • it might not work in the variables section, have you tried the artifacts section?...could you possibly post the example you have which is not working with extending the date with %H ?
  • Sure, I updated with my original artifacts section and corresponding error.
  • I missed the fact that it's PowerShell. I think it adhereas to .Net DateTime format then. And as far as I can see the capital S is what breaks the things here...… Try using "date +%Y%m%d-%H%M%s"
  • Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline.