Custom version label with aws code pipeline
Asked Answered
R

3

12

I'm using AWS CodePipeline with CodeBuild to build and deploy my application to ElasticBeanstalk.

CodePipeline generates version names like this: code-pipeline-1122334455667-MyApp-1ac31f7c-1343-471x-a7e8-46b24f1785a

Is it possible to customize these labels?

Rachele answered 12/11, 2018 at 14:57 Comment(0)
I
10

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.
Illusionist answered 6/2, 2020 at 2:33 Comment(1)
This worked out great for me! Went from having a CodePipeline deployment process to just using CodeBuild to send versions to Elastic Beanstalk. CodePipeline would even create different version labels for each environment even though they were all deployed with the same pipeline which made it almost impossible to determine what was actually deployed to each environment.Cassation
O
0

At the moment I believe that it's not possible and it's a real nuisance not being able to properly version our deployments.

It seems that AWS CodePipeline is not quite yet ready for prime time in terms of traceability and so on, which can also be seen by the impossibility of having custom named artifacts when CodeBuild stages are included in the pipeline.

Amazon really needs to step up its game on CI/CD stuff.

Olivia answered 25/1, 2019 at 12:36 Comment(0)
S
0

As already mentioned, it is basically not possible to update the version-label in Elastic Beanstalk.

But we found a solution which's fine for us. We created a separate stage in the CodePipeline with a Lambda action. The user parameter is #{source_variables.CommitMessage} and the Input artifact is the build_output. In the lambda function the incoming event is getting read out for the commit message

def lambda_handler(event, context):
    commit_message = event['CodePipeline.job']['data']['actionConfiguration']['configuration']['UserParameters']

In the next step we read out the version from the package.json from build_output on s3 bucket

# Read out data from package.json on s3 zip object
with tempfile.TemporaryFile() as f:
    s3_resource.meta.client.download_fileobj(s3_bucket_name, s3_object_key, f)
    archive = zipfile.ZipFile(f)
    package_json = archive.open('package.json')
    data = package_json.read() 
    json_data = json.loads(data)
    application_version = json_data['version']

Now the function can update the application version description from the elastic beanstalk environment

# Read out version-label from environemt 
environment = eb_client.describe_environments(ApplicationName=application_name)
version_label = environment['Environments'][0]['VersionLabel']
description = 'Version: {} - Commit Message: {}'.format(application_version, commit_message)

# Write new Description to given elastic Beanstalk Application version 
eb_response = eb_client.update_application_version(
ApplicationName=application_name,
VersionLabel=version_label,
Description=description
)

Now we can reference our application versions to version and commits. Please keep in mind those are just code snippets and not the whole logic.

Steepen answered 3/8, 2021 at 7:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.