Know How Guide and Hands on Guide for AWS
myCodePipelineInvokeFunction
Lambda functionNote: the Lambda execution role need permission of codepipeline:PutJobSuccessResult
var AWS = require('aws-sdk');
exports.handler = function(event, context) {
var codepipeline = new AWS.CodePipeline();
// Retrieve the Job ID from the Lambda action
var jobId = event["CodePipeline.job"].id;
// Retrieve the value of UserParameters from the Lambda action configuration in AWS CodePipeline,
// in this case it is the Commit ID of the latest change of the pipeline.
var commitId = event["CodePipeline.job"].data.actionConfiguration.configuration.UserParameters;
// The region from where the lambda function is being executed.
var lambdaRegion = process.env.AWS_REGION;
// Notify AWS CodePipeline of a successful job
var putJobSuccess = function(message) {
var successInput = {
jobId: jobId,
outputVariables: {
testRunId: Math.floor(Math.random() * 1000).toString(),
dateTime: Date(Date.now()).toString(),
region: lambdaRegion
}
};
codepipeline.putJobSuccessResult(successInput, function(err, data) {
if(err) {
context.fail(err);
} else {
context.succeed(message);
}
});
};
// Notify AWS CodePipeline of a failed job
var putJobFailure = function(message) {
var failureInput = {
jobId: jobId,
failureDetails: {
message: JSON.stringify(message),
type: 'JobFailed',
externalExecutionId: context.invokeid
}
};
codepipeline.putJobFailureResult(failureInput, function(err, data) {
context.fail(message);
});
};
var sendResult = function() {
try {
console.log("Testing commit - " + commitId);
// Your tests here
// Succeed the job
putJobSuccess("Tests passed.");
} catch (ex) {
// If any of the assertions failed then fail the job
putJobFailure(ex);
}
};
sendResult();
};
Reuse the two-stage-pipeline
pipeline define in two-stage-pipeline quickstart
two-stage-pipeline
pipeline. Enter a name for the stage: Lambda-Testing
myCodePipelineInvokeFunction
#{SourceVariables.CommitId}
Before deploy to production, we need manual approval step. After Lambda-Testing, add Stage Approval
https://#{TestVariables.region}.console.aws.amazon.com/codesuite/codecommit/repositories/MyDemoRepo/commit/#{SourceVariables.CommitId}
Make sure to review the code before approving this action. Test Run ID: #{TestVariables.testRunId}
Create a state machine using the HelloWorld
sample template
Add a stage after the Lambda-Testing
Stage before Approval
Stage in the two-stage-pipeline
pipeline. Enter a name for the stage: StepFunction-Testing
stateMachine:HelloWorld
codepipline_
{"IsHelloWorldExample": true}
codepipline_
.