continuous-integrationyamldevopsaws-codebuildbuildspec

How to set a custom Build Version in AWS CodeBuild


I am trying to set an environmental variable in a format of YYYY.MM.DD.BuildId for versioning binaries, but I cannot seem to figure out how to do this. I need it available from the buildspec.yml file to reference in the compiler commands. It doesn't seem like it should be this difficult, but I cannot seem to update the environmental variables with any kind of dynamic logic.

I've tried variations on this theme in the yaml file:

env:
  variables:
     BUILD_VERSION: "$(date +%Y.%m.%d).$(CODEBUILD_BUILD_NUMBER)"

...but it always treats the build version as a literal value. No substitution takes place.

I can't be the only person that's tried to do this sort of thing. How can I make this work? This is stupidly easy in Azure Devops, and trivial in Jenkins as well, so it's surprising how long I've been at this.


Solution

  • I discovered that in the build step, I could use the command shell to set the value I wanted for my compiler. For example:

      build:
        commands:
          - export BUILD_VERSION=$(date +%Y.%m.%d).$CODEBUILD_BUILD_NUMBER
          - echo Building version [$BUILD_VERSION]...
          - dotnet publish My.Project/My.Project.csproj --configuration Release --framework netcoreapp3.1 --output ./application --self-contained --runtime ubuntu.18.04-x64 /p:AssemblyVersion=$BUILD_VERSION /p:AssemblyFileVersion=$BUILD_VERSION /p:FileVersion=$BUILD_VERSION
    

    This allowed my binaries to be built with the proper file and build versions.