Pass CommitId of a CodeCommit tag to CodeBuild project

0

My requirement is to build a CodeCommit repository when a tag is pushed to origin.

I was able to do this using a CloudWatch event which triggers a Lambda function. Following is the resulting event,

{
	"version": "0",
	"id": "5cf07036-282a-a925-8007-4422b1377dbb",
	"detail-type": "CodeCommit Repository State Change",
	"source": "aws.codecommit",
	"account": "369837395262",
	"time": "2019-05-13T23:44:14Z",
	"region": "ap-southeast-2",
	"resources": ["arn:aws:codecommit:ap-southeast-2:XXX"],
	"detail": {
		"callerUserArn": "arn:aws:iam::XXX",
		"commitId": "3ea7c9822fc80813aeac50e46ab439f4bb26468d",
		"event": "referenceCreated",
		"referenceFullName": "refs/tags/temp-tag-1",
		"referenceName": "temp-tag-1",
		"referenceType": "tag",
		"repositoryId": "XXX",
		"repositoryName": "xxx"
	}
}

The "temp-tag-1" is created using "git tag temp-tag-1 -m 6a2359d64291a6490412a46de8d6566602e8f736" command. (This is not the latest commit id)

Is there a way to pass the commit id 6a2359d64291a6490412a46de8d6566602e8f736 rather than 3ea7c9822fc80813aeac50e46ab439f4bb26468d to the lambda function?

If possible I can pass that commit id as the "sourceVersion" to the code build project using lambda function (boto3).

Edited by: mumbo on May 13, 2019 11:19 PM

mumbo
preguntada hace 5 años1112 visualizaciones
3 Respuestas
1

Hi mumbo,

Glad you could find a solution for your issue. Right now CodeCommit does not emit events for tag creation, so as you have already figured out, the right way to get what you want, given you know the tag name, is to just call CodeCommit to get the commit id using that tag reference.

respondido hace 5 años
0

I think I found a way to do it.

Instead of trying to get the correct id from the CloudWatch event, I've used boto3 codecommit client in my Lambda function.

  botoCodeCommitClient = boto3.client('codecommit')
  botoCodeCommitClient.get_commit(repositoryName=repository, commitId=tagCommitId)

The result contains the correct commit id.

  "commit": {
	"commitId": "6a2359d64291a6490412a46de8d6566602e8f736",
	"treeId": "72e8659841587e1afa59a775bafe9d6a5c8a8f5d",
	"parents": ["8ccc13c43416850ac16105e789fc8db5911db11e"],
	"message": "xxx",
	"author": {
		"name": "xxx",
		"email": "xxx,
		"date": "1557711827 +0000"
}

Edited by: mumbo on May 14, 2019 1:10 AM

mumbo
respondido hace 5 años
0

Hi robison,

I am planning to add the branch name to the artifact path on S3. Eg. /tags/<branchname>/<tagname>
Is there a way in boto3 to get the branch name of a given commit id?

Thanks

mumbo
respondido hace 5 años

No has iniciado sesión. Iniciar sesión para publicar una respuesta.

Una buena respuesta responde claramente a la pregunta, proporciona comentarios constructivos y fomenta el crecimiento profesional en la persona que hace la pregunta.

Pautas para responder preguntas