<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Manvendra P Singh on Medium]]></title>
        <description><![CDATA[Stories by Manvendra P Singh on Medium]]></description>
        <link>https://medium.com/@manvendrapsingh?source=rss-17126b6e03------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*3usZ4hzJGwRhR4nEMjz0pg@2x.jpeg</url>
            <title>Stories by Manvendra P Singh on Medium</title>
            <link>https://medium.com/@manvendrapsingh?source=rss-17126b6e03------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 19:15:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@manvendrapsingh/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Maven (0) - Preface]]></title>
            <link>https://medium.com/@manvendrapsingh/maven-0-preface-bf2c3bcec122?source=rss-17126b6e03------2</link>
            <guid isPermaLink="false">https://medium.com/p/bf2c3bcec122</guid>
            <category><![CDATA[maven]]></category>
            <category><![CDATA[java-build]]></category>
            <category><![CDATA[build]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[compilation]]></category>
            <dc:creator><![CDATA[Manvendra P Singh]]></dc:creator>
            <pubDate>Mon, 20 Feb 2023 21:27:15 GMT</pubDate>
            <atom:updated>2023-02-20T21:36:56.990Z</atom:updated>
            <content:encoded><![CDATA[<p>During our java based microservice development, we extensively use build tools like <strong>Maven or Gradle. </strong>Usually, IDEs do a lot on our behalf or we just run some predefined commands without checking what&#39;s happening inside. Here in this series of 6 posts, I tried to explain Maven.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*L8gm7h0ShNmLIO8b1znNag.png" /></figure><p>Before I start talking about what Maven is, and its different components, let’s discuss the “why”. Why do we even need Maven? <br>For this, I’ve to first explain the nature of a Java-based project and also need to take you back in history.</p><h3>The “Build” Step.</h3><p>Java is a compilable language, Unlike Python or Javascript, which are interpreted. ie, the code we write in java, can not as-is run on a Java virtual machine (JVM). JVM understands only the bytecode.</p><p>Therefore, in the Java world, there is always a need for an<strong> intermediary step. </strong>A step that compiles the java code files into bytecode. That&#39;s why after writing the java code, we “somehow” create some deployable (jar, war, ear) to run on machines having JVMs.</p><p>This <strong>intermediary step </strong>of<strong> </strong>converting the java code to a deployable is called the <strong>build process, </strong>and this is what gave birth to various <strong>build tools </strong>for java.</p><blockquote>In an enterprise application, the build process is more complex. Older <strong>monolith enterprise applications </strong>used to have many other components, ie, XML configs, UI code files, etc. So the <strong>intermediary step</strong> was not limited to code compilation, there were various other steps involved to create a final deployable, such as placing UI files in the right places, copying configuration files, etc.</blockquote><h3>Older ways</h3><p>This is how we progressed as I remember.</p><ul><li>In the early 2000s, I remember creating jar files for standalone applications by using <strong>jar -cf </strong>on<strong> </strong>my<strong> </strong>developer machine and deploying them on the required production machine.</li><li>Things got a little better when we started creating <strong>deployable</strong> using fancy IDE (eclipse) plugins, but still on the developer machines.</li><li>Then came an era of <strong>ANT. </strong>With Ant scripts, the whole build process could be defined in <strong>XML</strong> files using interdependent <strong>Targets, </strong>with predefined or custom<strong> tasks</strong>.</li></ul><blockquote>I migrated the complete build process for a few big enterprise applications with ant script and <strong>custom tasks</strong>. In retrospect, that was too much work compared to what we need to do now thanks to maven.</blockquote><h3>The unsolved problem</h3><p>ANT streamlined the build processes to a big extent by removing manual steps during the build and providing the capability to create a final deployable with just one command or click.</p><p>However, an initial project setup was still an issue. Someone still had to figure out all the dependency jars needed by the codebase and manually place them at some predefined location for ANT to use during the build. <br>And It was not just the direct dependencies that need to be placed, but also the <strong>transitive dependencies with supported versions.</strong></p><blockquote>In my experience, finding the correct version of supported dependencies was the real challenge, because of the broken backward compatibility of many transitive dependencies.</blockquote><h3>The ultimate solution</h3><p>The folks in Apache solved this problem, by introducing a build tool named <strong>Apache-Maven</strong>. The word “<strong>Maven”</strong> means an expert. So this tool was an expert on all the things a build process needs.</p><p>Maven was introduced as a build tool for Java-based projects. Maven addressed a lot more than just the dependency management problem that I explained in the previous section. <br>For example, code quality checks, running integration tests, deploying the artifact to the shared repository, etc. We will discuss them in the next 6 (or more) posts of this series. Here are the direct link (if the post is ready) for each post in the series.</p><ol><li>Maven (1)- The Life of a build (Goals &amp; Phases)</li><li>Maven (2)- I need you (Dependencies)</li><li>Maven (3)- Conflicts resolution (the Maven way)</li><li>Maven (4)- Changing the life (build Plugins)</li><li>Maven (5)- Multi-Module project</li><li>Maven (6)- BOM to manage POM</li></ol><p>That&#39;s all for the preface of the series, go through the remaining posts in the series for more understanding.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bf2c3bcec122" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Git Series (5) — Picking a specific commit.]]></title>
            <link>https://medium.com/@manvendrapsingh/git-series-4-picking-a-specific-commit-c07379d9ae8f?source=rss-17126b6e03------2</link>
            <guid isPermaLink="false">https://medium.com/p/c07379d9ae8f</guid>
            <category><![CDATA[git]]></category>
            <category><![CDATA[gitlab]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[cherry-picking]]></category>
            <category><![CDATA[git-cherry-pick]]></category>
            <dc:creator><![CDATA[Manvendra P Singh]]></dc:creator>
            <pubDate>Mon, 26 Dec 2022 06:06:31 GMT</pubDate>
            <atom:updated>2023-02-20T22:11:57.355Z</atom:updated>
            <content:encoded><![CDATA[<h3>Git Series (5) — Picking a specific commit.</h3><p>There are scenarios when we want to apply the changes of an earlier commit in a repo, on top of the current repo tree of some branch in the same repo. <br>Or<br>In some scenarios, we just want all the changes from an earlier commit of this repo into the current staging area and make some more changes before finally committing all the changes to the repo tree.</p><p>For both scenarios, the git <strong>cherry-pick </strong>command<strong> </strong>comes in handy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HC7Rgmabrc018OM-mq0Slw.png" /></figure><p><a href="https://git-scm.com/docs/git-cherry-pick">Git documentation</a> describes cherry-pick as</p><blockquote>git-cherry-pick — Apply the changes introduced by some existing commits.<br>Given one or more existing commits, apply the change each one introduces, recording a new commit for each</blockquote><p>Let’s explain this with an example</p><h3>Step 1- Create a git repo</h3><pre>mkdir git-example-cherry-pick &amp;&amp; \ <br>cd git-example-cherry-pick &amp;&amp; \<br>git init</pre><h3>Step 2- Add a few commits</h3><pre>echo &quot;First change&quot; &gt;&gt; first.file &amp;&amp; \<br>git add first.file &amp;&amp; \<br>git commit -m &quot;first commit&quot;<br><br><br>echo &quot;Second change&quot; &gt;&gt; second.file &amp;&amp; \ <br>git add second.file &amp;&amp; \<br>git commit -m &quot;second commit&quot;<br><br><br>echo &quot;Third change&quot; &gt;&gt; third.file &amp;&amp; \<br>git add third.file &amp;&amp; \<br>git commit -m &quot;third commit&quot;</pre><blockquote>If you’ve been following my earlier posts in the git series, you already know that commits are the first class citizen in Git. Branches have very small significance as they are merely named pointers for the HEAD of a repo tree.</blockquote><blockquote>In this example, we will see, that it doesn’t matter if <strong>cherry-pick</strong> is done across branches or within the same branch.</blockquote><h3>Step 3- Switch to a new branch and add a commit</h3><pre>git checkout -b branch-one<br><br><br>echo &quot;4 change&quot; &gt;&gt; 4.file &amp;&amp; \<br>git add 4.file &amp;&amp; \<br>git commit -m &quot;4 commit&quot;</pre><h3>Step 4- Switch to another branch and add a commit</h3><p>Let’s create a new branch named <strong>branch-two. </strong>We will create this branch out of the <strong>master</strong> branch, so the file named <strong>4.file </strong>created in <strong>branch-one</strong> in step 3 will not be present in this new branch <strong>branch-two.</strong></p><pre>git checkout -b branch-two master<br><br><br>echo &quot;5 change&quot; &gt;&gt; 5.file &amp;&amp; \<br>git add 5.file &amp;&amp; \<br>git commit -m &quot;5 commit&quot;</pre><h3>Step 5- Use cherry-pick</h3><p>Now if you see the files in the current folder, you will not see a file named <strong>4.file</strong>. This file was created as part of a 4th commit which was applied only on branch-one</p><p>To apply the same changes in the current branch, we need the commit hash of the commit. <strong>git log </strong>command can be used for the same.<br>This is what I see when I look at the git logs for <strong>branch-one.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8c4zixCvPT_n2gP2W4KOTw.png" /></figure><p>Now we are on <strong>branch-two</strong>, Let&#39;s apply the change from commit by using the cherry-pick.</p><p><strong>git cherry-pick</strong> 0938880</p><p>This takes all the changes done under the mentioned commit &amp; applies them on top of the current HEAD. In our case, one new file named <strong>4.file </strong>will be added.</p><h3>Duplicate commits problem</h3><p>With the cherry-pick, the exact same changes are applied with a <strong>new commit SHA identifier</strong>. In other words, cherry-pick causes duplicate commits.</p><p>If you really needed the code from an earlier commit which you are cherry-picking as a base from your own further changes, you can use the command with the<strong> — no-commit option</strong>. <br>This will not create a duplicate commit but will put the changes from that earlier commit into your working directory. <br>Now you can make your changes on top of this code and then commit.</p><p><strong>git cherry-pick</strong> 0938880 <strong>--no-commit</strong></p><p>This is all for a simple but very useful cherry-pick command.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c07379d9ae8f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[I finally did it after 2 years & now ashamed of my prejudice.]]></title>
            <link>https://medium.com/@manvendrapsingh/i-finally-did-it-after-2-years-now-ashamed-of-my-prejudice-a83c44004165?source=rss-17126b6e03------2</link>
            <guid isPermaLink="false">https://medium.com/p/a83c44004165</guid>
            <category><![CDATA[windows-10]]></category>
            <category><![CDATA[macos]]></category>
            <category><![CDATA[logitech-mouse]]></category>
            <category><![CDATA[custom-built-computers]]></category>
            <category><![CDATA[hackintosh]]></category>
            <dc:creator><![CDATA[Manvendra P Singh]]></dc:creator>
            <pubDate>Sun, 18 Dec 2022 19:38:40 GMT</pubDate>
            <atom:updated>2022-12-21T21:39:45.311Z</atom:updated>
            <content:encoded><![CDATA[<p>A couple of years ago I planned to build <strong>A Dual Boot Hackintosh</strong> with <strong>Windows </strong>and <strong>macOS. </strong>You can read more about my hardware selections in <a href="https://medium.com/@manvendrapsingh/my-custom-build-pc-ee7b9ff81474">this</a> old post.<strong><br></strong>But after installing macOS and configuring it for my hardware, I got sucked into the <strong>Apple ecosystem</strong> and did not even bother to check for windows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_mf9UQJsnvcASEUlSxys-Q.jpeg" /></figure><h3>The Switch</h3><p>For over a year, I used this custom-built machine with <strong>Linux</strong> and <strong>Big Sur </strong>as my only computer. It is when I recently bought a <strong>13&quot; M1 MacBook Pro</strong> on a deal, I stopped using this custom-built. <br>My Hackintosh was not lacking in any performance, but working from anywhere on a 13-inch lightweight easy-to-carry machine, whose battery holds for more than a day, is better than sitting or standing at a desk at a fixed place.</p><p>This switch rendered my Hackintosh a bit useless. So I decided to play with windows OS.</p><h3>The prejudice.</h3><p>I had been using windows for 15 years and had a preconceived notion that windows-OS is very slow compared to macOS.</p><p>What I missed realizing was the fact that I’ve always used MacOS on my machines but my windows machines were always company-provided machines. <br>It was not Windows OS that ruined the user experience but it was those perpetual file-system scanning software, sluggish antiviruses, firewall, and much more internal software that were always running on those company machines.</p><p>Also, a lot has changed in Windows 10, Maybe my prejudice is justifiable for earlier windows versions.</p><h3>Current Windows is/may-be better</h3><p>It may be a very controversial statement, but not for custom builds. Nowadays windows, OS is a far better choice than macOS for custom-built machines</p><h4>Better Hardware</h4><p>The biggest problem with macOS is, you can not install it on any intel processor above the 10th gen. The OS and architecture is completely different since the introduction of the M1 Mac. Therefore, you are stuck with either 10th gen processor with 400 series motherboards. Or older.</p><p>However, with windows, you can choose any latest hardware.</p><h4>Hardware related Softwares</h4><p>Another point in favor of windows is hardware-specific software.</p><p>There are many Softwares which can configure custom hardware behavior but you can install them only on windows. Like my <a href="https://amzn.to/3hyDWhT">Gigabyte-Arous-Ultra</a> motherboard configurator, <a href="https://amzn.to/3HJAWd6">XPG</a> color schemes, etc.</p><h4>macOS Trackpad multitouch gestures</h4><p>This was one of the great features that were not available outside of MacOS.</p><p>With <strong>Windows 10, </strong>Microsoft<strong> </strong>introduced the concept of <strong>Multi-Desktops</strong> that you can swipe across with gestures, including some more multitouch gestures. But due to hardware, the experience was never close to working on a MacBook.</p><p>That problem can be solved by this <a href="https://amzn.to/3HN4fvo">Logitech M720 Triathlon</a> mouse where you can configure many gestures with <a href="https://www.logitech.com/en-us/software/logi-options-plus.html">Logitech Option +</a> and they work flawlessly.<br>I use this mouse, but there are many others to choose from <a href="https://amzn.to/3W6cNBL">here</a>, which you can configure with option+ and have a macOS-like experience</p><h4>The Boot time</h4><p>It&#39;s blazing fast, if you do not add a password and have only one user, in less than a second of switching on the power you will be inside windows. At least that&#39;s how it works on my Machine.</p><h4>A fluid design</h4><p>From Windows 10 onwards, the windows experience is as fluid as MacOS, provided you are not working on your company machine :-)</p><h4>The terminal</h4><p>If you are a technical user, you might hate windows for its <strong>CMD prompt </strong>with a limited command set and love MacOS for its easy-use <strong>terminal</strong> app, which can be configured with any shell although Zsh is enough.</p><p>You can however install <strong>bash</strong> on <strong>Windows OS </strong>by turning <strong>Developer-Mode </strong>on. However, it&#39;s not as powerful as the <strong>Terminal app &amp; Homebrew combination. </strong>working with <strong>PowerShell</strong> is a nightmare in itself. But it still gets the simple tasks done</p><p>In the past, I had been very hard on windows OS, so I had to get this article out to repent my sins. :-) . That&#39;s all for this post, happy computing.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a83c44004165" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create a window installer USB on a Mac]]></title>
            <link>https://medium.com/@manvendrapsingh/create-a-window-installer-usb-on-a-mac-b594644248cb?source=rss-17126b6e03------2</link>
            <guid isPermaLink="false">https://medium.com/p/b594644248cb</guid>
            <category><![CDATA[macbook]]></category>
            <category><![CDATA[windows-installer]]></category>
            <category><![CDATA[windows-operating-system]]></category>
            <category><![CDATA[mac-on-windows]]></category>
            <category><![CDATA[windows-installation]]></category>
            <dc:creator><![CDATA[Manvendra P Singh]]></dc:creator>
            <pubDate>Tue, 13 Dec 2022 23:42:35 GMT</pubDate>
            <atom:updated>2022-12-13T23:42:35.585Z</atom:updated>
            <content:encoded><![CDATA[<p>Let’s assume for some inexplicable reasons, we want to install the windows operating system on our machines. For this, first, we need to have a windows installer USB.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fwf79TnFaHuSpa8G" /><figcaption>Photo by <a href="https://unsplash.com/it/@brina_blum?utm_source=medium&amp;utm_medium=referral">Brina Blum</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>If you like to spend money, you can buy an original windows installer USB from <a href="https://amzn.to/3VXttLI"><strong><em>here</em></strong> </a>and skip the remaining post. <br><strong>Otherwise,</strong><br>You can create an installer USB by yourself by performing<strong><em> </em></strong><a href="https://learn.microsoft.com/en-us/windows-hardware/manufacture/desktop/install-windows-from-a-usb-flash-drive?view=windows-11"><strong><em>these step</em></strong></a><strong><em>s</em></strong> on a windows machine.</p><p>But wait. The reason that we want to create a windows installer USB in the first place was, that we do not have a machine running windows. Like me, if you only have macOS machines then you can use the below 3 steps to create a windows installer USB.</p><h3>1- Download windows ISO</h3><p>You can download windows ISO for free from the Microsoft <a href="https://www.microsoft.com/en-us/software-download/windows10ISO">website</a>. <br><strong>1.1- </strong>Choose the <strong>windows edition</strong> as follows</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/665/1*iUUBE0XWcxt9YAdeq8aAcQ.png" /></figure><p><strong>1.2- </strong>Choose the language as <strong>English</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/618/1*FuHYkEXHUW044KevPqx6qQ.png" /></figure><p><strong>1.3- </strong>Choose <strong>64 bits</strong> version</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/448/1*aXDvqy9U6f-ZhTkbIY7rYA.png" /></figure><p><strong>1.4- </strong>Above step will download an ISO file in your download location. Double-click on the ISO file and it will be mounted as a volume.</p><h3>2- Prepare the USB drive</h3><p>Now, let’s prepare the USB drive to copy the content from this ISO.</p><p><strong>2.1- </strong>Insert a USB drive of at least 8 GB capacity in your Mac.</p><p><strong>2.2- </strong>Open the <strong>Disk Utility</strong> app on your Mac.</p><p><strong>2.3- </strong>Click on the USB drive in the left column under <strong>External </strong>and click on <strong>Erase</strong> somewhere on the right top.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/974/1*IeIn1CCqIk05tB3Sb1gmXQ.png" /></figure><p><strong>2.4-</strong> I used the <strong>Name:</strong> <strong>WIN-USB</strong> but you can choose any. However, The <strong>Format:</strong> must be <strong>MS-DOS (FAT),</strong> and <strong>Scheme:</strong> need to be <strong>Master Boot Record</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/507/1*zhgVf2KcluRT_8Yf5QN5Lw.png" /></figure><h3>3- Copy the ISO content to the USB</h3><p>After the step-1 and 2. you should be able to see 2 extra mounted volumes under <strong>Locations</strong> in your <strong>Finder</strong> left nav. Same as the last 2 options in the below picture.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/368/1*cwvYJypLdHv57qNmAdPF2w.png" /></figure><p>Now, all we need to do is copy all the content from the location named <strong>CC***_X64***</strong> to the <strong>WIN-USB</strong>.</p><p>But there is one file <strong>sources/install.wim</strong> in our installer content, which is more than 4 GB. And the File format that we chose to format our USB only supports a maximum file size of 4GB. However, all the other files can be copied to a USB drive as is.</p><p>So</p><p><strong>3.1</strong>- Select all the folders except the source folder from this <strong>CC***_X64** </strong>volume as below, and paste it into the <strong>WIN-USB </strong>volume.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/356/1*Nrc-4S0JYqRLCev4qzpKkA.png" /></figure><p><strong>3.2</strong>- Create a folder name <strong>sources</strong> inside the USB drive and copy all the content of the <strong>sources</strong> folder from ISO into the <strong>sources </strong>folder in USB except the one file<strong> install.wim</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/514/1*WkHJmS9gR9rdpZZSCt7ZXg.png" /></figure><p>To copy <strong>sources/install.wim</strong> to USB, we need to break the file into pieces smaller than 4GB. For this, we are going to use the <a href="https://wimlib.net/"><strong>wimlib</strong></a><strong> </strong>utility. To install wimlib on the Mac, we will use <a href="https://brew.sh/"><strong>Homebrew</strong></a><strong>.</strong></p><blockquote>If you do not have homebrew on your mac my first question is why, But then my next statement is, you can easily install it with the below commands.</blockquote><blockquote><strong>/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)&quot;</strong></blockquote><p><strong>3.3</strong>- Assuming we have homebrew installed on Mac, we can install <strong>wimlib</strong> by the following command.<br>[~ %]==&gt; brew install wimlib</p><p><strong>3.4</strong>- After wimlib is installed we can use the following command to split our <strong>install.wim </strong>file into smaller files and copy them to USB.</p><pre>[~ %]==&gt; wimlib-imagex split /Volumes/CCCOMA_X64FRE_EN-US_DV9/sources/install.wim /Volumes/WIN-USB/sources/install.swm 4000</pre><p>That&#39;s all, now you can eject this USB from MAC and use it to install windows on a PC.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b594644248cb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[BDD Series (4) — karate API testing. Hands-on!]]></title>
            <link>https://medium.com/@manvendrapsingh/bdd-series-4-karate-api-testing-hands-on-55b8146ef1ec?source=rss-17126b6e03------2</link>
            <guid isPermaLink="false">https://medium.com/p/55b8146ef1ec</guid>
            <category><![CDATA[maven]]></category>
            <category><![CDATA[karate]]></category>
            <category><![CDATA[api-development]]></category>
            <category><![CDATA[cicd-pipeline]]></category>
            <category><![CDATA[api-testing]]></category>
            <dc:creator><![CDATA[Manvendra P Singh]]></dc:creator>
            <pubDate>Sat, 03 Sep 2022 18:53:19 GMT</pubDate>
            <atom:updated>2023-02-23T05:39:34.019Z</atom:updated>
            <content:encoded><![CDATA[<h3>BDD Series (4) — karate API testing. Hands-on!</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hEE0GGJqicVxbEJR" /><figcaption>Photo by <a href="https://unsplash.com/@jsnbrsc?utm_source=medium&amp;utm_medium=referral">Jason Briscoe</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In our previous post <a href="https://medium.com/@manvendrapsingh/bdd-continues-anatomy-of-a-feature-file-cucumber-8628c95777c4">Anatomy of a feature fil</a>e, we talked about the feature files, the Gherkin syntax, and Gherkin keywords. In this post, we will see a quick overview of Intuit Karate based on Gherkin.<br>Karate DSL uses Gherkin syntax, If you’re new to Cucumber &amp; Gherkin, I highly recommend reading the previous post to understand terminologies.</p><h3>Why Karate</h3><p><strong>Cucumber</strong>, the flagship BDD framework, popularized the <strong>programming language-neutral</strong>, behavioral test constructs, written in <strong>human languages </strong>using Gherkin. <br>The <strong>human-language </strong>test steps mentioned in the <strong>feature file</strong> are backed by programming-language-dependent <strong>Step-Definition </strong>files, that are processed by the Cucumber engine.</p><p>In today&#39;s world, development teams often work on Headless systems only comprised of the backend APIs<strong>. </strong>The Gherkin steps to test such systems are fixed. Such as but not limited to.</p><ul><li>Set API <strong>URL</strong></li><li>Set request<strong> </strong><strong>HEADER</strong></li><li>Append request<strong> </strong><strong>PATH</strong></li><li>Set request<strong> </strong><strong>PARAM</strong></li><li>Set HTTP methods <strong>GET, POST, PUT, etc</strong></li><li>Read/verify the <strong>RESPONSE</strong></li></ul><p>A skilled development team can write the step definitions for all these steps in any programming language, but that will not be the best use of their time. This is where Karate comes into the picture.</p><h3>What is Karate</h3><p><strong><em>Karate</em></strong><em> is an open-source framework for </em><strong><em>API Test Automation</em></strong><em> (</em>and more<em>) having support for </em><strong>HTTP, JSON, GraphQL, and XML.</strong> It also provides many step definitions needed for API testing. And has built-in <strong>HTML reports</strong>, and the ability to run tests in parallel.</p><p>Karate itself is written in Java, but it can be used without any prior knowledge of Java. However, in the beginning, we do require a <strong>one-time effort</strong> to set up Maven or Gradle builds and the Karate java test class.</p><p>As long as you know how to run commands on the terminal and edit files, you can use the below steps and start working on Karate.</p><p>After this initial setup, all you need to know is your APIs and Gherkin Syntax with Karate DSL</p><h3>Basic Karate Project setup</h3><p>In this post, we will create a bare minimum configured Karate Project to test the Google website. Here are the steps.</p><h3><strong>1. Create a simple maven project</strong></h3><p>It&#39;s not mandatory, but to keep life simple we require a Gradle or Maven project, so we can manage dependencies and trigger Karate with one command or click. I’m going to use Maven for our example here.</p><p>Do not worry if you do not understand these terms, just use the following command on the terminal.<br>OR <br>If you know how to create a maven project using your IDE, skip the command and create a maven project the way you like.</p><pre><strong>mvn archetype:generate</strong> \<br><strong>-DarchetypeGroupId</strong>=org.apache.maven.archetypes \<br><strong>-DarchetypeArtifactId</strong>=maven-archetype-simple \<br><strong>-DarchetypeVersion</strong>=1.4 \<br><strong>-DgroupId</strong>=com.mps.examples \<br><strong>-DartifactId</strong>=simple-karate-example \<br><strong>-Dversion</strong>=1.0-SNAPSHOT</pre><h3>2. Fix autogenerated maven project</h3><p>The above command generates a folder named <strong>simple-karate-example </strong>containing<strong> </strong>many files. But the <strong>maven-archetype-simple </strong>is outdated, so we need to make some changes to autogenerated files.</p><h4>2.1. Update project Java version</h4><p>I’m not sure why the maven archetype still points to <strong>java 1.7</strong>. So we will manually change it to use <strong>java 1.8</strong></p><p>Open the <strong>pom.xml file,</strong> it should be right under the <strong>simple-karate-example </strong>folder. Look for <strong>maven.compiler.source &amp; </strong><strong>maven.compiler.targe</strong> and Let&#39;s make sure we do not have 1.7 mentioned here, if so, update it with 1.8. Finally, it should look as below.</p><pre><strong>&lt;maven.compiler.source&gt;</strong>1.8<strong>&lt;/maven.compiler.source&gt;<br>&lt;maven.compiler.target&gt;</strong>1.8<strong>&lt;/maven.compiler.target&gt;</strong></pre><h4>2.2. Update the surefire plugin</h4><p>If you’ve read my series on Maven, you might know maven <strong>build lifecycle</strong> is based on <strong>phases</strong>, that are comprised of <strong>goals</strong>, that are executed by <strong>maven plugins</strong>. One such plugin is <strong>surefire</strong>, which is used in the <strong>test</strong> phase. We need a minimum <strong>2.22.1</strong> <strong>surefire </strong>version to support this Karate example.</p><p>It&#39;s ok if you don&#39;t understand the above, just look for the <strong>&lt;plugins&gt; &lt;/plugin&gt; </strong>tags in the autogenerated <strong>pom.xml</strong> and delete everything between these tags. Then add the below lines</p><pre>&lt;<strong>plugins</strong>&gt;<br>  &lt;<strong>plugin</strong>&gt;<br>    &lt;<strong>groupId</strong>&gt;org.apache.maven.plugins&lt;/<strong>groupId</strong>&gt;<br>    &lt;<strong>artifactId</strong>&gt;maven-surefire-plugin&lt;/<strong>artifactId</strong>&gt;.  <br>   <strong> &lt;version&gt;</strong>2.22.1<strong>&lt;/version&gt;</strong><br>    &lt;<strong>configuration</strong>&gt;<br>      &lt;<strong>argLine</strong>&gt;-Dfile.encoding=UTF-8&lt;/<strong>argLine</strong>&gt;<br>    &lt;/<strong>configuration</strong>&gt;<br>  &lt;/<strong>plugin</strong>&gt;<br>&lt;/<strong>plugins</strong>&gt;</pre><h4>2.3: update test resources</h4><p>Karate tests are written in feature files, that are not Java files. Maven expects non-java test files to be placed under the <strong>resources</strong> folder. But for code maintainability, I want to keep feature files next to the Java test file.</p><p>For maven to pick up our feature files, add the following between the <strong>&lt;build&gt;&lt;/build&gt;</strong> tags.</p><pre><strong>&lt;testResources&gt;<br>  &lt;testResource&gt;<br>    &lt;directory&gt;</strong>src/test/java<strong>&lt;/directory&gt;<br>    &lt;excludes&gt;<br>      &lt;exclude&gt;</strong>**/*.java<strong>&lt;/exclude&gt;<br>    &lt;/excludes&gt;<br>  &lt;/testResource&gt;<br>&lt;/testResources&gt;</strong></pre><h4>2.4 Remove Old Junit dependency</h4><p>As I said above, due to the outdated archetype, on the day of writing this blog, the autogenerated <strong>POM.xml </strong>comes with an old <strong>Junit</strong> dependency. We need to remove this dependency.</p><p>Search for <strong>&lt;dependencies&gt; &lt;/dependencies&gt; </strong>tags in <strong>pom.xml</strong> file. Delete the <strong>Junit</strong> <strong>dependency</strong> present between the <strong>&lt;dependencies&gt;</strong> tags.</p><p>The correct Junit version will come as a <strong>transitive dependency, </strong>once we add Karate in the next step,</p><h3><strong>3. Add code to Trigger Karate</strong></h3><p>The command in step 1 created a basic maven project. Now let&#39;s introduce <strong>Karate</strong> to our project.</p><h4>3.1 Add Karate dependency</h4><p>Add karate by pasting the below lines inside the <strong>dependencies </strong>tag<strong>.</strong></p><pre>&lt;dependency&gt;<br>    &lt;groupId&gt;<strong>com.intuit.karate</strong>&lt;/groupId&gt;<br>    &lt;artifactId&gt;<strong>karate-junit5</strong>&lt;/artifactId&gt;<br>    &lt;version&gt;1.2.0&lt;/version&gt;<br>    &lt;scope&gt;test&lt;/scope&gt;<br>&lt;/dependency&gt;</pre><h4>3.2 Edit Java Test file to trigger Karate</h4><p>The command used in step 1 created the below folder structure, <br>As we are building a test suite, we are interested only in the <strong>AppTest.java</strong> file. We can delete the <strong>site</strong> &amp; <strong>main</strong> folder</p><pre>├── pom.xml<br>└── src<br>    ├── main<br>    │ └── java<br>    │     └── com<br>    │         └── mps<br>    │             └── examples<br>    │                 └── App.java<br>    ├── site<br>    │ └── site.xml<br>    └── test<br>        └── java<br>            └── com<br>                └── mps<br>                    └── examples<br>                        └── <strong>AppTest.java</strong></pre><p>The <strong>AppTest.java</strong> file refers to the old Junit versions which we‘ve removed from <strong>pom.xml</strong>. Let&#39;s delete everything in this file until you see it below.</p><pre>package com.mps.examples;<br><br>public class AppTest {<br><br>}</pre><blockquote>When we run a maven build, the maven <strong>test phase</strong> looks for all Java files inside the <strong>test</strong> folder and its subfolders, which are ending with the word <strong>”Test&quot;</strong>. And execute the methods annotated with the <strong>Test </strong>annotation.</blockquote><p>We can add a test in our <strong>AppTest.java.</strong> to trigger the Karate test suite upon a maven build. Copy the below lines in <strong>AppTest.java.</strong></p><pre><strong>@Test</strong><br>void triggerKarate() {<br>    com.intuit.karate.Results results =<br>            com.intuit.karate.Runner<br>                    .<em>builder</em>()<br>                    <strong>.relativeTo(getClass())<br>                    </strong>.parallel(2);</pre><pre>    org.junit.jupiter.api.Assertions.<em>assertEquals <br>              </em>(0, results.getFailCount(),  <br>               results.getErrorMessages());<br>}</pre><p>When we execute the command <strong>mvn clean test </strong>inside the <strong>simple-karate-example </strong>folder, this test runs and due to <strong>.relativeTo(getClass())</strong>, karate looks for any <strong>.feature </strong>file in the same folder or subfolder as <strong>AppTest</strong> class and executes it.</p><p>This is all that is mandatory for the basic setup.</p><h3>4. Create a Krate test feature File</h3><p>I assume you either already work with <strong>Gherkin</strong> <strong>syntax</strong> or have read my previous posts in this series. Because Gherkin &amp; JSON are the only things that we need to know for writing simple karate tests.</p><p>Let&#39;s write a feature to test if Google is working or not. Let&#39;s create a new file in the same folder as <strong>AppTest.java</strong> I named it <strong>google.feature </strong>but you can name it anything as long as the name ends with the <strong>.feature</strong> extension,</p><p>Let&#39;s add the below content to the file</p><pre><strong>Feature</strong>: Feature to test the Google website<br><br>  <strong>Scenario</strong>: Google site should be up<br>    <strong>Given</strong> url &#39;https://www.google.com&#39;<br>    <strong>When</strong> method Get<br>    <strong>Then</strong> status 200<br><br>  <strong>Scenario</strong>: Google site should not respond to PUT<br>    <strong>Given</strong> url &#39;https://www.google.com&#39;<br>    <strong>When</strong> method PUT<br>    <strong>Then</strong> assert responseStatus != 200</pre><h3>5. Run the Karate Suite</h3><p>Now with the magic of maven, all we need is, to run the <strong>mvn clean test </strong>command</p><p>Karate will execute all the feature files and will generate a fancy report for us. You can find the report path at the end of the build output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RZjvwPT-AS6TGBx4PUZvRQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7Gl9bKMAQ7mBUlW8Kk6XxA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iDzGJ1IKRFPOlWNVtkknwA.png" /></figure><h3>Some optional Steps</h3><p>Below are a few optional steps that can be used to set up a Karate test suite</p><h3>1. Setup logging</h3><blockquote>You can skip this step and Karate will use default settings.</blockquote><p>Karate uses <a href="https://logback.qos.ch/"><strong>Logback</strong></a> to perform logging during test execution. <strong>Logback</strong> can be configured using <strong>logback-test.xml</strong> or <strong>logback.xml</strong> in src/test and src/main respectively. <br>In absence of these files, <strong>Karate</strong> will use the default settings, which will print a lot more data than you might be interested in.</p><p>So for our bare minimum setup, let’s create a file named <strong>logback-test.xml </strong>inside<strong> </strong><strong>simple-karate-example/src/test/java </strong>and paste the below content.</p><p>This will make sure only relevant information is logged on the terminal.</p><pre><strong>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;configuration&gt;</strong></pre><pre><strong>   &lt;appender name=&quot;</strong>STDOUT<strong>&quot;   <br>           class=&quot;</strong>ch.qos.logback.core.ConsoleAppender<strong>&quot;&gt;<br>       &lt;encoder&gt;<br>          &lt;pattern&gt;<br>            </strong>%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n<br>          <strong>&lt;/pattern&gt;<br>      &lt;/encoder&gt;<br>  &lt;/appender&gt;</strong></pre><pre><strong>  &lt;logger name=&quot;</strong>com.intuit.karate<strong>&quot; <br>         level=&quot;</strong>INFO<strong>&quot;&gt;<br>       &lt;appender-ref ref=&quot;</strong>STDOUT<strong>&quot; /&gt;<br>  &lt;/logger&gt;<br>&lt;/configuration&gt;</strong></pre><h3>2. Configuring Karate</h3><blockquote>You can skip this step if there is nothing to configure.</blockquote><p>Karate can be configured by providing a file named <strong>karate-config.js </strong>in the classpath.</p><p>This file must contain<strong> a javascript function</strong> that returns a JSON object. <br>All the properties in this JSON object are made available to each feature file during test execution.</p><p>For a simple demo, let&#39;s create a file <strong>karate-config.js </strong>inside<strong> </strong><strong>simple-karate-example/src/test/java </strong>and paste the below content.</p><pre>function fn() {<br><br>  var config = {<br>     name: {<br>        first: &#39;Manvendra&#39;,<br>        middle: &#39;Pratap&#39;,<br>        last: &#39;Singh&#39;,<br>     }<br>  };<br>  return config;<br>}</pre><p>This is how we are making my name available to all Feature-Files and scenarios in Karate. Now you can access this variable <strong>name</strong> in any of the feature files, let&#39;s add this background step at the top of our feature file <strong>google.feature</strong></p><pre>Background:<br>  * print name.first</pre><p>Now when you run the <strong>mvn clean test </strong>command<strong> </strong>inside the <strong>simple-karate-example </strong>folder, you will see my first name is printed in logs.</p><blockquote>This file also has access to <strong>ScenarioBridge </strong>class as a javascript variable named <strong>karate. <br></strong>You can use this variable to configure/execute various available methods in <strong>ScenarioBrige</strong> but more on this in a later post in this series.</blockquote><p>This is all for a basic setup for the Karate API test suite. In the next post, we will dive a little deeper into <strong>best practices,</strong> <strong>configuration,</strong> and other <strong>quirks</strong> of writing a real-world full-fledged Karate suite for a microservice.</p><p>Stay tuned.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=55b8146ef1ec" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Git Series (4) — Cleaning up the Pull requests]]></title>
            <link>https://medium.com/@manvendrapsingh/git-series-4-cleaning-up-the-pull-requests-3812af724c16?source=rss-17126b6e03------2</link>
            <guid isPermaLink="false">https://medium.com/p/3812af724c16</guid>
            <category><![CDATA[pull-request]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[git-rebase]]></category>
            <category><![CDATA[git-commit]]></category>
            <category><![CDATA[git-reset]]></category>
            <dc:creator><![CDATA[Manvendra P Singh]]></dc:creator>
            <pubDate>Sun, 14 Aug 2022 22:31:10 GMT</pubDate>
            <atom:updated>2023-02-20T21:55:26.053Z</atom:updated>
            <content:encoded><![CDATA[<h3>Git Series (4) — Cleanup before Pull requests</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HC7Rgmabrc018OM-mq0Slw.png" /></figure><h3>Expectation</h3><p>In an ideal world, One should be able to identify the product maturity model by merely looking at the commit logs for the main/master branch in the git. But it&#39;s possible only if, each commit represent a feature Featureor change or bugfix or at least for an incremental atomic change.</p><h3>Problem</h3><p>Having only meaningful commits directly contradicts what I call as “no code left behind” principle. Developers shouldn’t leave any code on their development machines. Instead, the code should be committed and pushed to the remote repo, every day before logging off.</p><p>But sometimes one day is not enough to complete even <strong>incremental atomic changes</strong> suitable for a PR, let alone the code for the full feature. In those cases, this everyday push to the remote repo brings a lot of commits in the final Pull Request aka PR.</p><p>In this post, I will show how can we combine all these commits before raising a PR, or as they call MR in GitLab.</p><h3>Solution</h3><p>There are two ways, I’ve used in the past to combine commits. One is by using rebase and another by reset.</p><blockquote><strong>NOTE — </strong><em>both these processes change commit history in the remote repo. So </em><strong><em>DO NOT use anything explained below if you are working on a shared branch</em></strong><em> (branches </em>where other developers also push &amp; pull the code<em>). <br>All this should be used to fix the commits on the branches, only you are working on.</em></blockquote><h3>Let&#39;s create the problem</h3><h4>Setup a repository</h4><p>Let&#39;s create a <strong>local git repo </strong>and add a README file in the master branch.</p><pre>mkdir git-prs &amp;&amp; cd git-prs &amp;&amp; <strong>git init</strong></pre><pre>echo “#Git PRs” &gt;&gt; README.md &amp;&amp; <strong>git add</strong> README.md &amp;&amp; <strong>git commit</strong> -m “first commit”</pre><p>Let&#39;s <a href="https://docs.github.com/en/get-started/quickstart/create-a-repo">create a repository</a> named git-prs in your GitHub account &amp; push the code to remote.</p><pre><strong>git remote add</strong> origin git@github.com:&lt;your-user/org-id&gt;/git-prs.git</pre><pre><strong>git push</strong> -u origin master</pre><h4>A slow developer — Multiple pushes, No PR</h4><p>Let&#39;s assume, a user story was assigned to me <strong>to add a few lines of description </strong>to the project readme file. <br>Because of my development speed, I am able to add only one line a day. And I push my code to the remote repo every day before logging off from work.</p><p>Here are the steps</p><pre><strong>//Day-1</strong><br>git checkout -b feature/readme-description</pre><pre>echo &quot;Line 1&quot; &gt;&gt; README.md &amp;&amp; git add README.md &amp;&amp; <strong>git commit</strong> -m &quot;Added description in readme&quot; &amp;&amp; <strong>git push</strong> -u origin HEAD</pre><pre><strong>//Day-2 </strong><br>echo &quot;Line 2&quot; &gt;&gt; README.md &amp;&amp; git add README.md &amp;&amp; <strong>git commit</strong> -m &quot;updated description in readme&quot; &amp;&amp; <strong>git push</strong> origin HEAD</pre><pre><strong>//Day-3</strong><br>echo &quot;Line 3&quot; &gt;&gt; README.md &amp;&amp; git add README.md &amp;&amp; <strong>git commit</strong> -m &quot;Line-3 description in readme&quot; &amp;&amp; <strong>git push</strong> origin HEAD</pre><p><strong>On day 4</strong>, when I tried to create the PR, I realized I have these 3 commits for just one logical change. This will clutter the final project history</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bk_U2WbaA0tFfGQW7VhMqg.png" /></figure><p>So here are the steps I can take to make my PR look good</p><h3>Squash commits using Rebase</h3><p>We want to meld the <strong>last 3 commits</strong> into one, so we will do the interactive rebase on the<strong> 3 commits prior to HEAD </strong>using the following commands</p><p><strong>git rebase -i HEAD~3</strong></p><p>Once you press enter, the following content will show up in whichever editor is configured in your git.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CYgX3c_WQQTUJAzam54a-A.png" /></figure><h4>Content of this file</h4><p>The lines prefixed with # are the comments for some help documentation provided by git. The remaining lines at the top are the commits that we want to work on during this rebase.</p><p><strong>NOTE</strong>- Unlike git log, in the rebase output the latest commit is displayed at the bottom and the oldest at the top.</p><p>Each line has the following 3 records.<br><strong>&lt;rebase-action&gt; &lt;commit-hash&gt;</strong> &lt;<strong>commit-description&gt;</strong></p><p>For our use case, only the following 2 rebase-actions are needed.</p><ol><li><strong>fixup</strong> (f) to meld mentioned commit into the previous commit and ignore mentioned commit’s message</li><li><strong>reword</strong> (r), To change mentioned commit’s message.</li></ol><p>So, in my editor, I will write either f or fixup (both work same) in front of the commits I want to meld. Here is what my final file will look like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Id5lwuTUTf1O5-WafEXi4w.png" /></figure><p>Save this file and you will get such a message.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_kGUYT0GOUmZdnbp_T8hpg.png" /></figure><blockquote><strong>NOTE</strong>- You might not see a successfully rebased message if you’ve contradictory changes between melding commits. In that case, you will resolve conflicts as you usually do. I try to avoid such melds.</blockquote><p>We’ve changed commit history, so we will not be able to push the local repo to remote. So we’ve to use force push</p><p>git push -f origin HEAD</p><p>Now I can go back to Github and raise my nice-looking PR</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qn9lrv9yhcRFRBGVKz-ijw.png" /></figure><h3>Rewriting new commit using Reset</h3><p>If you’ve read my <a href="https://medium.com/@manvendrapsingh/git-series-2-command-line-cheat-sheet-81dceb1d284d">second post</a> in the git series, you might remember, how <strong>git reset -— &lt;option&gt; &lt;pointer&gt; </strong>affects repo-tree, staging-area, &amp; working-tree differently, when used with different options.</p><p>For our use case where we’ve 3 commits that we want to meld into one, we will do a <strong>soft reset on HEAD, going back to 3 commits.</strong></p><ul><li>To verify, run the command git log. you should see 4 commits. a first commit, followed by 3 commits where we changed the readme.</li><li>run git reset — -soft HEAD~3</li><li>Run the command git log again. Now you should see only the first commit. However, when you open the readme file, you will see all the changes from the last 3 commits are preserved.</li></ul><p>So now we’ve all our changes in the staging area and no commit in the repo tree. All we need to do now is to commit these changes with a clean commit message and push them to the remote repo (force push)</p><pre>git commit -m &quot;Adding Readme description&quot;</pre><pre>git push -f origin HEAD</pre><p>Now go ahead and raise a PR on GitHub.</p><p><strong>Conclusion</strong></p><p>That&#39;s all we had for this topic, but before I close, Two things to note here are<br><br>1. We are using a <strong>force push to remote. </strong>Avoid it if you do not completely know what you are doing.</p><p>2. <strong>Avoid </strong>using<strong> </strong>rebase or reset when you have <strong>merged or rebased commits</strong> in between the commits you want to meld.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3812af724c16" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Git Series (3) — Rename a Pushed branch]]></title>
            <link>https://medium.com/@manvendrapsingh/rename-a-pushed-git-branch-d7fff829d9a5?source=rss-17126b6e03------2</link>
            <guid isPermaLink="false">https://medium.com/p/d7fff829d9a5</guid>
            <category><![CDATA[git-branch]]></category>
            <category><![CDATA[git-remote]]></category>
            <category><![CDATA[git-checkout]]></category>
            <category><![CDATA[git-push]]></category>
            <category><![CDATA[git]]></category>
            <dc:creator><![CDATA[Manvendra P Singh]]></dc:creator>
            <pubDate>Sat, 23 Jul 2022 20:00:30 GMT</pubDate>
            <atom:updated>2023-04-11T05:02:59.391Z</atom:updated>
            <content:encoded><![CDATA[<h3>Git Series (3) — Rename a Pushed branch</h3><p><strong>Commits</strong> are the first-class citizens in Git worlds, not the <strong>Branches</strong>. But, branch names do show up in our commit messages and a few more places. Moreover, build-pipelines and other agile tools are often configured to refer to branch names with predetermined patterns.</p><p>Therefore, every organization define some nomenclature for naming branches. This post is not about <strong>Branch naming strategies</strong>, Here we will see how to change a branch name if it&#39;s not adhering to organization&#39;s standards.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HC7Rgmabrc018OM-mq0Slw.png" /></figure><h3>Create the problem</h3><p>Let&#39;s first set up our problem case.</p><ul><li>Let&#39;s say we have a repo named git-renaming in local as well as remote</li><li>Let&#39;s create a feature branch and name it incorrectly <br><strong>git checkout -b feature/master-incorrect-name</strong></li><li>Let&#39;s commit a new file in this branch<br><strong>echo “file1” &gt;&gt; file1 &amp;&amp; git add file1 &amp;&amp; git commit -m “second commit”</strong></li><li>Let&#39;s push this branch with the wrong name to remote. <br><strong>git push -u origin HEAD</strong></li></ul><p>Now we have a branch with the wrong name at local as well as at remote, Next, we will see the step to fix it.</p><h3>Correct the problem</h3><p>Before going into fixing things, let&#39;s verify what we have<br>Run below command</p><p><strong>git branch -a</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aujeAcSqWC7DtXrcGkKtKg.png" /></figure><h4>Rename the branch at local</h4><p>Let’s rename the branch to the correct name at local.</p><p>Make sure you are on the branch that you want to rename. <br>Use the below command</p><p><strong>git branch -m feature/master-correct-name</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vnAJ6FlMnJLad8FEcKwbTw.png" /></figure><h4>Fix the remote branch</h4><p>Until now we have renamed only the local branch. See highlighted line below, this branch is still tracked with a branch from remote with the old name</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LFuXAURlSUB3qmOnQbrBfw.png" /></figure><p>Unfortunately, by using the command line, can not rename the remote branch, so we will</p><ol><li>Push the branch with the correct name to remote<br> <strong>git push -u origin HEAD</strong></li><li>Delete the branch at remote with the incorrect name.<br><strong>git push origin --delete feature/master-incorrect-name</strong></li></ol><p>That&#39;s all. That&#39;s how we rename a pushed branch in git.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d7fff829d9a5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Git Series (2) — Command Line Cheat Sheet]]></title>
            <link>https://medium.com/@manvendrapsingh/git-series-2-command-line-cheat-sheet-81dceb1d284d?source=rss-17126b6e03------2</link>
            <guid isPermaLink="false">https://medium.com/p/81dceb1d284d</guid>
            <category><![CDATA[git-reset]]></category>
            <category><![CDATA[git-stash]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[git-commands]]></category>
            <category><![CDATA[git-diff]]></category>
            <dc:creator><![CDATA[Manvendra P Singh]]></dc:creator>
            <pubDate>Tue, 31 May 2022 00:57:37 GMT</pubDate>
            <atom:updated>2023-02-20T21:53:40.398Z</atom:updated>
            <content:encoded><![CDATA[<h3>Git Series (2) — Command-Line Cheatsheet</h3><p>In the <a href="https://medium.com/@manvendrapsingh/git-series-1-the-architecture-internals-3740419629d0">last post</a>, we discussed <strong>git internals</strong> to understand Git. In this post we will discuss Git commands, to use Git. I recommend reading the last post, to easily understand the terminologies used here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HC7Rgmabrc018OM-mq0Slw.png" /></figure><p>I’m going to skip the commands to install git as it varies based on the operating system and y. Once you have our machine architecture. After installation following are the commonly used commands.</p><h3><strong>1. Check Git Version</strong></h3><p><strong>git —-version </strong>check and confirm the installation</p><h3>2. Configure Git</h3><p>You can use the below command to set global configurations for Git.</p><p><strong>git config --global </strong>key “value”.</p><p>By default, Git doesn’t allow you to commit until it knows who you are. So the first thing to do after installation is, to set the below properties. Preferably globally</p><p><strong>git config --global</strong> <strong>user.name</strong> “My Name”<br><strong>git config --global</strong> <strong>user.email</strong> “<a href="/service/mailto:myemail@email.com">myemail@email.com</a>”</p><blockquote><strong>NOTE</strong>. If your present working directory is inside a local git repository, you can omit the <strong><em>—-global</em></strong> option from the above command and set up a project-specific configuration.</blockquote><h3>3. Create a local repo</h3><p>To create a git repo on your machine, you can either <strong>“clone” </strong>an already existing remote repository or you can create a fresh local repository.</p><p><strong>git init</strong> — Converts the current folder into a local git repo.<br><strong>git clone </strong>git@github:user1/repo1.git<em> — </em>Brings a repo from the remote server to the local machine.</p><h3>4. Work with branches</h3><p>The branch is just a pointer to a commit, usually at the head of the tree. Any commit you make is made on the branch you’re currently “checked out” to.</p><p>Here are a few commands to work with branches</p><p><strong>git status</strong> — Show which branch you are on, (and some more info)<br><strong>git branch</strong>— Show local branches<br><strong>git branch -a </strong>— Show both local and remote branches</p><p><strong>git branch </strong>branch-name — create a local branch<br><strong>git checkout -b </strong>branch-name — create a new branch and switch to it.</p><p><strong>git checkout </strong>branch-name —Switch to an existing branch <br><strong>git switch </strong>branch-name —similar (not same) as above</p><p><strong>git branch -d</strong> branch-name — delete a <strong>local branch<br></strong><strong>git push origin --delete</strong> branch-name — delete a remote branch. <em>Might fail in the absence of required privileges.</em></p><p><strong>git merge </strong>source-branch-name — merge the commits from the source branch into the current branch.</p><h3>4: Add new content</h3><p>Following are the commands to manage and add content to the repository</p><p><strong>git status<br></strong>Display the status of the working tree, staging area, and untracked files.</p><p><strong>git add </strong>&lt;pathspec&gt;<br>Move the files matching the given <strong>pathspec,</strong> from the <strong>working tree</strong> to <strong>Staging Area.</strong></p><p><strong>git commit -m &quot;</strong>&lt;commit message&gt;”<br>Move the code from the s<strong>taging area</strong> to the <strong>repo-tree</strong></p><p>Some examples for git add are</p><pre><strong>git add .</strong>            # all content of the current directory<br><strong>git add</strong> ui/          # content from ui/ directory <br><strong>git add</strong> MyScript.js  # Only MyScript.js file</pre><blockquote><strong>NOTE: pathspec</strong> is a lot more powerful than the above 3 simple examples. For the sake of the size of this post, I’ll cover it in detail in a separate post.</blockquote><h3>5. Remove / Undo content</h3><p><strong>git rm </strong>&lt;pathspec&gt; <br>Remove the file from the staging area &amp; working tree.</p><p><strong>git revert </strong>&lt;commit-hash&gt;<br>It <strong>doesn’t</strong> actually remove anything. When applied on a <strong>clean working tree</strong>, The revert creates a <strong>new commit </strong>in the <strong>repo tree</strong>. This commit has the code to invert changes from the given commit-hash</p><p><strong>git commit --amend<br></strong>Adds the <strong>currently staged changes</strong> to the previous commit</p><p><strong>git reset --&lt;option&gt; &lt;pointer&gt;</strong></p><pre><strong>common option values - </strong>soft | hard | mixed(default)<br><strong>pointer values</strong> - commit-hash | HEAD</pre><p>Commands update all 3 trees as follows</p><ul><li><strong>Repo tree</strong>- The HEAD pointer of the current branch is moved to the given commit-hash. No change If there is no commit hash.</li><li><strong>staging area- </strong>effects of reset depend upon the used option <strong><br></strong> hard — the changes are lost. <br>mixed — the current content is moved to the working tree. <br>soft — no change.</li><li><strong>working tree- </strong>effects of reset depends upon the used option<strong><br></strong>hard — the changes are lost. <br>mixed— the changes from staging are added back, and any file which was created after a given commit-hash stays in a tree but becomes untracked <br>soft — no change.</li></ul><h3>5. Observe the content &amp; repo</h3><p>Following are the commands which do not affect data in any tree. <br>They are used only for observation or analysis</p><p><strong>git status<br></strong>Display the changes in the working tree, staging area, and untracked files.</p><p><strong>git log <br></strong>Show details from commit logs, there are many options to use this command the simplest is as above</p><p><strong>git show</strong> &lt;commit-hash&gt;<br>Shows the changes done in a commit</p><p><strong>git blame</strong> &lt;file name&gt; <br>Shows the author and time information for each line.</p><p><strong>git diff <br></strong>There are many variations for diff also. Here are some commonly used ones</p><ul><li><strong>git diff — </strong>Difference between the <strong>working tree</strong> &amp; <strong>repo-tree.<br></strong><em>for the file present in both working-tree &amp; staged areas, shows the difference between working and staging</em></li><li><strong>git diff --cached — </strong>Difference between the <strong>staging area &amp; repo-tree.</strong></li><li><strong>git diff HEAD — </strong>Difference between (staging area+working-tree) &amp; repo-tree. <em>for the file present in both working-tree &amp; staged areas, shows the difference between working and repo-tree</em></li><li><strong>git diff </strong>&lt;commit-hash-1&gt; &lt;commit-hash-2&gt; Difference between the two given commits</li></ul><h3><strong>6. Working with remote</strong></h3><p>The repository usually stays on the local machine as well as remote servers. Here are a few commands to manage them</p><p><strong>git remote -v </strong>— shows the remote repo associated with this local repo.<br><strong>git remote add origin</strong> &lt;URL&gt; — set a remote repo for fetch &amp; push</p><p><strong>git fetch </strong>— download objects and refs from the remote repo. Does not merge them in the local repo tree <br><strong>git pull —</strong> run git fetch followed by a git merge from origin to local branch.</p><p><strong>git push — </strong>push object and refs from local to the remote repo.<br><strong>git push origin HEAD</strong> push the current HEAD, which is the currently checked out branch, to the origin, which is the remote/ref. <br><strong>git push -u origin HEAD </strong>This is good (<em>but not limited)</em> to newly created <strong>local-only</strong> branches. With the -u option git set the tracking information for the current local branch.</p><p>Before I explain the last command in the “working with remote” section, let&#39;s look in detail at some commands we already talked about above.</p><p><strong>git checkout</strong> &lt;remote-branch-name&gt; <br>When we checkout first time, a local branch with the name same as remote-branch-name is created and its upstream is set to origin/remote-branch-name.</p><p><strong>git push -u origin HEAD <br></strong>When we push with -u, a remote branch with a name same as local-branch-name is created and upstream is set as origin/local-branch-name</p><p>In all other cases, our current branch does not have tracking information, which can be set with the below command</p><p><strong>git branch --set-upstream-to=origin</strong>/&lt;remote-branch&gt; &lt;local-branch&gt; —sets a remote counterpart to run fetch &amp; push on a given local-branch.</p><h3>7. Stash</h3><p>Git stash doesn’t fit anywhere in our 3 working trees, it’s a handy tool provided by git to stash away the changes that can be used later.</p><p><strong>git stash</strong> <br>Stash changes available in the working tree into a directory away from 3 trees.</p><p><strong>git stash pop <br></strong>Apply stashed changes to the working tree and drop the stash</p><p><strong>git stash apply<br></strong>Apply the stashed change to the working tree.</p><p><strong>git stash list <br></strong>Show all the stashes</p><p><strong>git stash drop <br></strong>Drop the last stash</p><p><strong>git stash clear<br></strong>Drop all stashes</p><p><strong>git stash show<br></strong>show the changes in the last taken stash</p><blockquote>NOTE — Once you stash, changes get added to the stash data structure at the top. so <strong><em>stash@{0}</em> </strong>always points to the last stash. You can run<strong> </strong><strong>git stash </strong><strong><em>apply, drop, and show</em> </strong>commands with<strong><em>stash@{x}</em> </strong>parameter. where x is the index starting from 0 for the latest stash.</blockquote><p>I intentionally did not cover <strong>rebase </strong>because I’ve had many experiences of developers using it incorrectly with force push and causing trouble for everyone else while merging. I would suggest avoiding rebase until you know what you are doing.</p><p>This is all as far as a quick cookbook is concerned. I barely scratched the surface here. but whatever is discussed above, is more than enough for everyday use. <br>However, every command has a plethora of options, Let me know in the comments if you want me to cover any of the above commands in detail. Thanks for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=81dceb1d284d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Git Series (1) — The Architecture &  Internals!]]></title>
            <link>https://medium.com/@manvendrapsingh/git-series-1-the-architecture-internals-3740419629d0?source=rss-17126b6e03------2</link>
            <guid isPermaLink="false">https://medium.com/p/3740419629d0</guid>
            <category><![CDATA[git-architecture]]></category>
            <category><![CDATA[staging-area]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[git-internals]]></category>
            <dc:creator><![CDATA[Manvendra P Singh]]></dc:creator>
            <pubDate>Sun, 29 May 2022 17:35:06 GMT</pubDate>
            <atom:updated>2023-02-20T21:53:29.117Z</atom:updated>
            <content:encoded><![CDATA[<h3>Git Series (1) — The Architecture &amp; Internals!</h3><p>Since the dawn of software engineering, managing the source code had been a great challenge for engineers. A plethora of <strong>Source code management (SCM) systems &amp; Version Control Systems </strong>(<strong>VCS</strong>) have been developed in the search for the best one.</p><p>In my short career, I have used the useless <strong>Microsoft VSS</strong>, the ubiquitous <strong>CVS</strong>, the slightly better <strong>SVN, </strong>and the proprietary <strong>IBM-ClearCase</strong>. But none provided as comprehensive a solution as the one and only <strong>GIT</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HC7Rgmabrc018OM-mq0Slw.png" /></figure><h3>The Evolution</h3><p>The quest for the best SCM ended in <strong>2005</strong> when <strong>Linus Torvalds </strong>gave another opensource gift to the world. It was a source code management system (SCM) that he created to manage the <strong>distributed development</strong> of the Linux kernel&#39;s<strong> </strong>huge codebase<strong>.</strong> He named the SCM <strong>GIT</strong>.</p><p>The industry embraced <strong>GIT</strong> with open arms. SAAS providers like <strong>GitHub</strong>, <strong>BigBucket</strong> &amp; <strong>GitLab</strong> added more bells and whistles on top, and within a few years, <strong>GIT</strong> became the ubiquitous source control solution.</p><h3>Git Architecture</h3><p>Git is different from all other SCMs in many aspects. To understand better, let&#39;s look at a 30,000 feet view of the architecture of SCMs.</p><h4>The Terminology</h4><p>Before we start, here is an oversimplified explanation of a few SCM&#39;s related terminologies.</p><ul><li><strong>Tree<br></strong>A codebase is usually a collection of files &amp; folders. It can be represented as a Tree data structure, having the Project folder as the root node and files (blobs) as leaves with subfolders as intermediate nodes. For our further discussion, we will refer to the codebase as a <strong>Tree.</strong></li><li><strong>Working Tree</strong><br>The source code that we work on and make changes on. All the code that we change or add, is saved in the working tree.</li><li><strong>Repo Tree</strong> <br>This is the source code that is kept for later reference. The data in the repo tree <strong>is uneditable. </strong>To make any change, you have to checkout, edit, and then commit the file.</li><li><strong>Commit </strong><br>An action to put the code in the repo tree.</li><li><strong>Checkout <br></strong>An action to bring the code from the repo tree to the working tree.</li></ul><h4>Two Tree Architecture</h4><p>This is is how most of the SCM worked. We make changes in a working tree and commit them to the repo. Repo is usually somewhere on a remote server. Right after the code commit, new changes become available for everyone.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8QeEu_-gGj0yhD0lL5S-ew.png" /></figure><h4>Three Tree Architecture</h4><p>Linus introduced many distinctive elements in GIT design, making it different from all other systems, The 2 prominent elements were <strong>the</strong> <strong>local Repository</strong> &amp; <strong>The Staging area</strong>.</p><ul><li><strong>Local Repository <br></strong>Git maintains a full repository on our machines, called a <strong>local repository</strong>. Developers <strong>commit to</strong> and <strong>checkout from</strong> the <strong>local repository</strong>. If needed, these commits can be pushed to remote servers. But each machine works as a full repository, containing all information, such as <strong>branches, commits, logs, tags, etc.</strong></li><li><strong>Staging Area /Cache <br></strong>Linus also created a <strong>cache</strong> that holds the code, that a developer <strong>intends to commit </strong>to the repo tree. <br>Any change for local development can be done onto a working tree without worrying about mixing it with commit code. Once ready, changes that are needed can be explicitly added to this <strong>cache,</strong> only the changes that are added to the cache will be committed to the local repo.</li></ul><p>So a typical workflow in a GIT will look like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FyoP6im0sfiDkvIcbFq8HA.png" /><figcaption>Git 3 Tree design</figcaption></figure><h4>Adding Remote Tier</h4><p>Usually, the goal of working with SCM is, to store the code on a reliable server machine, not on a developer machine. Therefore, we finally <strong>PUSH</strong> the local repo code to <strong>a remote server</strong>. Thus, the complete workflow looks like below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OVcq3Ysyufp1wcY9HGmr8Q.png" /><figcaption>Git 4-tier design</figcaption></figure><h3>Additional Key components</h3><p>If you require just a high-level overview, you can very well stop reading here. however, for developers who use git for day-to-day work, below are a few more useful concepts</p><ul><li><strong>SHA-1</strong> <strong>Hash<br></strong>This is the key concept that all things in Git revolve around. Think of it as a <strong>40-character hexadecimal SHA </strong>string generated by passing “some value” to a cryptographic function.<br> <br><strong> — A file hash </strong>is calculated upon a commit, by applying the checksum on the <strong>uncompressed </strong>file content<strong>. </strong><br> —<strong> A Commit hash,</strong> however, contains more information than just the files, like the <strong>commit author, commit time,</strong> etc. Commit hash is calculated by combining all the changes in a commit and other commit information. The<strong> Commit hash </strong>is<strong> </strong>unique within the repository.</li><li><strong>Blob <br></strong>The content of a file along with file metadata is called a <strong>blob</strong>. On commit, a <strong>file hash</strong> is calculated on an <strong>uncompressed</strong> file. The file is then <strong>compressed</strong> using <strong>zlib,</strong> and data is stored in the <strong>.git/objects/ </strong>directory, having the file’s hash as the filename.</li><li><strong>HEAD Pointer<br></strong>It is a pointer that <strong>directly or indirectly</strong> points to a commit hash in the repo tree. <br><strong>Indirect pointer</strong> — When we checkout a branch, the HEAD pointer holds the <strong>reference to the branch.</strong> But a branch ref points to the last commit hash on the tip of the branch. Making HEAD indirectly pointing to that commit hash<br><strong>Direct Pointer</strong>-. Git allows us to checkout by a commit hash instead of a branch name, When we do this, our tree goes into <strong>Detached HEAD State</strong>, In this scenario, the HEAD pointer points directly to a commit hash.</li><li><strong>Repo Metadata<br></strong>As said above, Git brings the whole repository to every developer machine, How Git does this, is by creating a <strong>.git</strong> folder right under the project’s root folder. All git data is kept in this directory.</li></ul><h3>.git folder content (Advanced)</h3><p>Below is an example of the content inside a <strong>.git</strong> directory. <br>This was a repo with 1 file and 1 commit (no tags).</p><pre>.git<br>├── COMMIT_EDITMSG<br>├── HEAD<br>├── config<br>├── description<br>│ <br>├── hooks<br>│   ├── hook scripts <br>│ <br>├── index<br>│ <br>├── info<br>│   └── exclude<br>│ <br>├── logs<br>│   ├── HEAD<br>│   └── refs<br>│       └── heads<br>│           └── master<br>│ <br>├── objects <br>│   ├── 05<br>│   │   └── f683xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx2774<br>│   ├── 1b<br>│   │   └── 0f93xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx4e21<br>│   ├── 37<br>│   │   └── 40b8xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5510<br>│   ├── info<br>│   └── pack<br>│ <br>└── refs<br>    ├── heads<br>    │   └── master<br>    └── tag</pre><p>Here is a high-level overview of the content.</p><ul><li><strong>description- </strong>This<strong> </strong>file<strong> </strong>contains the basic description of the repo.</li><li><strong>config- </strong>This<strong> </strong>file<strong> </strong>contains the git configuration specific for this repository,</li><li><strong>HEAD- </strong>This<strong> </strong>file<strong> </strong>contains either the commit hash or branch ref</li><li><strong>COMMIT_EDITMSG- </strong>This<strong> </strong>file contains the message from the last commit.</li><li><strong>hook- </strong>This<strong> </strong>folder contains pre and post-scripts that you want to execute on various events, like commit/push/merge/rebase, etc.</li><li><strong>ref- </strong>This<strong> </strong>folder<strong> </strong>contains the files storing the commit hash <br>- for <strong>local</strong> branches under the <strong>heads </strong>folder, <br>- for <strong>remote</strong> branches under the <strong>origin</strong> folder<br>- for releases under<strong> </strong>the<strong> tags </strong>folder<strong>.</strong></li><li><strong>logs- </strong>This<strong> </strong>folder contains one file for HEAD and files for the local and remote branch contains a <strong>hash, time, user,</strong> and other important information for all the git events.</li><li><strong>object-</strong>This<strong> </strong>folder contains <strong>blobs, trees, </strong>&amp;<strong> commit </strong>files<strong> </strong>for each commit</li></ul><p>This is all for the first post in the Git series. Let me know your response in the comments or clap. In the <a href="https://medium.com/@manvendrapsingh/git-series-2-command-line-cheat-sheet-81dceb1d284d">next post</a>, we will discuss all the commands needed to perform day-to-day operations on git.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3740419629d0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Serverless Framework —  Develop, deploy, and troubleshoot your serverless!]]></title>
            <link>https://medium.com/@manvendrapsingh/serverless-framework-the-kubernetes-of-faas-a-little-bit-cc6d53646489?source=rss-17126b6e03------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc6d53646489</guid>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[serverless-framework]]></category>
            <category><![CDATA[aws-cloudformation]]></category>
            <category><![CDATA[function-as-a-service]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <dc:creator><![CDATA[Manvendra P Singh]]></dc:creator>
            <pubDate>Tue, 17 May 2022 23:47:01 GMT</pubDate>
            <atom:updated>2022-06-07T18:00:13.905Z</atom:updated>
            <content:encoded><![CDATA[<h3>Serverless Framework — <strong>Develop, deploy, and troubleshoot your serverless!</strong></h3><p>Serverless is the industry&#39;s buzzword these days, It doesn’t solve all the problems, but enough to become important. What exactly is server-less programming and how did we get where we are today, I have covered it <a href="https://medium.com/@manvendrapsingh/rpc-to-serverless-how-did-we-get-here-8c722441fe10">here</a>.</p><h3>What this post is about?</h3><p>This post is not about when you should or should not use a serverless. I’m assuming you’ve decided to use serverless. This post is also not about which cloud solution should you use, <strong>AWS Lambda, Google Cloud Functions,</strong> or <strong>Azure Functions. </strong>I’m assuming you’ve chosen AWS Lambda. This is not even about comparing <strong>AWS SAM</strong> or <strong>AWS CDK</strong> &amp; <strong>Serverless framework.</strong></p><p>This post is about covering all the steps and showing how easy is to have an AWS lambda up and running with Serverless Framework.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KrpzJbN1YeXswR3lDgGqdA.png" /></figure><h3>Install Serverless</h3><p>The easiest way to install Serverless is by running the following command, assuming you already have Node installed on your machine.</p><p>If you don’t have <strong>Node JS</strong> installed, install it from<strong> </strong><a href="https://nodejs.org/en/"><strong>here</strong></a><strong>.</strong> After installing <strong>Node</strong>, run the following command to install <strong>Serverless</strong></p><pre>npm install -g serverless</pre><h3>Create an AWS IAM user for Serverless</h3><p>Serverless Framework deploys the lambda using the <strong>policy attached</strong> to the <strong>IAM</strong> <strong>credentials</strong> in your <strong>AWS CLI profile. </strong>So we need an IAM user.</p><h4>Create a policy (Optional but recommended)</h4><p>Theoretically, you can configure your <strong>AWS CLI</strong> with an IAM user having <strong>Administrator Access</strong>, but it is not recommended. It&#39;s advisable to fine-tune the level of access to the minimum required levels.</p><p><strong>Serverless Framework </strong>requires access to the following AWS services</p><pre><strong>S3</strong> - to upload &amp; store serverless artifacts and Lambda code<br><strong>EC2</strong> - to execute Lambda in VPC<br><strong>IAM</strong> - to manage policies for the Lambda IAM Role<br><strong>Lambda</strong> - to manage Lambda functions<br><strong>API Gateway</strong> - to manage API endpoints<br><strong>CloudFormation</strong> - to create changes and update stack <br><strong>CloudWatch Logs</strong> - to store Lambda execution logs<br><strong>CloudWatch Events</strong> - to manage CloudWatch event triggers</pre><p>Let’s create <strong>a custom policy </strong>and attach it to our <strong>IAM user</strong>. This might be the only time you need to log in to the <strong>AWS console</strong>. <br>To create a custom policy, Go into <strong>IAM</strong>, click <strong>policies, </strong>click on <strong>create policy </strong>and go to the <strong>JSON</strong> tab.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ai2McqJhjvuvWF548K2_oA.png" /><figcaption><strong>Create IAM policy</strong></figcaption></figure><p>Copy below JSON in the above section.</p><pre>{<br>    &quot;Version&quot;: &quot;2012-10-17&quot;,<br>    &quot;Statement&quot;: [<br>        {<br>            &quot;Effect&quot;: &quot;Allow&quot;,<br>            &quot;Action&quot;: [<br>                &quot;cloudformation:*&quot;,<br>                &quot;s3:*&quot;,<br>                &quot;logs:*&quot;,<br>                &quot;iam:*&quot;,<br>                &quot;apigateway:*&quot;,<br>                &quot;lambda:*&quot;,<br>                &quot;ec2:DescribeSecurityGroups&quot;,<br>                &quot;ec2:DescribeSubnets&quot;,<br>                &quot;ec2:DescribeVpcs&quot;,<br>                &quot;events:*&quot;<br>            ],<br>            &quot;Resource&quot;: [<br>                &quot;*&quot;<br>            ]<br>        }<br>    ]<br>}</pre><blockquote><strong>NOTE</strong>: I am not an AWS expert, I wasn’t sure about fine-grained policies, so I used <strong>*</strong> for many services. Let me know in the comments if something can be finetuned</blockquote><h4>Create User</h4><p>Create a new user with <strong>credential type</strong> as <strong>Access Key</strong> as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JyHSHpPD5DLyzB0TLmvlYg.png" /><figcaption><strong>Add IAM User</strong></figcaption></figure><p>As shown below, On the second screen, click on the last tab and attach the policy that we created in the above step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2lF1LTDAXgdjE6MNfj2wVg.png" /><figcaption>Attach policy to the user</figcaption></figure><p>Follow the instructions, until the last step. In the last step, make sure to<strong> download the Key and Secret CSV. </strong>It&#39;s required in the later steps.</p><h3>Install AWS CLI</h3><p>As I mentioned above, the Serverless framework requires a locally configured AWS CLI, Let’s install AWS CLI on our machines. <a href="https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html">Here</a> are the installation instructions for <strong>Mac, Linux &amp; Windows</strong>.</p><h3>Configure CLI</h3><p>After AWS CLI is installed, type the following command and use the ID and secrets from the CSV that we downloaded in one of the above steps.</p><pre>aws configure </pre><h3>Create a java project</h3><p>Now all the setup is done. <br>The Serverless Framework will create all boilerplate code including the request-response java classes, sample handler class, pom files, and a lot more, by just the following command. (<em>use anything you like as name &amp; path)</em></p><pre><strong>serverless</strong> <strong>create</strong> \<br>  -t <strong>aws-java-maven</strong> \<br>  --<strong>name</strong> your-lambda-api \<br>  --<strong>path</strong> aws-java-your-lambda-api</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yilhP5kRam-szts-zeZfWQ.png" /></figure><p>The serverless framework creates the following directory and files with prepopulated details.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tSeew7g41ft0S5A017JnrA.png" /></figure><p>And now we have a complete project setup to add our business logic.</p><h3>Update Code</h3><p>Open the project in your favorite IDE. Update the highlighted lines in your pom.xml with your artifact details.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LOgW2McoAD0i32botoTKxw.png" /></figure><p>This post is only about using the serverless framework and not about coding the lambda, so I will not discuss the content of these java files, I’ve another post <strong>coding a lambda</strong> that covers it.</p><h3>Configuring the lambda</h3><p>The serverless framework reads the <strong>serverless.yml </strong>file<strong> </strong>to create a <strong>cloud formation template </strong>for us and pushes it to AWS to spin up all required infrastructure. That is the file we need to configure.</p><p>The actual content in this yml will depend upon what your lambda function is doing, how many services it requires access to, etc. The good thing is, that the YML file created by the framework has all the instructions as commented content as below screenshot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YNVEvc41SWLEdK1qemhv-Q.png" /><figcaption>Default serverless.yml created by framework</figcaption></figure><h3>Deploy the lambda</h3><p>Once you are done configuring this YML as per your requirement and adding code for your lambda, run the following command to create the artifact. For this example, I’m using the default code from the framework, without any change</p><pre>mvn package</pre><p>Now you can run just one below command and serverless will do the rest.</p><pre>serverless deploy</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ee62UUTA26q9qmiXZRe5Ng.png" /></figure><h3>Test your lambda</h3><p>There are many ways to test, from CLI, to curl but the easiest way is, to log in to the AWS console and go to the lamda section. you will see your lambda, click on Test. <br>This default hello word lambda is created by serverless.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lvqRbfl4O0Io2E2nTipwsw.png" /></figure><p>This was a quick summary for using the Serverless framework, explore the default YML file provided in the code, it has a good amount of documentation to configure for your need.</p><p>Check out my other post to see the actual code for writing lambdas. Lets me know your feedback about this post in the comments.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc6d53646489" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>