{"id":10736,"date":"2025-10-30T03:32:42","date_gmt":"2025-10-30T03:32:42","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=10736"},"modified":"2025-10-30T03:32:42","modified_gmt":"2025-10-30T03:32:42","slug":"automating-deployment-setting-up-ci-cd-with-github-actions-and-docker","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/automating-deployment-setting-up-ci-cd-with-github-actions-and-docker\/","title":{"rendered":"Automating Deployment: Setting up CI\/CD with GitHub Actions and Docker"},"content":{"rendered":"<h1>Automating Deployment: Setting up CI\/CD with GitHub Actions and Docker<\/h1>\n<p>In today&#8217;s fast-paced software development landscape, the need for continuous integration and continuous deployment (CI\/CD) has never been higher. Automating this process can save time, reduce errors, and streamline the workflow between development and production. In this blog post, we explore how to set up a robust CI\/CD pipeline using GitHub Actions and Docker.<\/p>\n<h2>What is CI\/CD?<\/h2>\n<p>CI\/CD is a set of practices that enable development teams to deliver code changes more frequently and reliably. Continuous Integration (CI) refers to the automation of integrating code changes from multiple contributors into a single project. Continuous Deployment (CD), on the other hand, is the automated process of deploying code to production as soon as it passes all tests.<\/p>\n<p>Incorporating CI\/CD into your workflow can have several benefits:<\/p>\n<ul>\n<li><strong>Faster Development Cycles:<\/strong> Automating testing and deployment allows developers to focus on feature development.<\/li>\n<li><strong>Improved Code Quality:<\/strong> Automated tests catch errors before they reach production.<\/li>\n<li><strong>Consistent Deployment:<\/strong> Automated processes reduce the chances of human error during deployment.<\/li>\n<\/ul>\n<h2>Why Use GitHub Actions?<\/h2>\n<p>GitHub Actions provides a flexible and powerful way to automate your software development workflows directly within your GitHub repository. With the ability to create custom workflows, trigger actions based on GitHub events, and integrate with various services, it\u2019s an ideal choice for CI\/CD.<\/p>\n<p>Some notable features of GitHub Actions include:<\/p>\n<ul>\n<li><strong>Event-Driven:<\/strong> Trigger workflows based on commits, pull requests, and other GitHub activities.<\/li>\n<li><strong>Free for Public Repositories:<\/strong> Ideal for open-source projects, with generous limits on private repositories.<\/li>\n<li><strong>Marketplace:<\/strong> A rich ecosystem of pre-built actions to quickly integrate into your workflow.<\/li>\n<\/ul>\n<h2>Getting Started with Docker<\/h2>\n<p>Docker helps developers create, deploy, and run applications in containers. Containers package your code with everything it needs to run, ensuring it behaves the same way across different environments.<\/p>\n<p>Before diving into CI\/CD, you should have a basic understanding of Docker. Here are some steps to help you get started:<\/p>\n<ul>\n<li><strong>Install Docker:<\/strong> Ensure that Docker is installed on your local machine. Follow the instructions on the <a href=\"https:\/\/docs.docker.com\/get-docker\/\" target=\"_blank\">official Docker documentation<\/a>.<\/li>\n<li><strong>Create a Dockerfile:<\/strong> This file contains the instructions to build your Docker image.<\/li>\n<li><strong>Build and Run Your Application:<\/strong> Use the command <code>docker build -t your-image-name .<\/code> to build the image and <code>docker run -p 80:80 your-image-name<\/code> to run it.<\/li>\n<\/ul>\n<h2>Setting Up GitHub Actions for CI\/CD<\/h2>\n<p>Now that we\u2019re familiar with Docker and its capabilities, let\u2019s look at how to set up a CI\/CD pipeline using GitHub Actions.<\/p>\n<h3>Step 1: Create a GitHub Repository<\/h3>\n<p>Start by creating a new repository on GitHub. You can do this by visiting GitHub, clicking on the &#8220;New Repository&#8221; button, and following the prompts. For this blog, let\u2019s assume the repository is named <strong>my-app<\/strong>.<\/p>\n<h3>Step 2: Add a Dockerfile<\/h3>\n<p>In your repository, create a file named <code>Dockerfile<\/code>. Here is a sample Dockerfile for a Node.js application:<\/p>\n<pre><code>FROM node:14\n\nWORKDIR \/usr\/src\/app\n\nCOPY package*.json .\/\n\nRUN npm install\n\nCOPY . .\n\nEXPOSE 8080\n\nCMD [ \"node\", \"app.js\" ]<\/code><\/pre>\n<p>This Dockerfile uses the Node.js 14 image, sets the working directory, and installs the dependencies listed in <code>package.json<\/code>.<\/p>\n<h3>Step 3: Create a GitHub Actions Workflow<\/h3>\n<p>Next, you need to set up a workflow file in the <code>\/.github\/workflows<\/code> directory. Create a new file named <code>ci-cd.yml<\/code> and add the following content:<\/p>\n<pre><code>name: CI\/CD Pipeline\n\non:\n  push:\n    branches:\n      - main\n\njobs:\n  build:\n    runs-on: ubuntu-latest\n\n    steps:\n      - name: Check out repository\n        uses: actions\/checkout@v2\n\n      - name: Set up Docker Buildx\n        uses: docker\/setup-buildx-action@v1\n\n      - name: Build Docker Image\n        uses: docker\/build-push-action@v2\n        with:\n          context: .\n          push: true\n          tags: your-dockerhub-username\/my-app:latest\n\n      - name: Log in to Docker Hub\n        uses: docker\/login-action@v1\n        with:\n          username: ${{ secrets.DOCKER_USERNAME }}\n          password: ${{ secrets.DOCKER_PASSWORD }}\n\n      - name: Push Docker Image\n        uses: docker\/build-push-action@v2\n        with:\n          context: .\n          push: true\n          tags: your-dockerhub-username\/my-app:latest<\/code><\/pre>\n<p>This workflow triggers on pushes to the main branch, checks out the repository, builds the Docker image, and pushes it to Docker Hub. Replace <code>your-dockerhub-username<\/code> with your actual Docker Hub username.<\/p>\n<h3>Step 4: Set Up Secrets in GitHub<\/h3>\n<p>To store sensitive information like your Docker Hub credentials, you should use GitHub Secrets. Go to your GitHub repository, click on <strong>Settings<\/strong>, then navigate to <strong>Secrets and variables<\/strong> and select <strong>Actions<\/strong>. Here, add two new secrets:<\/p>\n<ul>\n<li><strong>DOCKER_USERNAME<\/strong> &#8211; your Docker Hub username<\/li>\n<li><strong>DOCKER_PASSWORD<\/strong> &#8211; your Docker Hub password<\/li>\n<\/ul>\n<h2>Testing Your CI\/CD Pipeline<\/h2>\n<p>Now that you have everything set up, push your changes to the remote repository. If everything is configured correctly, GitHub Actions should automatically trigger the CI\/CD pipeline.<\/p>\n<p>Navigate to the <strong>Actions<\/strong> tab on your GitHub repository to monitor the status of the workflow. You can view logs for each step to troubleshoot any issues that may arise.<\/p>\n<h2>Deploying to a Server<\/h2>\n<p>Once your Docker image is built and pushed to Docker Hub, you need to deploy it to a server. Here are some common ways to do this:<\/p>\n<ul>\n<li><strong>Manual Deployment:<\/strong> You can log into your server and pull the latest image using <code>docker pull your-dockerhub-username\/my-app:latest<\/code> and restart your container.<\/li>\n<li><strong>Automated Deployment:<\/strong> Use additional GitHub Actions or tools like <a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-automatic-deployment-with-github-actions\" target=\"_blank\">Ansible<\/a> or <a href=\"https:\/\/www.jenkins.io\/doc\/book\/pipeline\/\" target=\"_blank\">Jenkins<\/a> for automated deployment.<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>Setting up CI\/CD using GitHub Actions and Docker is a powerful way to enhance your development workflow. By automating the building and deployment of your applications, you can focus more on writing code and less on managing the deployment lifecycle.<\/p>\n<p>Implementing the practices outlined in this post will set you on the path towards a more efficient, reliable, and scalable development process. The key is to iterate on your workflow, continuously improving it as your project and team evolve.<\/p>\n<p>Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Automating Deployment: Setting up CI\/CD with GitHub Actions and Docker In today&#8217;s fast-paced software development landscape, the need for continuous integration and continuous deployment (CI\/CD) has never been higher. Automating this process can save time, reduce errors, and streamline the workflow between development and production. In this blog post, we explore how to set up<\/p>\n","protected":false},"author":92,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[275,1111],"tags":[1124,1297,364,387,1119,1122],"class_list":{"0":"post-10736","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-ci-cd","7":"category-github-actions-automation","8":"tag-automation","9":"tag-ci-cd","10":"tag-deployment","11":"tag-docker","12":"tag-github-actions","13":"tag-pipeline"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/10736","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/92"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=10736"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/10736\/revisions"}],"predecessor-version":[{"id":10737,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/10736\/revisions\/10737"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=10736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=10736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=10736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}