Custom version label with aws code pipeline

You can set a version label if you use an AWS CodeBuild action provider instead of an AWS ElasticBeanstalk deploy action provider.

CodeBuild has the ability to run AWS CLI commands in the buildspec, which you can use to

  1. upload your build artifact to S3 (documentation)
  2. create a version in Elastic Beanstalk (documentation)
  3. deploy the version (documentation)

Below is an example buildspec uploading an artifact with a custom label, file name, and description.

version: 0.2

phases:
  build:
    commands:
      - mvn clean package
      - export POM_VERSION=$(mvn -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec)
      - export JAR_NAME='application-'$POM_VERSION'.jar'
      - export EB_VERSION=$POM_VERSION'-'$(date +%s)
      - aws s3 cp target/application.jar s3://bucket-name/$JAR_NAME
      - aws elasticbeanstalk create-application-version --application-name "Application Name" --version-label "$EB_VERSION" --description "$CommitMessage" --source-bundle S3Bucket=bucket-name,S3Key=$JAR_NAME
      - aws elasticbeanstalk update-environment --application-name "Application Name" --version-label "$EB_VERSION" --environment-name "EnvironmentName"

Notes:

  • $CommitMessage is coming in from CodePipeline as an environment variable.
  • The date is appended to the version name to avoid naming collisions.