Run a Build in AWS CodeBuild When a Git Tag is Pushed

Run a Build in AWS CodeBuild When a Git Tag is Pushed

AWS CodeBuild has nice integration with different Git repository hosting service providers(like GitHub, BitBucket, even amazon’s own CodeCommit etc). Even using WebHook is pretty easy. You can start a build in CodeBuild for every push, pull, PR created, PR merged etc. But it can be bit tricky when it comes to trigger for every tag push only, as it us not a default event type provided by CodeBuild. You need to add some customized settings to the filter section of the Source for it. Here is how I implemented it step by step.

Enable re-build on code change

First you need to enable Rebuild every time a code change is pushed to this repository from the source settings.

Rebuild source

Change event type

You need to change the Event Type to push to trigger the CodeBuild.

push event

Track branch

I am assuming you want to track tags from any branch. So I have put * on branch settings:


Add filter

Finally, I have added the filter in Start a build under this condition. The filter is ^refs/tags/.* under HEAD_REF.


Use tag in buildspec.yml file

If you want to use the Tag number in buildspec.yml, then use this:

- TAG_NUMBER="$(git describe --tags --abbrev=0)"

But above code won’t work if you are tracking a build which is based on a commit that is not associated a tag or a build that is not triggered by a tag push. To resolve this, you need to increase depth value. You can set it to full but it will increase build time depending of the size of the repository. It is in the Additional configuration section.

Full depth

In conclusion

IMHO, CodeBuild is a fantastic tool for implementing CI/CD, but documentation lacks some minor details. It might give you subtle hints but you need to dig deeper to find the exact solution. Let us talk more in the comment section if you think this article is missing something. Cheers!!

Last updated: July 2, 2020

Share Your Thoughts
M↓ Markdown