<?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>Forem: Himanshu Garg</title>
    <description>The latest articles on Forem by Himanshu Garg (@mercury).</description>
    <link>https://forem.com/mercury</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%2F427649%2Fb9ab2d31-9599-4bdd-b445-a8472804d1dd.png</url>
      <title>Forem: Himanshu Garg</title>
      <link>https://forem.com/mercury</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mercury"/>
    <language>en</language>
    <item>
      <title>Schedule Tasks Using GitHub Actions</title>
      <dc:creator>Himanshu Garg</dc:creator>
      <pubDate>Thu, 29 Oct 2020 18:34:00 +0000</pubDate>
      <link>https://forem.com/mercury/schedule-tasks-using-github-actions-3g52</link>
      <guid>https://forem.com/mercury/schedule-tasks-using-github-actions-3g52</guid>
      <description>&lt;p&gt;GitHub Actions can do a lot of things and can be triggered by different methods. Typically, you want a workflow to be executed on a push or a pull-request or an issue. &lt;/p&gt;

&lt;p&gt;So, Where does scheduling a task fit in? &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For Tracking good health of your software, you can perform scheduled testing. &lt;/li&gt;
&lt;li&gt;Publishing the &lt;strong&gt;Nightly Build&lt;/strong&gt; of your library, framework or software. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Scheduling doesn't require a change to be made into code. It will keep running on the unchanged or changed code on scheduled time. Scheduling a GitHub Workflow is done using &lt;strong&gt;cron&lt;/strong&gt;. For those who don't know what cron is,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The software utility &lt;strong&gt;cron&lt;/strong&gt; also known as a &lt;strong&gt;cron job&lt;/strong&gt; is a time-based job scheduler in Unix-like computer operating systems. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here is an example of scheduling a task.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Give the workflow a name. &lt;/span&gt;
&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Nightly Build&lt;/span&gt;

&lt;span class="c1"&gt;# Trigger Workflow every midnight UTC&lt;/span&gt;

&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;schedule&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;cron&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;0&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;0&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;*&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;*&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;*'&lt;/span&gt;  
  &lt;span class="na"&gt;workflow_dispatch&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; 
&lt;span class="c1"&gt;#workflow_dispatch helps run workflow anytime with a single click. &lt;/span&gt;

&lt;span class="c1"&gt;# A workflow run is made up of one or more jobs that can run sequentially or in parallel&lt;/span&gt;
&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Scheduling&lt;/span&gt;
    &lt;span class="c1"&gt;# make sure to run this action in Linux env (say ubuntu)&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="c1"&gt;# Steps represent a sequence of tasks that will be executed as part of the job&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Write all the steps you want to schedule&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For creating nightly build use &lt;a href="https://github.com/actions/create-release"&gt;create-release&lt;/a&gt; action with the above workflow. Make sure to use a dynamic naming format while creating tags &amp;amp; to set &lt;code&gt;prerelease: true&lt;/code&gt;. Use the YYYYMMDD date format in the tag&lt;/p&gt;

&lt;p&gt;Defining a cron schedule is a tougher task than one might expect. &lt;a href="https://crontab.guru/"&gt;crontab.guru&lt;/a&gt; is a website which converts cron schedule into human-readable format. eg. &lt;code&gt;0 0 * * *&lt;/code&gt; means midnight according to UTC.  &lt;/p&gt;

</description>
      <category>github</category>
      <category>devops</category>
      <category>linux</category>
      <category>bash</category>
    </item>
    <item>
      <title>Continuous Deployment, The Easy Way</title>
      <dc:creator>Himanshu Garg</dc:creator>
      <pubDate>Sun, 23 Aug 2020 08:01:12 +0000</pubDate>
      <link>https://forem.com/mercury/continuous-deployment-the-easy-way-8j2</link>
      <guid>https://forem.com/mercury/continuous-deployment-the-easy-way-8j2</guid>
      <description>&lt;p&gt;Github Actions let you automate all your boring stuff. In this blog, You'll see How you can automate deployment on any Personal Server, Virtual Private Server(VPS) or Virtual Machine(VM). Every Major Cloud Service Provider provide auto-deployment which is only restricted to web hosting service. &lt;/p&gt;

&lt;p&gt;Most of you want to always want to automate deployment on your Personal Server and VPS/VM but afraid of the hectic process. Here is how you can easily do it. It is not restricted to JavaScript, Python, Ruby. You can automate the deployment of anything from ML Model to Game's new version.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's Start 🚀
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We will use SSH for Automation. Make sure you have Port 22 open your Server. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Let's create new SSH Key for Github Actions in server and &lt;br&gt;
copy it.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Save it with a recognizable name eg. github_actions &lt;/span&gt;
&lt;span class="c"&gt;# Do give it a strong passphrase&lt;/span&gt;
ssh-keygen &lt;span class="nt"&gt;-t&lt;/span&gt; rsa &lt;span class="nt"&gt;-b&lt;/span&gt; 4096 &lt;span class="nt"&gt;-C&lt;/span&gt; &lt;span class="s2"&gt;"your_email@example.com"&lt;/span&gt;

&lt;span class="c"&gt;# Add Key to Authorize Keys. So, We can connect to it. &lt;/span&gt;
&lt;span class="nb"&gt;cat&lt;/span&gt; ~/.ssh/github_actions.pub &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; ~/.ssh/authorized_keys

&lt;span class="c"&gt;# Copy content of Public Key. &lt;/span&gt;
clip &amp;lt; ~/.ssh/github_actions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Navigate to Secrets Option in your Github Repository Settings. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Click on 'New Secret' to add new Secrets. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;KEY&lt;/code&gt; - the content of ssh private key. eg. raw content of ~/.ssh/github_actons. Copied in the Previous Step. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;HOST&lt;/code&gt; - ssh host&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PORT&lt;/code&gt; - ssh port, default is 22&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;USERNAME&lt;/code&gt; - ssh username&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PASSWORD&lt;/code&gt; - ssh password&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PASSPHRASE&lt;/code&gt; - the passphrase is usually to encrypt the private key.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to the 'Actions' Tab in your repository and click on 'set up a workflow yourself'. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use &amp;amp; Personalize below given .yml file.&lt;br&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Give the workflow a name. &lt;/span&gt;
&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Continuous Deployment&lt;/span&gt;

&lt;span class="c1"&gt;# Trigger Workflow on master/production branch. &lt;/span&gt;

&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;branches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt; &lt;span class="nv"&gt;master&lt;/span&gt; &lt;span class="pi"&gt;]&lt;/span&gt;
  &lt;span class="na"&gt;pull_request&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;branches&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt; &lt;span class="nv"&gt;master&lt;/span&gt; &lt;span class="pi"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# A workflow run is made up of one or more jobs that can run sequentially or in parallel&lt;/span&gt;
&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="c1"&gt;# This workflow contains a single job called "deploy"&lt;/span&gt;
  &lt;span class="na"&gt;deploy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Deploy&lt;/span&gt;
    &lt;span class="c1"&gt;# make sure to run this action in Linux env (say ubuntu)&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="c1"&gt;# Steps represent a sequence of tasks that will be executed as part of the job&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;executing remote ssh commands using password&lt;/span&gt;
      &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;appleboy/ssh-action@master&lt;/span&gt;
      &lt;span class="c1"&gt;# all environment variables needed to connect with Server is placed in with. &lt;/span&gt;
      &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.HOST }}&lt;/span&gt;
        &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.KEY }}&lt;/span&gt;
        &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.USERNAME }}&lt;/span&gt;
        &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.PASSWORD }}&lt;/span&gt;
        &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.PORT }}&lt;/span&gt;
        &lt;span class="na"&gt;passphrase&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ secrets.PASSPHRASE }}&lt;/span&gt;
        &lt;span class="c1"&gt;# Here will go script required for auto-deployment.&lt;/span&gt;
        &lt;span class="c1"&gt;# eg. For Django server using Gunicorn. &lt;/span&gt;
        &lt;span class="c1"&gt;# Website Server is stored in thewebsite folder&lt;/span&gt;
        &lt;span class="na"&gt;script&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
        &lt;span class="s"&gt;echo "Changing Directory to thewebsite"&lt;/span&gt;
        &lt;span class="s"&gt;cd thewebsite/&lt;/span&gt;
        &lt;span class="s"&gt;echo "Pulling From Github"&lt;/span&gt;
        &lt;span class="s"&gt;git pull -q origin master&lt;/span&gt;
        &lt;span class="s"&gt;echo "Git Pull Complete"&lt;/span&gt;
        &lt;span class="s"&gt;echo "Restarting Service"&lt;/span&gt;
        &lt;span class="s"&gt;sudo systemctl daemon-reload&lt;/span&gt;
        &lt;span class="s"&gt;sudo systemctl restart gunicorn&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This above will automatically deploy any change in Django. You can change the script according to the language and framework of your choice. &lt;/p&gt;

&lt;p&gt;You can also use a locally saved script by giving the path of the script instead of writing here.  eg.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;script&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/home/username/deploy.sh&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion 📓
&lt;/h2&gt;

&lt;p&gt;You can automate anything with this workflow with a change in the master/production branch as a trigger.  &lt;/p&gt;

&lt;p&gt;This workflow is designed using &lt;code&gt;appleboy/ssh-action&lt;/code&gt;. You can read more about arguments and documentation &lt;a href="https://github.com/appleboy/ssh-action"&gt;here&lt;/a&gt;. &lt;/p&gt;

</description>
      <category>github</category>
      <category>devops</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
