<?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 Jim Eric Skogman on Medium]]></title>
        <description><![CDATA[Stories by Jim Eric Skogman on Medium]]></description>
        <link>https://medium.com/@jim.eric.skogman?source=rss-a8420a3d563f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*qT1Pq3DCt6oG9uyoTrvLmw.jpeg</url>
            <title>Stories by Jim Eric Skogman on Medium</title>
            <link>https://medium.com/@jim.eric.skogman?source=rss-a8420a3d563f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 02:47:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jim.eric.skogman/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[A CI/CD solution in under 10 minutes Featuring Terraform, Ansible and Drone CI on GCP]]></title>
            <link>https://medium.com/google-cloud/a-ci-cd-solution-in-under-10-minutes-featuring-terraform-ansible-and-drone-ci-on-gcp-16bba497c655?source=rss-a8420a3d563f------2</link>
            <guid isPermaLink="false">https://medium.com/p/16bba497c655</guid>
            <category><![CDATA[infrastructure]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[ansible]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[terraform]]></category>
            <dc:creator><![CDATA[Jim Eric Skogman]]></dc:creator>
            <pubDate>Thu, 07 Nov 2019 14:47:00 GMT</pubDate>
            <atom:updated>2019-11-07T14:47:00.942Z</atom:updated>
            <content:encoded><![CDATA[<h3>A CI/CD solution in under 10 minutes, using Terraform &amp; Ansible on GCP</h3><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*VwcyIZ3x7tlrtME9QSjv1A.jpeg" /></figure><p>As projects mature and complexity increases, integrating a CI/CD solution can make a lot of sense.</p><p>However, getting such a system up and running can be a surprisingly technical and time consuming process.</p><p>What follows is a hands-on guide for deploying Drone CI, a lightweight, container-centric CI/CD solution, in a manner of minutes as opposed to days.</p><p>To accomplish this, we will harness the power of Terraform and Ansible, a powerful combo for automating the creation and and provisioning of infrastructure resources.</p><p>We won’t dive too deep into any specific technologies or low level concepts, as there already exists a plethora of resources that address such topics.</p><p>Instead, we will take a more pragmatic approach and focus on a real-world scenario, designed to highlight the benefits of automation and provide an insightful glimpse into the world of “Infrastructure as code”.</p><p>If however, you favour a more direct approach, head over to <a href="https://github.com/jadechip/infrastructure-as-code">Github</a> and follow the accompanying instructions. The whole process shouldn’t take you more than 10 minutes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*0x2I8DIFfIEXXdQs1Q1F4g.gif" /></figure><p>In any case, be sure to give me a few claps, or a star on Github, should you find the content useful 🤩</p><h3>Prereqs</h3><p>We will be using Google Cloud Provider (GCP), however, the instructions that follow can easily be modified for other cloud providers. If you don’t yet have an account, GCP provides 300$ worth of credits for new users.</p><p>We also need <a href="https://www.terraform.io/">Terraform</a> and <a href="https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html">Ansible</a> for our orchestration and provisioning needs, so be sure to have those installed before proceeding.</p><p>Lastly, I’ve opted to use Github, however Drone CI also supports other version control solutions including Bitbucket, GitLab, Gitea and Gogs. Check out the <a href="https://docs.drone.io/installation/overview/">documentation</a> for more information.</p><h3>Gameplan</h3><p>We are now ready to begin, here is our game plan:</p><ol><li>Generate the required infrastructure with <strong>Terraform</strong>: a single VM, or compute instance in the vernacular of GCP.</li><li>Add a static IP to our machine and configure basic Firewall settings.</li><li>Setup an OAuth application on Github.</li><li>Install any necessary dependencies and run Docker using <strong>Ansible</strong> Playbooks.</li><li>Download and run the <strong>Drone</strong> agent and server images from Docker Hub.</li><li>Celebrate 🎉 (optional).</li></ol><h3>Authentication</h3><p>In order for Terraform to securely interact with GCP, we first need to setup a<strong> service account.</strong></p><h4>Service account</h4><blockquote>The preferred method of provisioning resources with Terraform is to use a GCP service account, a “robot account” that can be granted a limited set of IAM permissions.</blockquote><p>Head over to <a href="https://console.cloud.google.com/">Google Cloud Console</a> and create a new project or select an existing one.</p><p>Under the <em>APIs &amp; Services </em>section in the main menu, click on <em>Credentials</em>. Select <em>Service account key</em> from the <em>Create credentials </em>dropdown menu and fill in the required details. Finally, click <em>create</em> to download the resulting JSON file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*SIcpKNV-XOZlxwW7TDYihA.gif" /></figure><p>For more information on setting up authentication on GCP, check out the link below.</p><p><a href="https://cloud.google.com/docs/authentication/getting-started">Getting started with authentication | Authentication | Google Cloud</a></p><h4>SSH</h4><p>We also need to grant Ansible programmatic access to our VM by adding a <strong>public SSH</strong> key to the Terraform metadata.</p><p>We can easily generate a pair of keys by running the following command and entering a password when prompted.</p><pre>ssh-keygen -t rsa -f ~/.ssh/[KEY_FILENAME] -C `whoami`</pre><p>Let’s also restrict access to our private key to prevent others from modifying it.</p><pre>chmod 400 ~/.ssh/[KEY_FILENAME]</pre><p>For more details about creating SSH keys for resources on GCP, check out the following link.</p><p><a href="https://cloud.google.com/compute/docs/instances/adding-removing-ssh-keys">Managing SSH keys in metadata | Compute Engine Documentation</a></p><h3>Terraform</h3><p>Terraform uses machine-readable definition files to define, create, manage and update infrastructure resources such as physical hardware, VMs, containers and much more. This allows us to create reproducible environments, share configuration with other developers and subject our infrastructure to version control.</p><h4>Infrastructure as Code</h4><p>Let’s begin by creating a directory called terraform and adding the following files.</p><h4><strong>terraform/main.tf</strong></h4><p>Here, we simply declare a few <a href="https://www.terraform.io/docs/configuration/variables.html">input variables</a>, and specify our chosen provider.</p><blockquote>A provider is responsible for understanding API interactions and exposing resources. Providers generally are an IaaS (e.g. Alibaba Cloud, AWS, GCP, Microsoft Azure, OpenStack), PaaS (e.g. Heroku), or SaaS services (e.g. Terraform Cloud, DNSimple, CloudFlare).</blockquote><p>Note that we are referencing the credentials file we generated earlier.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aea288bbff21ddd0cc6d3f3132434561/href">https://medium.com/media/aea288bbff21ddd0cc6d3f3132434561/href</a></iframe><h4><strong>terraform/vm.tf</strong></h4><p>Now we can move on to configuring the main resources of our infrastructure. First, we specify a boot disk and machine type for our compute instance. Then, we set up some basic Firewall settings and expose the necessary ports. We also need to attach the public SSH key from earlier. Finally, we specify a static IP address and bind it to our machine.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0c1fdd62a54bc865756a041e5933c576/href">https://medium.com/media/0c1fdd62a54bc865756a041e5933c576/href</a></iframe><h4><strong>terraform/terraform.tfvars</strong></h4><p>We also need to declare the actual values for the variables in <strong>main.tf</strong>. Go ahead and replace the placeholder text with your own values. Be sure to include the credentials file of your service account and public SSH key.</p><p>Note that the <em>project name</em> needs to match that of <strong>your selected project in GCP</strong>, otherwise our credentials file won’t have any effect.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f65251d48ef8b88f88a61cca39974984/href">https://medium.com/media/f65251d48ef8b88f88a61cca39974984/href</a></iframe><p>For a complete list of regions and zones, check out <a href="https://cloud.google.com/compute/docs/regions-zones/">https://cloud.google.com/compute/docs/regions-zones/</a></p><h4>Orchestration</h4><p>To initialize Terraform, hop into the terraform directory and run the following:</p><pre>terraform init</pre><p>To test out what we have so far, we can use the terraform plan command to get a preview of what resources Terraform will end up creating and/or modifying. If everything looks good, we can go ahead and apply the changes:</p><pre>terraform apply</pre><p>If the operation was a success, the resulting output should look similar to this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/421/1*ltGLflM9SpJ6lzT8CooIVg.png" /></figure><p><strong>Be sure to take note of the IP address of our new compute instance</strong>, as we will be using it shortly. For convenience, we can store it in a variable:</p><pre>export VM_IP=$(terraform output ip)</pre><h3>OAuth</h3><p>For Drone to eventually be able access our source code on Github, we need to make a quick detour and register an OAuth application. Head on over to Github’s settings page and click on <em>Developer settings</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*2zDBnGe0PXtFDEQ0LLVTbQ.gif" /></figure><p>Select the <em>OAuth Apps</em> tab and click<em> New OAuth App, </em>then fill in the required details.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KvrnRRCt9XtM8xRhAoGiyQ.png" /></figure><p>Note that the authorization callback URL must match the above format and path, and must use your exact server scheme and host.</p><p><strong>If you don’t have a custom domain, go ahead and use the IP address of our compute instance.</strong></p><pre>echo $VM_IP</pre><h3>Ansible</h3><p>With Ansible, we can easily automate a wide variety of tasks, ranging from application deployment to multi-node orchestration, all without installing any agents or additional software on remote systems.</p><p>To accomplish this, Ansible uses the existing SSH daemon in combination with <a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks.html">Playbooks</a>, instruction files that help us easily manage configuration and define our orchestration processes.</p><h4>Configuration as code</h4><p>We first need to add a reference to our VM in a so called <a href="https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html">inventory file</a>. You can either edit the default inventory, which you can usually find at /etc/ansible/hosts, or create your own inventory file and reference it with the -i &lt;path&gt; flag.</p><p>Go ahead and add the following line.</p><pre>droneci ansible_host=[IP]ansible_ssh_private_key_file=[SSH_KEY_PATH]</pre><p>To test out the connection, we can use Ansible’s built in ping command.</p><pre>ansible droneci -m ping</pre><p>This should result in an output similar to this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/429/1*lAgifFN5jnGAm0VacDZFLw.png" /></figure><h4>Environment variables</h4><p>Before we start writing our Playbooks, we need to add a few environment variables in order to properly configure Drone CI.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e07f393f26849cd47ef108ed8c5db553/href">https://medium.com/media/e07f393f26849cd47ef108ed8c5db553/href</a></iframe><p>Note that the <em>DRONE_SERVER_HOST </em>variable needs to match the domain we specified in our OAuth application on Github, though we do not need to specify the protocol.</p><p>Execute the bash script by running chmod +x env.sh followed by source env.sh.</p><p>For more information on the variables required to configure Drone, check out <a href="https://docs.drone.io/installation/reference/">https://docs.drone.io/installation/reference/</a>.</p><h4>Playbooks</h4><p>We’ll need a dedicated folder for our Playbooks. Let’s nest it within another directory called ansible, like so:</p><pre>.<br>├── ansible<br>│   └── playbooks<br>├── env.sh<br>└── terraform<br>    ├── main.tf<br>    ├── terraform.tfvars<br>    └── vm.tf</pre><p>At last we can start adding our Playbooks.</p><h4><strong>ansible/playbooks/install-docker.yaml</strong></h4><p>Our first Playbook will simply install Docker and Docker Compose. We also specify a Python interpreter and install the required system packages.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ad3fd0015454e45b0d99b692b23fdaa4/href">https://medium.com/media/ad3fd0015454e45b0d99b692b23fdaa4/href</a></iframe><h4>ansible/playbooks/<strong>start-drone.yaml</strong></h4><p>With everything installed, the only step left is to run the Drone images. To do this we can use Ansible’s built-in Docker Compose module. Notice how we are referencing the environment variables we added earlier.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/673e3c75854c85d155e3701adba4e3e5/href">https://medium.com/media/673e3c75854c85d155e3701adba4e3e5/href</a></iframe><h4>ansible/playbooks/main.yaml</h4><p>Finally, we simply add a reference to each file in <strong>main.yaml</strong>, which will serve as the main entrypoint for executing our Playbooks.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/936727d7dffef11090543ce4cb1e2f50/href">https://medium.com/media/936727d7dffef11090543ce4cb1e2f50/href</a></iframe><h4>Provisioning</h4><p>To begin provisioning, run the following command:</p><pre>ansible-playbook playbooks/main.yaml</pre><p>If you choose to use a custom inventory file, the same command might look like this:</p><pre>ansible-playbook -i inventory.tpl playbooks/main.yaml -u jimericskogman — private-key ~/.ssh/drone-ci</pre><p>This might take a couple of minutes. In the meantime, why not take a glance at your crypto portfolio and gasp in despair 😱.</p><p>To verify the success of our recent endeavours, fire up a browser and head over to the domain/ip that we specified earlier. You should be prompted with the following screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pvAQm_p4PW2yHHg-XiG0sA.png" /></figure><p>Once you’ve signed in to Github, you will be redirected to the main dashboard and asked to sync your repositories. Hoorah! 🎉</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Cevmc4apIa0X524z1Q-d4A.png" /></figure><h3>Conclusion</h3><p>We’ve made it to the end of a hopefully informative and relatively painless journey. With our CI/CD all set up, the stage is set for us to start building, testing and deploying our code. A topic we will cover in future posts. Until then, I thank you for your time and bid thee farewell!</p><p><a href="https://github.com/jadechip/infrastructure-as-code">jadechip/infrastructure-as-code</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=16bba497c655" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-cloud/a-ci-cd-solution-in-under-10-minutes-featuring-terraform-ansible-and-drone-ci-on-gcp-16bba497c655">A CI/CD solution in under 10 minutes Featuring Terraform, Ansible and Drone CI on GCP</a> was originally published in <a href="https://medium.com/google-cloud">Google Cloud - Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>