DEV Community

Marco Platzer
Marco Platzer

Posted on

Automating Azure Project Setup with PowerShell and GitHub Actions

Setting up a new project on GitHub that integrates Azure services and GitHub Actions pipelines is something I frequently do. To streamline this repetitive process, I recently developed a Quickstart Template for Azure Kubernetes Service (AKS) that includes an Azure Container Registry (ACR) and a fully automated CI/CD pipeline. Here's a breakdown of how I approach these projects and a new PowerShell function I created to make the setup even easier.

Initial Setup Workflow

Whenever I start a new project, the following steps are my go-to checklist:

  1. Create a Service Principal
    I create a service principal to enable GitHub workflows to manage Azure resources. Necessary permissions are granted for Bicep deployments and resource provisioning.

  2. Add Federated Credentials to Entra Application
    By leveraging federated credentials, I enable GitHub Actions workflows to authenticate with Azure without requiring secrets.

  3. Configure GitHub Actions Secrets
    All essential variables are stored in GitHub Actions secrets to facilitate seamless access within the pipeline.

Automating the Process with PowerShell

This time, I went a step further and created a PowerShell function to automate the setup process. While it’s currently tailored for AKS, it can serve as a starting point for your Azure-specific needs.

Key Features of the PowerShell Function

  • Automates service principal creation and permission assignment.
  • Configures federated credentials for GitHub Actions.
  • Populates GitHub repository secrets with the required variables.

How to Use the PowerShell Function

Step 1: Dot Source the PowerShell Script

First, load the PowerShell script into your session:

. ./Setup-AzureProject.ps1
Enter fullscreen mode Exit fullscreen mode

Step 2: Call the Function with Parameters

Next, invoke the Setup-AzureProject function with your project-specific parameters:

Setup-AzureProject -DisplayName "Quickstart AKS" `
-AksSubscriptionId "<SubscriptionID>" `
-AksResourceGroup "rg-k8s-dev-001" `
-AksClusterName "latzok8s" `
-AksRegion "switzerlandnorth" `
-DeploymentManifestPath "./aks-deploy/deployment.yaml" `
-ServiceManifestPath "./aks-deploy/service.yaml" `
-DockerImageName "quickstart-aks-py" `
-AcrSubscriptionId "<SubscriptionID>" `
-AcrResourceGroup "rg-acr-prod-001" `
-AcrName "latzox" `
-SshKeyName "ssh-latzok8s-dev-001" `
-GitHubOrg "Latzox" `
-RepoName "quickstart-azure-kubernetes-service" `
-EnvironmentNames @('aks-prod', 'build', 'infra-preview', 'infra-prod')
Enter fullscreen mode Exit fullscreen mode

Replace the placeholders with your specific values to customize the setup.

What Happens Next?

After running the PowerShell script:

Preconfigured CI/CD Pipelines

The .github/workflows/ directory contains ready-to-use pipelines that:

  • Build and publish a container image.
  • Deploy AKS resources.
  • Set up a Kubernetes deployment and expose the service.

Step-by-Step Guide

The repository’s README file provides a detailed walkthrough for deploying this solution.

Explore the Repository

Feel free to explore the repository and use the template as a foundation for your projects. Whether you’re setting up a Kubernetes cluster in Azure or looking for a quick-start solution, this template has you covered.

GitHub logo Latzox / quickstart-azure-kubernetes-service

Quickstart template for Azure Kubernetes Service

Quickstart Azure Kubernetes Service

This repository contains the code and configurations for deploying and managing applications and infrastructure using Kubernetes, Azure, and Docker. It is structured to support various stages of development, from infrastructure provisioning to application deployment.

Repository structure

.github/
  workflows/
    docker-build-and-publish.yml    # GitHub Actions workflow for building and publishing Docker images
    infra-deployment.yml            # Workflow for deploying infrastructure
    k8s-deployment.yml              # Workflow for deploying applications to Kubernetes

aks-deploy/ 
  deployment.yaml                   # Kubernetes Deployment manifest
  service.yaml                      # Kubernetes Service manifest

app/  
  app.py                            # Python application source code
  Dockerfile                        # Dockerfile for building the application image

azure-deploy/ 
  aks.bicep                         # Azure Kubernetes Service (AKS) Bicep template
  main.bicep                        # Main Bicep template for infrastructure deployment
  role.bicep                        # Bicep template for setting up roles and permissions

.gitignore                          # Git ignore rules
initial.ps1                         # PowerShell script for initial Azure setup
LICENSE                             # License information
README.md                           # Documentation file (this file)
Enter fullscreen mode Exit fullscreen mode

How to use

Clone the repository

Use…




Top comments (0)