'Append the package.json version number to my build artifact in aws-codebuild

I really dont know if this is a simple (must be), common or complex task.

I have a buildspec.yml file in my codebuild project, and i am trying to append the version written in package.json file to the output artifact.

I have already seen a lot of tutorials that teach how to append the date (not really useful to me), and others that tell me to execute a version.sh file with this echo $(sed -nr 's/^\s*"version": "([0-9]{1,}.[0-9]{1,}.*)",$/\1/p' package.json)

and set it in a variable (it doesn't work). i'm ending up with a build folder called: "my-project-$(version.sh)"

codebuild environment uses ubuntu and nodejs

Update (solved):
my version.sh file:

    #!/usr/bin/env bash
    echo $(sed -nr 's/^\s*\"version": "([0-9]{1,}\.[0-9]{1,}.*)",$/\1/p' package.json)

Then, i just found out 2 things:

  • Allow access to your version.sh file:

    git update-index --add --chmod=+x version.sh

  • Declare a variable in any phase in buildspec, i dit in in build phase (just to make sure repository is already copied in environment)

    TAGG=$($CODEBUILD_SRC_DIR/version.sh)

then reference it in artifact versioned name:

   artifacts:
     files:
       - '**/*'
     name: workover-frontend-$TAG   

As result, my build artifact's name: myproject-1.0.0



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source