AWS – CodeBuild: Add an incremental build number

As it turned out, CodeBuild does not have a built-in incremental variable for the build number, as in Jenkins, for example.

The solution was found on medium.com

To add the build number you will need the following services:

  • AWS SSM (Systems Manager Parameter Store)
  • AWS Lambda
  • AWS CloudWatch
  • AWS IAM

 

Create Parameter Store

Go to the service AWS Systems Manager > Parameter Store

Create a parameter named/build-number/artem-test

 

Set the parameters:

  • Type: String
  • Value: 1

Save ourParameter store

Go to AIM -> Policy

Create a new Policy with the name “codebuild-buildnumber-ssm” for the “CodeBild” project with the following contents:

    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ssm:GetParameter",
                "ssm:GetParameters"
            ],
            "Resource": "arn:aws:ssm:us-east-1:XXXXXXXXXXXX:parameter/build-number/*"
        }
    ]

 

XXXXXXXXXXXX – replace with your AWS ID. Also check your region.

 

Go to AIM -> Roles and find the role of CodeBuild for our project and make it “Attach” created by the Policy.

Create a Lambda function

Go to Lambda -> “Create function

Set the name “update-codebuild-build-number” and select “Node.js 8.10

 

Insert the following code:

const AWS = require('aws-sdk');

const ssm = new AWS.SSM();

exports.handler = async (event) => {
    
    const parameterName = '/build-number/' + event['detail']['project-name'];
    
    const getBuildNumberParams = {
        Name: parameterName
    };
    
    const getBuildNumberResponse = await ssm.getParameter(getBuildNumberParams).promise();
    
    const buildNumber = parseInt(getBuildNumberResponse.Parameter.Value);
    
    const setBuildNumberParams = {
        Name: parameterName,
        Type: 'String',
        Value: (buildNumber + 1).toString(),
        Overwrite: true
    };
    
    const setBuildNumberResponse = await ssm.putParameter(setBuildNumberParams).promise();
};

 

Save the changes.

 

Go to AIM -> Policy

Create a new Policy with the name “lambda-buildnumber-ssm” for the “Lambda” project with the following contents:

{
    "Version": "2012-10-17",
    "Statement": [        
        {
            "Effect": "Allow",
            "Action": [
                "ssm:GetParameter",
                "ssm:GetParameters",
                "ssm:PutParameter"
            ],
            "Resource": "arn:aws:ssm:us-east-1:XXXXXXXXXXXX:parameter/build-number/*"
		}
	]
}

 

XXXXXXXXXXXX – replace with your AWS ID. Also check your region.

 

Go to AIM -> Roles and find the role of Lambda for our project and make it “Attach” created by the Policy.

 

Create a CloudWatch rule

Go to CloudWatch -> “Create rule” -> “Event Pattern

Specify service – CodeBuild

Set the Event Type -> “CodeBuild Build State Change

 

Specify “Specific state (s)” and select the states “SUCCESS” and “FAILED

Go to “Targets” and select in the “Function” “update-codebuild-build-number

Confirm the “Create rule“, set the name “update-codebuild-build-number” and save.

 

CodeBuild

Editingbuildspec.yml

Add a variable to the “parameter-store” block to store the build number.

env:
  parameter-store:
     BUILD_NUMBER: "/build-number/artem-test"

 

Now in assemblies you can use the variable “BUILD_NUMBER” which will be incremented.

 

An example of a simple work with CodeCommit and Codebuild can be found in the previous post.

Tagged: Tags

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments