<?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 Eugenio Di Tullio on Medium]]></title>
        <description><![CDATA[Stories by Eugenio Di Tullio on Medium]]></description>
        <link>https://medium.com/@ditullioeugenio?source=rss-2a5b00fdc4f0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*CFCiN0UtyMB4QFtDRbgsDw.jpeg</url>
            <title>Stories by Eugenio Di Tullio on Medium</title>
            <link>https://medium.com/@ditullioeugenio?source=rss-2a5b00fdc4f0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 25 Nov 2026 00:52:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ditullioeugenio/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[How To Create Custom CI/CD With Github and Docker]]></title>
            <link>https://medium.com/rewrite-tech/how-to-create-custom-ci-cd-with-github-and-docker-495e8ff87c7e?source=rss-2a5b00fdc4f0------2</link>
            <guid isPermaLink="false">https://medium.com/p/495e8ff87c7e</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[raspberry-pi]]></category>
            <category><![CDATA[self-hosting]]></category>
            <category><![CDATA[ci-cd-pipeline]]></category>
            <dc:creator><![CDATA[Eugenio Di Tullio]]></dc:creator>
            <pubDate>Wed, 13 Apr 2022 12:00:31 GMT</pubDate>
            <atom:updated>2024-12-04T12:37:08.194Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_z59rgNYNJRyle0fIIHSOA.png" /></figure><p>I have a lot of side projects. Most of them are never really finished, but nevertheless, I like to publish them to show them to friends and family.</p><p>In most cases these projects are not worthy of some professional platform where to host them, so I end up using some free service like Heroku or similar.</p><p>Of course, the free tier for this kind of services usually has some strong limitations that can be annoying, so I’m always looking for something better.</p><p>But what if I could host my projects myself?</p><p>What if I made this… my new side project?</p><p>Self-hosting my own apps would bring some nice advantages, right?</p><ul><li>Free hosting</li><li>No limitations</li><li>Full control over the hardware</li></ul><p>But, of course, is not all peaches:</p><ul><li>Security</li><li>Scalability</li><li>Malicious attacks</li><li>Hardware, energy, and bandwidth costs</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gDxgGkKOidCicHPt" /><figcaption>Photo by <a href="https://unsplash.com/@miracleday?utm_source=medium&amp;utm_medium=referral">Elena Mozhvilo</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>So why do it?</p><p>For the science of course!</p><p>In general, I like the idea of self-hosting my services, although I see the advantages of not having to care for the technicalities that come with handling the “bare metal”. But doing so, for some toy projects without so much criticality, is a fantastic learning opportunity.</p><p>My usual go-to solution is to dockerize my service and run it in my RasperryPi.</p><p>This works very well in a big variety of situations, but having to always deploy my services manually, becomes boring very quickly.</p><ul><li>Wouldn’t it be better if I could have my code automatically deployed to my RaspberryPi without my intervention, like in any respectable CI/CD system? Of course, it would! So, let’s try to build one.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6XHfLhgyyOZsLb9-" /><figcaption>Photo by <a href="https://unsplash.com/@viazavier?utm_source=medium&amp;utm_medium=referral">Laura Ockel</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>The setup</h3><p>• A GitHub repository</p><p>• A local clone of the repository</p><p>• A local Docker instance</p><p>• An instance of Docker, always available and accessible via SSH (As mentioned, mine is on my RasperryPi)</p><p>• A simple Node app to test the process</p><h3>Expected Result</h3><p>After every push of my code to the master branch of my GitHub repository, a new version of my app will be deployed to my Docker instance, which runs in my Raspberry Pi</p><h3>Requirements</h3><ul><li>Basic knowledge JavaScript and NodeJS for the app used as an example.</li><li>Basic knowledge of Git and GitHub to be able to commit the code to GitHub.</li><li>Basic knowledge of Bash to be able to understand the commands needed for the automations.</li><li>Basic knowledge of YAML to be able to understand how to read the configuration of the GitHub pipeline.</li><li>Basic knowledge of docker and the image/container system.</li></ul><p>This article will focus on the main topic and provide links for some more complex topics that are referred in the chapters.</p><p>A complete example of the final project is provided at</p><p><a href="https://github.com/Eux86/remote-docker-deploy-action-example">GitHub - Eux86/remote-docker-deploy-action-example</a></p><h3>Step 1: Demo App</h3><p><em>Note: from this point on I will assume that a new GitHub repository was already created and that it was already cloned on the local development environment</em></p><p>Let’s create a small NodeJS app that will help us testing the deploy process.</p><ul><li>Create project:</li></ul><pre>npm init</pre><ul><li>Let’s install “express” so we can build an example server with it</li></ul><pre>npm i express</pre><ul><li>Let’s setup a basic express server</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ffc98cd4ba478840538c7ebd9211ec81/href">https://medium.com/media/ffc98cd4ba478840538c7ebd9211ec81/href</a></iframe><ul><li>Create a start script</li></ul><pre>&quot;start&quot;: &quot;node index.js&quot;</pre><ul><li>And finally let’s run the script</li></ul><pre>npm run start</pre><p>After this we should be able to connect to <a href="http://localhost:3000/">http://localhost:3000</a> and see:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/376/1*qRWFaIMcQ3ZzlLNWrblAAA.png" /></figure><h3>Step 2: Dockerize the app</h3><p>Let’s now create a basic Docker image that runs the app.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f441a6bd526c8f119c457c912af763c2/href">https://medium.com/media/f441a6bd526c8f119c457c912af763c2/href</a></iframe><p>Note that in my case I’m using the <em>arm32v7</em> version of the node:lts-alpine which is built to run on the RaspberryPI arm processor. A different base image should be used if a different architecture is used. A list of all the available targets is available at: <a href="https://hub.docker.com/_/node?tab=tags">https://hub.docker.com/_/node?tab=tags</a></p><p><a href="https://hub.docker.com/_/node?tab=tags">node Tags | Docker Hub</a></p><p>Let’s now build the image:</p><pre>docker build . -t test-server</pre><p>And run it:</p><pre>docker run --name test-server -d -p 3000:3000 test-server</pre><p>If everything goes as expected, now we will have a local docker container running our test app available navigating at <a href="http://localhost:3000/">http://localhost:3000</a> with our browser.</p><p>Good!</p><p>Let’s now make it more interesting.</p><h3>Step 3: Deploy on the remote Docker</h3><p>First things first: if we don’t have it already, we need to configure a SSH key-based authentication for our remote environment.</p><p>I’m not going to go into details about this since it is thoroughly explained here:</p><p><a href="https://www.ssh.com/academy/ssh/public-key-authentication#setting-up-public-key-authentication-for-ssh">Public Key authentication - security, automatic log-in, no passwords.</a></p><p>Long story short, this allows us to access a remote SSH environment without an authentication prompt and will allow us to automate this process into our ci/cd system.</p><p>Now that we have access to the remote system and we also tested that the docker container is working in our local environment, let’s publish it to our remote docker instance. To do so we use the “DOCKER_HOST” environment variable, which tells docker that every command will be redirected to the remote instance:</p><pre>DOCKER_HOST=ssh://user@remote.address docker run --name test-server -d -p 3000:3000 test-server</pre><p>If everything goes as expected, we should be able to see our demo app when we access <a href="http://remote.address:3000/">http://remote.address:3000</a></p><h3>Deploy commands</h3><p>Great, we were able to manually deploy our container on our remote docker instance. Now we want to make this step as simple as possible, so that we will be able, later, to automate it.</p><p>Let’s create a new <em>deploy</em> command in our package.json file. This file will take care of all the needs for publishing the project to our remote docker instance. It should be able to:</p><ul><li>build a new updated image every time we change our code</li><li>stop and remove an old container that was previously created</li><li>run a new container with the new updated image</li></ul><p>To do so, let’s create a set of command that will deal with each of these tasks:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/79e99365e201c20dbff047ac52ded7e0/href">https://medium.com/media/79e99365e201c20dbff047ac52ded7e0/href</a></iframe><p>Let’s analyse each one of these commands:</p><p><strong>deploy: </strong>only executes all the other commands in the right sequence.</p><p><strong>deploy:docker-build: </strong>executed the build process and gives a name to the image (in this case test-server)</p><p><strong>deploy:start-container:</strong> runs an image of our test-server image, names it (again using test-server, but could be anything) and configures it to expose the internal container’s port 3000, to the external port 3000. More details on publishing ports in docker can be found <a href="https://docs.docker.com/config/containers/container-networking/">here</a>.</p><p><strong>deploy:remove-container:</strong> removes a previously created container. The first time we run the deploy, no container would be there and the command would exit with an error code, breaking our process. To avoid this we add a “|| true” at the end, which bypasses the error.</p><p><strong>deploy:stop-container:</strong> very similarly to the deploy:remove-container command, this one tries to stop a container and if no container is found it does not exit with an error code.</p><p>With this set of commands, we can easily deploy our project just running a single command:</p><pre>DOCKER_HOST=ssh://user@remote.address npm run deploy</pre><p>Let’s give it a try and, if everything is working on our machine, it’s time to automate the same process on GitHub!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*UsscK8hggNINIaqK" /><figcaption>Photo by <a href="https://unsplash.com/@realaxer?utm_source=medium&amp;utm_medium=referral">T K</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Step 4: GitHub actions</h3><p><em>Note: from this point on I will assume that a new GitHub repository was already created and that it was already cloned on the local development environment</em></p><p>Let’s now put it all together in a GitHub Action configuration file which will be executed every time we publish something new on our repository.</p><p>Create a new file in the folder:</p><pre>project_root/.github/workflows/any-name-you-want.yaml</pre><p>The file can have any name, but must have the “.yml” extension</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f3197de6d1e39eb722332a6188bf9e37/href">https://medium.com/media/f3197de6d1e39eb722332a6188bf9e37/href</a></iframe><p>At first, this can look very complicated, but if we analyse it step by step it will make a lot more sense:</p><p><strong>name:</strong> the name of our pipeline. This will be shown in The GitHub interface later.</p><p><strong>on:</strong> The git event that we want to be triggering our pipeline</p><p><strong>jobs:</strong> Groups together different phases of our deploy pipeline. In this case I’m on going to use a single job for simplicity.</p><p><strong>build</strong>: The name we give to a job</p><p><strong>runs-on:</strong> The base container on which we want to execute the commands that will build and deploy our app. In this case it’s a container running the latest ubuntu version.</p><p><strong>steps:</strong> Literally, the steps needed to build and publish our app.</p><p><strong>uses: actions/checkout@v2<br> </strong>Uses a GitHub action already prepared for a certain Task. In this case this action will checkout our code into the container where we are running the build process, so that we can work on it. This action is publicly available on GitHub at <a href="https://github.com/actions/checkout/tree/v2">https://github.com/actions/checkout/tree/v2</a></p><p><strong>name: </strong>Similarly as before, but in this case the name of a step of our pipeline. This section will contain attributes such as</p><p><strong>run:</strong> The command to run.</p><p><strong>env:</strong> environment variables to be used in the environment where the script is going to run (with the run command).</p><h3>The Configure SSH connection step</h3><p>This is the most cryptic part of the configuration and it’s the one that contains all the magic that allows GitHub to “talk” with our remote docker instance using SSH. Let’s analyse it line by line:</p><pre>mkdir ~/.ssh</pre><p>Creates a directory called <em>.ssh </em>inside the current user’s home directory. This is the place where SSH checks for all possible configuration and we make it available in case it’s not there already.</p><pre>ssh-keyscan -p ${{ secrets.SSH_PORT }} -t rsa ${{ secrets.SSH_HOST }} &gt;&gt; ~/.ssh/known_hosts</pre><p>The ssh-keyscan command collects a public key from a SSH server. This process is needed to let the SSH agent existing in the GitHub action container the public key of our remote system. Without this step, the SSH agent would refuse to communicate with. The two variables ${{secrets.SSH_PORT}} and ${{secrets.SSH_HOST}} contain, as the name suggests, the port and the host url of our remote server. The next chapter “Secrets” explains how these secrets are configured.</p><pre>ssh-agent -a $SSH_AUTH_SOCK &gt; /dev/null</pre><p>This would be very long to explain here, but let’s just say it’s needed for the next step to be able to work properly. More information can be found here: <a href="http://blog.joncairns.com/2013/12/understanding-ssh-agent-and-ssh-add/">http://blog.joncairns.com/2013/12/understanding-ssh-agent-and-ssh-add/</a></p><pre>ssh-add — &lt;&lt;&lt; “${{ secrets.SSH_KEY }}”</pre><p>Adds a private key to the list of known private key. This is a fundamental step to allow our pipeline to access our remote server, using SSH without any prompt for username and password. Without it, the deploy action would stop for the SSH login, waiting for a user input.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*kUN1KT2NET7WwGHr" /><figcaption>Photo by <a href="https://unsplash.com/@tinaflour?utm_source=medium&amp;utm_medium=referral">Kristina Flour</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Secrets</h3><p>Our configuration file contains several references to the <em>secrets</em> variable. This is a feature of GitHub that allows us to add to our pipelines some values that need to be kept secret for security reason. In depth documentation about how this works can be found at: <a href="https://docs.github.com/en/actions/security-guides/encrypted-secrets">https://docs.github.com/en/actions/security-guides/encrypted-secrets</a>.</p><p>Let’s consider these secrets as a sort of secret configuration to which only the owner of the repository have access. So, let’s create the following secrets:</p><p><strong>SSH_HOST</strong>: must contain the public internet IP of our remote server. It’s the same IP that we use for the host parameter when using the ssh command. Making it a secret avoids having our server’s IP directly available in our repository. This wouldn’t be a security issue per-se but could be a starting point for someone with hill intention to start probing our server for weak points.</p><p><strong>SSH-KEY</strong>: must contain the private SSH key needed to access our server. It goes without saying that we don’t want this value in any public place. We created it on the step 3 of this guide.</p><p><strong>SSH-PORT</strong>: must contain the port where our server’s SSH daemon is listening for connections. Normally is the 22, but depending on the server’s configuration, it could be different.</p><p><strong>SSH-USER</strong>: Must contain the username we use to access our SSH server.</p><h3>Step 5: Testing our pipeline</h3><p>Everything is set up: it’s finally the moment to test the system!</p><p>Let’s make a small change to our app:</p><pre>app.get(‘/’, (req, res) =&gt; {<br> res.send(‘Hello World 2!’) // ← Let’s change our message<br>})</pre><p>Now let’s commit and push it.</p><p>If everything goes according to plan, we should now see the GitHub pipeline kicking in and start the deploy process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/539/1*NjeXXW4AC7racIZVZ9UCAQ.png" /><figcaption>The output of the pipeline should look like the one in the picture</figcaption></figure><p>If all the steps go through without any error, our new app should be deployed to our remote Docker instance. Let’s now access our remote server from the browser and notice that the content is changed:</p><p>Success!</p><h3>Conclusion</h3><p>With this exercise we now have our own very basic but free CI/CD pipeline that deploys our content to our own remote server. From here on, we can build more and more complex scenarios with the help of GitHub Actions and docker, scaling the system more and more with anyone of our projects!</p><p>And if we end up deciding that after all, this system is too clunky and requires too much work, and that we would rather be using one of the easier, but more expensive solutions out there, at least we learned something new and gained some insight about what are the basic steps for an automated CI/CD system.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=495e8ff87c7e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/rewrite-tech/how-to-create-custom-ci-cd-with-github-and-docker-495e8ff87c7e">How To Create Custom CI/CD With Github and Docker</a> was originally published in <a href="https://medium.com/rewrite-tech">REWRITE TECH by Diconium</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>