Skip to content

Latest commit

 

History

History

task_005_trigger_codebuild_PR_events__eventbridge__lambda__dynamic_branches

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Trigger codebuild PR events using EventBridge (Lambda, Dynamic Branches)

Objective

  • The PR in codebuild get's raised always with the branch hardcoded in build project.
  • We want to trigger codebuild with PR branches which requires involvement of lambda function.

Reasearch Links and docs with relevant information

aws . codebuild . start-build

You can start codebuild using commands dynamically. You can use the same command in lambda function to trigger the codebuild project.

docs.aws.amazon.com » Monitoring CodeCommit events in Amazon EventBridge and Amazon CloudWatch Events

You can get the payload from the event and use it in the lambda function for testing.

stackoverflow.com » Creating Lambda Function to Trigger Codebuild Project using Nodejs

Trigger lambda using nodejs

docs.aws.amazon.com » Environment variables in build environments

You can use environment variables in the buildspec.yaml file to get the source branch and use it in the buildspec.yaml file

Create repo-a in Codecommit

Create repo-b in Codecommit

Create a codebuild project for repo-a - codebuild-repo-a

Create a codebuild project for repo-b - codebuild-repo-b

Create codepipeline for repo-a - codepipeline-repo-a

Create codepipeline for repo-b - codepipeline-repo-b

Create a lambda function to trigger respetive codebuilds

Copy the code lambda.py to the lambda function

Click on deploy to deploy the lambda function (your code get updated in the function)

Click on Test and it should ask you to create a sample event using which we can test the lambda function

Get the event from docs.aws.amazon.com » Monitoring CodeCommit events in Amazon EventBridge and Amazon CloudWatch Events and paste it in the event

Click on Test

You will get error

Add the permissions w.r.t to lambda_service_role (codebuild-dynamic-communicator-role-py375219) to trigger the codebuild project

		{
            "Effect": "Allow",
            "Action": "codebuild:StartBuild",
            "Resource": "arn:aws:codebuild:xx-region-y:xxxxxxxxxxxxxx:project/*"
        }

Create a rule for repo-a in EventBridge to trigger the lambda function

Select default event bus and create rule

Give the arn of codecommit repo-a as source with events as PR_STATUS_CHANGE

Select the target as lambda function

Create rule

Create PR for repo-a

Add a buildspec.yaml file in repo-a to know the latest commit changes and update the codebuild-project-a

$ gst
On branch feat_pr_from_repo_a
Changes not staged for commit:
        deleted:    repo_a__commit_4
Untracked files:
        repo_a__commit_5

Create the pull request

Check logs for lambda in cloudwatch when PR is created

Logs in codebuild-repo-a

Add commits to PR

$ gst
On branch feat_pr_from_repo_a
Changes not staged for commit:
        deleted:    repo_a__commit_5

Untracked files:
        repo_a__commit_6

# Push the changes as new commit

See the build is triggered against PR branch

Latest commit information is available in build logs

[Container] 2024/04/27 04:19:26.605628 Running command ls -ltrh
total 4.0K
-rw-r--r-- 1 root root   0 Apr 27 04:19 repo_a__commit_6
-rw-r--r-- 1 root root 273 Apr 27 04:19 buildspec.yaml

Note: The branch name is refs/heads/feat_pr_from_repo_a

Create a rule for repo-b in EventBridge to trigger the lambda function

Create PR for repo-b

$ git branch --show-current
feat_pr_from_repo_b_branch1

$ gst                      
On branch feat_pr_from_repo_b_branch1
Changes not staged for commit:
        deleted:    repo_a__test5
Untracked files:
        repo_a__test6
# commit the changes

Logs of codebuild-repo-b

[Container] 2024/04/27 04:38:28.453263 Running command ls -ltrh
total 4.0K
-rw-r--r-- 1 root root   0 Apr 27 04:38 repo_a__test6
-rw-r--r-- 1 root root 273 Apr 27 04:38 buildspec.yaml

Note: The branch name is refs/heads/feat_pr_from_repo_b_branch1 and has latest commit