5-how-to-set-up-a-cicd-pipeline-with-github-actions-and-azure.html

How to Set Up a CI/CD Pipeline with GitHub Actions and Azure

In today's software development landscape, Continuous Integration (CI) and Continuous Deployment (CD) have become essential for delivering high-quality software quickly and efficiently. This article will guide you through setting up a CI/CD pipeline using GitHub Actions and Azure, providing you with clear code examples, actionable insights, and troubleshooting tips.

What is CI/CD?

Continuous Integration (CI)

Continuous Integration is the practice of automatically testing and integrating code changes into a shared repository. The main goals of CI are to detect errors quickly, improve software quality, and reduce the time it takes to deliver updates.

Continuous Deployment (CD)

Continuous Deployment extends CI by automating the release process. With CD, every change that passes automated tests is automatically deployed to production, enabling faster delivery of features and fixes.

Why Choose GitHub Actions and Azure?

  • GitHub Actions: Offers a flexible way to automate workflows directly in your GitHub repository. It supports a wide range of triggers, including push events, pull requests, and scheduled tasks.
  • Azure: Provides a robust cloud platform for deploying applications, offering features like scalability, security, and global reach.

Combining GitHub Actions with Azure allows developers to create efficient CI/CD pipelines that streamline the development process.

Setting Up Your CI/CD Pipeline

Prerequisites

Before we dive into the setup, ensure you have the following:

  • A GitHub account.
  • An Azure account with access to Azure DevOps or Azure App Services (depending on your deployment choice).
  • A basic understanding of Git and GitHub.

Step 1: Create a New Repository

  1. Log in to your GitHub account.
  2. Click on the New button to create a new repository.
  3. Name your repository (e.g., ci-cd-demo), add a description, and click Create repository.

Step 2: Add Your Application Code

For demonstration purposes, let's create a simple Node.js application.

  1. Clone your repository to your local machine:

bash git clone https://github.com/yourusername/ci-cd-demo.git cd ci-cd-demo

  1. Create a simple Node.js app:

bash npm init -y npm install express

  1. Create an index.js file and add the following code:

```javascript const express = require('express'); const app = express(); const PORT = process.env.PORT || 3000;

app.get('/', (req, res) => { res.send('Hello, CI/CD with GitHub Actions and Azure!'); });

app.listen(PORT, () => { console.log(Server is running on port ${PORT}); }); ```

  1. Commit your changes and push them to GitHub:

bash git add . git commit -m "Initial commit" git push origin main

Step 3: Create a GitHub Actions Workflow

  1. In your GitHub repository, navigate to the Actions tab.
  2. Click on New workflow and select Set up a workflow yourself.
  3. Replace the default YAML with the following configuration:

```yaml name: CI/CD Pipeline

on: push: branches: - main

jobs: build: runs-on: ubuntu-latest

   steps:
     - name: Checkout code
       uses: actions/checkout@v2

     - name: Set up Node.js
       uses: actions/setup-node@v2
       with:
         node-version: '14'

     - name: Install dependencies
       run: npm install

     - name: Run tests
       run: npm test

 deploy:
   runs-on: ubuntu-latest
   needs: build
   if: github.ref == 'refs/heads/main'

   steps:
     - name: Checkout code
       uses: actions/checkout@v2

     - name: Deploy to Azure
       uses: azure/webapps-deploy@v2
       with:
         app-name: <Your-Azure-App-Name>
         publish-profile: ${{ secrets.AZURE_PUBLISH_PROFILE }}

```

Replace <Your-Azure-App-Name> with the name of your Azure Web App.

Step 4: Configure Azure

  1. Log in to your Azure account and create a new Web App service.
  2. Once your Web App is created, navigate to the Deployment Center.
  3. Select GitHub as the source and authenticate with your GitHub account.
  4. Set the branch to main and complete the setup.
  5. Copy the publish profile from the Overview section and add it to your GitHub repository secrets:

  6. Go to your GitHub repository.

  7. Click on Settings > Secrets and variables > Actions > New repository secret.
  8. Name it AZURE_PUBLISH_PROFILE and paste the publish profile XML.

Step 5: Test the Pipeline

  1. Push a new commit to the main branch:

bash echo "New feature added!" >> README.md git add README.md git commit -m "Update README" git push origin main

  1. Navigate to the Actions tab in your GitHub repository to see the workflow in action.

  2. Once the build and deployment are successful, visit your Azure Web App URL to see your application live!

Troubleshooting Tips

  • Failed Builds: Check the logs in the Actions tab for detailed error messages. Common issues include dependency installation failures or test failures.
  • Deployment Issues: Ensure that your Azure Web App name is correct and that your publish profile is properly configured in GitHub secrets.
  • Testing Locally: Always test your application locally before pushing changes to catch issues early.

Conclusion

Setting up a CI/CD pipeline using GitHub Actions and Azure can significantly improve your development workflow. By automating the build, test, and deployment processes, you can focus more on coding and less on manual tasks. With the above steps, you now have a solid foundation to implement CI/CD in your projects, ensuring faster delivery and higher quality in your software development efforts. Happy coding!

SR
Syed
Rizwan

About the Author

Syed Rizwan is a Machine Learning Engineer with 5 years of experience in AI, IoT, and Industrial Automation.