<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mark Ingram</title>
    <description>The latest articles on DEV Community by Mark Ingram (@markdingram).</description>
    <link>https://dev.to/markdingram</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F427082%2Fce44a711-e7a0-421c-b938-2182e828c3ed.jpeg</url>
      <title>DEV Community: Mark Ingram</title>
      <link>https://dev.to/markdingram</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/markdingram"/>
    <language>en</language>
    <item>
      <title>Basic CDK deployment pipeline with GitHub Actions</title>
      <dc:creator>Mark Ingram</dc:creator>
      <pubDate>Wed, 08 Jul 2020 15:50:00 +0000</pubDate>
      <link>https://dev.to/markdingram/basic-cdk-deployment-pipeline-with-github-actions-13pm</link>
      <guid>https://dev.to/markdingram/basic-cdk-deployment-pipeline-with-github-actions-13pm</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;At the time of writing &lt;a href="https://github.com/pricing"&gt;Github Actions&lt;/a&gt; has a free tier allowing 2,000 minutes a month of builds for private repos. For smaller / side projects that are already hosted on GitHub it is straightforward to setup a PR based workflow to automate the deployment process.&lt;/p&gt;

&lt;p&gt;A CI/CD process suitable for a small project could be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;PR workflow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;on every pull request commit targeting main branch:

&lt;ul&gt;
&lt;li&gt;run all tests&lt;/li&gt;
&lt;li&gt;run &lt;code&gt;cdk synth&lt;/code&gt; and to produce a Cloud Assembly directory (cdk.out) with the latest changes&lt;/li&gt;
&lt;li&gt;run &lt;code&gt;cdk diff&lt;/code&gt; with the produced Cloud Assembly&lt;/li&gt;
&lt;li&gt;post diff as a comment to the PR&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Main workflow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;on every commit to main branch:

&lt;ul&gt;
&lt;li&gt;acquire cdk.out artifact from the PR workflow synth&lt;/li&gt;
&lt;li&gt;run &lt;code&gt;cdk apply&lt;/code&gt; with that Cloud Assembly&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Currently Github actions don't make it too easy to share artifacts between different workflows (see &lt;a href="https://github.com/actions/download-artifact/issues/3"&gt;https://github.com/actions/download-artifact/issues/3&lt;/a&gt;). It is now possible via wrangling the APIs but until it gets polished we'll amend the main workflow as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Main workflow:

&lt;ul&gt;
&lt;li&gt;on every commit to main branch:

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;run &lt;code&gt;cdk synth&lt;/code&gt; and to produce a Cloud Assembly directory (cdk.out) with the latest changes&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;run &lt;code&gt;cdk apply&lt;/code&gt; with the produced Cloud Assembly&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  PR Workflow
&lt;/h1&gt;

&lt;p&gt;Using the same repo as &lt;a href="//./2020-06-30-bazel-clojure.md"&gt;previously&lt;/a&gt; introduced &lt;a href="https://github.com/markdingram/bazel-cdk-clojure"&gt;https://github.com/markdingram/bazel-cdk-clojure&lt;/a&gt;, a &lt;a href="https://github.com/markdingram/bazel-cdk-clojure/blob/fc3d5b4259fe825e945227e70a6aa98f0fdbdd40/.github/workflows/pr.yml"&gt;pr.yml workflow&lt;/a&gt; is added.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Aside: I lost count of the number of broken links I saw from not using permanent Github links - to get a permalink simply press &lt;code&gt;y&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;While the generation of the cdk.out directory uses Bazel, the diff / apply of that product is done without reference to Bazel using an NPM installed CDK binary. Simplifying down this example for a pure NPM Javascript project would be straightforward.&lt;/p&gt;

&lt;p&gt;The workflow shouldn't be complicated to follow, in the &lt;code&gt;build&lt;/code&gt; job the steps of interest are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- name: Test
  run: |
    bazel test //...

- name: Synth
  run: |
    bazel build infra:synth
    find dist/bin/infra/cdk.out -type d -exec chmod 0755 {} \;

- name: Upload Cloud Assembly
  uses: actions/upload-artifact@v1
  with:
    name: cdk.out
    path: dist/bin/infra/cdk.out
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
`&lt;/p&gt;

&lt;p&gt;As required run all tests first. A Bazel synth rule has been added to output the Cloud Assembly &lt;code&gt;dist/bin/infra/cdk.out&lt;/code&gt; via &lt;code&gt;bazel build infra:synth&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;As of now CDK likes to add '.cache' directories inside the cdk.out directory when uploading assets to S3. Given all Bazel output is read only make the directories writable prior to uploading as an artifact.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;diff&lt;/code&gt; job the steps of note are:&lt;/p&gt;

&lt;p&gt;`&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- name: Download Cloud Assembly
  uses: actions/download-artifact@v1
  with:
    name: cdk.out

- name: Run CDK diff
  run: node_modules/.bin/cdk diff -c aws-cdk:enableDiffNoFail=true --no-color --app cdk.out "*" 2&amp;gt;&amp;amp;1 | tee cdk.log

- name: Add comment to PR
  env:
    URL: ${{ github.event.pull_request.comments_url }}
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  run: |
    jq --raw-input --slurp '{body: .}' cdk.log &amp;gt; cdk.json
    curl \
      -H "Content-Type: application/json" \
      -H "Authorization: token $GITHUB_TOKEN" \
      -d @cdk.json \
      -X POST \
      $URL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
`&lt;/p&gt;

&lt;p&gt;JQ is used to slurp the raw text output of CDK diff into a JSON message suitable for uploading to Github. Presumably there is a Github action that would encapsulate the PR comment posting, but the Rest API is so straightforward adding a dependency for that seems overkill.&lt;/p&gt;

&lt;p&gt;With this workflow in place a CDK diff should spring onto the PR after a few minutes:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8q5-1zcx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0bp0qcz24e592y2mbqfc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8q5-1zcx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0bp0qcz24e592y2mbqfc.png" alt="cdk_diff.png" width="748" height="944"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is also useful to setup a Github Branch protection rule that ensures the PR workflow is successful prior to merge:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DIlSJYzn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/asal9xhxdz40nuv548uo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DIlSJYzn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/asal9xhxdz40nuv548uo.png" alt="gh_branch_protection.png" width="721" height="713"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The main workflow is similar to the PR one, but with a &lt;code&gt;cdk deploy&lt;/code&gt; step instead.&lt;/p&gt;

&lt;h1&gt;
  
  
  Notes
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;I first encountered showing planned changes on Pull Requests from &lt;a href="https://www.runatlantis.io"&gt;Atlantis&lt;/a&gt; a couple of years ago. I believe Hashicorp hired the maintainer from that to work on &lt;a href="https://www.terraform.io/docs/cloud/index.html"&gt;Terraform cloud&lt;/a&gt;, so worth checking both of these for inspiration! &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The Amazon equivalent &lt;a href="https://aws.amazon.com/codebuild/pricing"&gt;AWS CodeBuild&lt;/a&gt; free tier is only a miserly 100 minutes. After the free tiers run out the AWS lowest tier of build instances (general1.small) is cheaper than GitHub ($0.005/minute for a general1.small vs $0.008), but has less RAM (3GB compared to 7GB). For larger projects I'd be inclined to pursue AWS (configured via CDK of course!). The CodeBuild / CodePipeline constructs &amp;amp; further integration over time with CDK / CloudFormation will likely allow more detailed/involved workflows to be constructed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There is a very detailed RFC with some changes coming to CDK to help with Pipelines:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://github.com/aws/aws-cdk-rfcs/blob/master/text/0049-continuous-delivery.md"&gt;https://github.com/aws/aws-cdk-rfcs/blob/master/text/0049-continuous-delivery.md&lt;/a&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>github</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
