Trigger AWS CodeBuild When a Git Tag is Pushed
Apr 16, 2020 · 2 Min Read · 17 Likes · 5 Comments![Trigger AWS CodeBuild When a Git Tag is Pushed](/content/images/2020/04/amazon_hu504c49aeaa757f7178504d64840503fc_2321021_220x0_resize_q75_lanczos.jpg 220w,/content/images/2020/04/amazon_hu504c49aeaa757f7178504d64840503fc_2321021_440x0_resize_q75_lanczos.jpg 440w,/content/images/2020/04/amazon_hu504c49aeaa757f7178504d64840503fc_2321021_500x0_resize_q75_lanczos.jpg 500w,/content/images/2020/04/amazon_hu504c49aeaa757f7178504d64840503fc_2321021_600x0_resize_q75_lanczos.jpg 600w,/content/images/2020/04/amazon_hu504c49aeaa757f7178504d64840503fc_2321021_640x0_resize_q75_lanczos.jpg 640w,,/content/images/2020/04/amazon_hu504c49aeaa757f7178504d64840503fc_2321021_720x0_resize_q75_lanczos.jpg 720w,,/content/images/2020/04/amazon_hu504c49aeaa757f7178504d64840503fc_2321021_1024x0_resize_q75_lanczos.jpg 640w)
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](/content/images/2020/04/aws_pr_hud08f6d839b3d4ed3c2dfb9168894bb7c_41330_500x0_resize_q75_lanczos.jpg)
Change event type
You need to change the Event Type to push to trigger the CodeBuild.
![push event](/content/images/2020/04/aws_pr_event_hu38d2e39975c4b671905037625c687851_23040_500x0_resize_q75_lanczos.jpg)
Track branch
I am assuming you want to track tags from any branch. So I have put *
on branch settings:
![TRACK BRANCH](/content/images/2020/04/aws_branch_hu140e652c1882040f1a1fbfb57f7bf566_8980_500x0_resize_q75_lanczos.jpg)
Add filter
Finally, I have added the filter in Start a build under this condition. The filter is ^refs/tags/.*
under HEAD_REF
.
![HEAD REF](/content/images/2020/04/aws_head_ref_hube06af43839bae8083dfcd20f14eba79_13291_500x0_resize_q75_lanczos.jpg)
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](/content/images/2020/04/aws_clone_depth_hua287e01877b39c17e11367a48e3c747b_9797_500x0_resize_q75_lanczos.jpg)
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: Jul 13, 2024
I won't spam you. Unsubscribe at any time.