DEV Community

Marco Platzer
Marco Platzer

Posted on • Edited 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.

Get it on Codeberg

Top comments (0)