{"id":1140,"date":"2019-05-25T16:52:44","date_gmt":"2019-05-25T13:52:44","guid":{"rendered":"https:\/\/artem.services\/?p=1130"},"modified":"2019-11-11T12:09:53","modified_gmt":"2019-11-11T09:09:53","slug":"aws-codebuild-d0-b4-d0-be-d0-b1-d0-b0-d0-d0-8f-d0-bc-b5-d0-d0-bd-b8-ba-d1-80-d0-b5-d0-bc-d0-b5-d0-bd-d1-82-d0-bd-d1-8b-d0-b9-d0-bd-d0-be-d0-bc-d0-b5-d1-80-d1-81-d0-b1-d0-be","status":"publish","type":"post","link":"https:\/\/artem.services\/?p=1140&lang=en","title":{"rendered":"AWS &#8212; CodeBuild: Add an incremental build number"},"content":{"rendered":"<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-214\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2018\/11\/AWS-Logo.png\" alt=\"\" width=\"975\" height=\"450\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2018\/11\/AWS-Logo.png 975w, https:\/\/artem.services\/wp-content\/uploads\/2018\/11\/AWS-Logo-300x138.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2018\/11\/AWS-Logo-768x354.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2018\/11\/AWS-Logo-954x440.png 954w\" sizes=\"(max-width: 975px) 100vw, 975px\" \/><\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">As it turned out, <strong>CodeBuild<\/strong> does not have a built-in incremental variable for the build number, as in <strong>Jenkins<\/strong>, for example.<\/span><\/span><\/p>\n<p><img loading=\"lazy\" class=\"aligncenter wp-image-1131 size-full\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Facepalm.jpg\" alt=\"\" width=\"640\" height=\"360\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Facepalm.jpg 640w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Facepalm-300x169.jpg 300w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">The solution was found on<\/span><\/span> <a href=\"https:\/\/medium.com\/faun\/build-numbers-for-aws-codebuild-5f91f41e5ebd\" target=\"_blank\" rel=\"noopener noreferrer\">medium.com<\/a><\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">To add the build number you will need the following services:<\/span><\/span><\/p>\n<ul>\n<li>AWS SSM (Systems Manager Parameter Store)<\/li>\n<li>AWS Lambda<\/li>\n<li>AWS CloudWatch<\/li>\n<li>AWS IAM<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h4>Create Parameter Store<\/h4>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">Go to the service<\/span><\/span> <strong>AWS Systems Manager<\/strong> <strong>&#8212;<\/strong>&gt; <strong>Parameter Store<\/strong><\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">Create a parameter named<\/span><\/span> &quot;<strong>\/build-number\/artem-test<\/strong>&quot;<\/p>\n<p><img loading=\"lazy\" class=\"size-full wp-image-1132 aligncenter\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.21.33-PM.png\" alt=\"\" width=\"1018\" height=\"890\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.21.33-PM.png 1018w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.21.33-PM-300x262.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.21.33-PM-768x671.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.21.33-PM-954x834.png 954w\" sizes=\"(max-width: 1018px) 100vw, 1018px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">Set the parameters:<\/span><\/span><\/p>\n<ul>\n<li>Type: String<\/li>\n<li>Value: 1<\/li>\n<\/ul>\n<p><img loading=\"lazy\" class=\"size-full wp-image-1133 aligncenter\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.21.54-PM.png\" alt=\"\" width=\"964\" height=\"606\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.21.54-PM.png 964w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.21.54-PM-300x189.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.21.54-PM-768x483.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.21.54-PM-954x600.png 954w\" sizes=\"(max-width: 964px) 100vw, 964px\" \/><\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">Save our<\/span><\/span> &quot;<strong>Parameter store<\/strong>&quot;<\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">Go to<\/span><\/span> <strong>AIM<\/strong> -&gt; <strong>Policy<\/strong><\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">Create a new Policy with the name &quot;<strong>codebuild-buildnumber-ssm<\/strong>&quot; for the &quot;<strong>CodeBild<\/strong>&quot; project with the following contents:<\/span><\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n    &quot;Version&quot;: &quot;2012-10-17&quot;,\r\n    &quot;Statement&quot;: [\r\n        {\r\n            &quot;Effect&quot;: &quot;Allow&quot;,\r\n            &quot;Action&quot;: [\r\n                &quot;ssm:GetParameter&quot;,\r\n                &quot;ssm:GetParameters&quot;\r\n            ],\r\n            &quot;Resource&quot;: &quot;arn:aws:ssm:us-east-1:XXXXXXXXXXXX:parameter\/build-number\/*&quot;\r\n        }\r\n    ]\r\n<\/pre>\n<p>&nbsp;<\/p>\n<blockquote><p>XXXXXXXXXXXX &#8212; r<span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">eplace with your AWS ID.<\/span> <span class=\"\" title=\"\">Also check your region.<\/span><\/span><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">Go to <strong>AIM<\/strong> -&gt; <strong>Roles<\/strong> and find the role of <strong>CodeBuild<\/strong> for our project and make it &quot;<strong>Attach<\/strong>&quot; created by the <strong>Policy<\/strong>.<\/span><\/span><\/p>\n<p><!--more--><\/p>\n<h4><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">Create a Lambda function<\/span><\/span><\/h4>\n<p>Go to <strong>Lambda<\/strong> -&gt; &quot;<strong>Create function<\/strong>&quot;<\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">Set the name &quot;<strong>update-codebuild-build-number<\/strong>&quot; and select &quot;<strong>Node.js 8.10<\/strong>&quot;<\/span><\/span><\/p>\n<p><img loading=\"lazy\" class=\"size-full wp-image-1134 aligncenter\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.30.44-PM.png\" alt=\"\" width=\"1918\" height=\"728\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.30.44-PM.png 1918w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.30.44-PM-300x114.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.30.44-PM-768x292.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.30.44-PM-1024x389.png 1024w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.30.44-PM-954x362.png 954w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.30.44-PM-1354x514.png 1354w\" sizes=\"(max-width: 1918px) 100vw, 1918px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">Insert the following code:<\/span><\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nconst AWS = require('aws-sdk');\r\n\r\nconst ssm = new AWS.SSM();\r\n\r\nexports.handler = async (event) =&amp;amp;gt; {\r\n    \r\n    const parameterName = '\/build-number\/' + event['detail']['project-name'];\r\n    \r\n    const getBuildNumberParams = {\r\n        Name: parameterName\r\n    };\r\n    \r\n    const getBuildNumberResponse = await ssm.getParameter(getBuildNumberParams).promise();\r\n    \r\n    const buildNumber = parseInt(getBuildNumberResponse.Parameter.Value);\r\n    \r\n    const setBuildNumberParams = {\r\n        Name: parameterName,\r\n        Type: 'String',\r\n        Value: (buildNumber + 1).toString(),\r\n        Overwrite: true\r\n    };\r\n    \r\n    const setBuildNumberResponse = await ssm.putParameter(setBuildNumberParams).promise();\r\n};\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">Save the changes.<\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p>Go to <strong>AIM<\/strong> -&gt; <strong>Policy<\/strong><\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">Create a new <strong>Policy<\/strong> with the name &quot;<strong>lambda-buildnumber-ssm<\/strong>&quot; for the &quot;<strong>Lambda<\/strong>&quot; project with the following contents:<\/span><\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n{\r\n    &quot;Version&quot;: &quot;2012-10-17&quot;,\r\n    &quot;Statement&quot;: [        \r\n        {\r\n            &quot;Effect&quot;: &quot;Allow&quot;,\r\n            &quot;Action&quot;: [\r\n                &quot;ssm:GetParameter&quot;,\r\n                &quot;ssm:GetParameters&quot;,\r\n                &quot;ssm:PutParameter&quot;\r\n            ],\r\n            &quot;Resource&quot;: &quot;arn:aws:ssm:us-east-1:XXXXXXXXXXXX:parameter\/build-number\/*&quot;\r\n\t\t}\r\n\t]\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<blockquote><p>XXXXXXXXXXXX &#8212; r<span class=\"tlid-translation translation\" lang=\"en\"><span title=\"\">eplace with your AWS ID.<\/span> <span class=\"\" title=\"\">Also check your region.<\/span><\/span><\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">Go to <strong>AIM<\/strong> -&gt; <strong>Roles<\/strong> and find the role of <strong>Lambda<\/strong> for our project and make it &quot;<strong>Attach<\/strong>&quot; created by the <strong>Policy<\/strong>.<\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<h4><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">Create a CloudWatch rule<\/span><\/span><\/h4>\n<p>Go to <strong>CloudWatch<\/strong> -&gt; &quot;<strong>Create rule<\/strong>&quot; -&gt; &quot;<strong>Event Pattern<\/strong>&quot;<\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">Specify service &#8212; <\/span><\/span><strong><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">CodeBuild<\/span><\/span><\/strong><\/p>\n<p>Set the <strong>Event Type<\/strong> -&gt; &quot;<strong>CodeBuild Build State Change<\/strong>&quot;<\/p>\n<p><img loading=\"lazy\" class=\"size-full wp-image-1135 aligncenter\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.38.51-PM.png\" alt=\"\" width=\"1092\" height=\"908\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.38.51-PM.png 1092w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.38.51-PM-300x249.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.38.51-PM-768x639.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.38.51-PM-1024x851.png 1024w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.38.51-PM-954x793.png 954w\" sizes=\"(max-width: 1092px) 100vw, 1092px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">Specify &quot;<strong>Specific state (s)<\/strong>&quot; and select the states &quot;<strong>SUCCESS<\/strong>&quot; and &quot;<strong>FAILED<\/strong>&quot;<\/span><\/span><\/p>\n<p><img loading=\"lazy\" class=\"size-full wp-image-1136 aligncenter\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.49.01-PM.png\" alt=\"\" width=\"1018\" height=\"808\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.49.01-PM.png 1018w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.49.01-PM-300x238.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.49.01-PM-768x610.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-3.49.01-PM-954x757.png 954w\" sizes=\"(max-width: 1018px) 100vw, 1018px\" \/><\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span title=\"\">Go to &quot;<strong>Targets<\/strong>&quot; and select in the &quot;<strong>Function<\/strong>&quot; &quot;<strong>update-codebuild-build-number<\/strong>&quot;<\/span><\/span><\/p>\n<p><span class=\"\" title=\"\">Confirm the &quot;<strong>Create rule<\/strong>&quot;, set the name &quot;<strong>update-codebuild-build-number<\/strong>&quot; and save.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h4>CodeBuild<\/h4>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">Editing<\/span><\/span> &quot;<strong>buildspec.yml<\/strong>&quot;<\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">Add a variable to the &quot;<strong>parameter-store<\/strong>&quot; block to store the build number.<\/span><\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nenv:\r\n  parameter-store:\r\n     BUILD_NUMBER: &quot;\/build-number\/artem-test&quot;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">Now in assemblies you can use the variable &quot;<strong>BUILD_NUMBER<\/strong>&quot; which will be incremented.<\/span><\/span><\/p>\n<p><img loading=\"lazy\" class=\"size-full wp-image-1137 aligncenter\" src=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-4.44.41-PM.png\" alt=\"\" width=\"1868\" height=\"744\" srcset=\"https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-4.44.41-PM.png 1868w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-4.44.41-PM-300x119.png 300w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-4.44.41-PM-768x306.png 768w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-4.44.41-PM-1024x408.png 1024w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-4.44.41-PM-954x380.png 954w, https:\/\/artem.services\/wp-content\/uploads\/2019\/05\/Screen-Shot-2019-05-25-at-4.44.41-PM-1354x539.png 1354w\" sizes=\"(max-width: 1868px) 100vw, 1868px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">An example of a simple work with <strong>CodeCommit<\/strong> and <strong>Codebuild<\/strong> can be found in the previous <a href=\"https:\/\/artem.services\/?p=1127&amp;lang=en\" target=\"_blank\" rel=\"noopener noreferrer\">post<\/a>.<\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As it turned out, CodeBuild does not have a built-in incremental variable for the build number, as in Jenkins, for example. The solution was found on medium.com To add the build number you will need the following services: AWS SSM (Systems Manager Parameter Store) AWS Lambda AWS CloudWatch AWS IAM &nbsp; Create Parameter Store Go &hellip; <a href=\"https:\/\/artem.services\/?p=1140&#038;lang=en\" class=\"more-link\">\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u044c \u0447\u0438\u0442\u0430\u0442\u044c<span class=\"screen-reader-text\"> &quot;AWS &#8212; CodeBuild: Add an incremental build number&quot;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[599,613],"tags":[543,881,873,899,875,651,885,883],"_links":{"self":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1140"}],"collection":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1140"}],"version-history":[{"count":4,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1140\/revisions"}],"predecessor-version":[{"id":1458,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/1140\/revisions\/1458"}],"wp:attachment":[{"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}