Logo

dev-resources.site

for different kinds of informations.

Automating Azure Project Setup with PowerShell and GitHub Actions

Published at
12/5/2024
Categories
azure
powershell
githubactions
kubernetes
Author
latzo
Author
5 person written this
latzo
open
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…




powershell Article's
30 articles in total
Favicon
Windows 上 VSCode 的 C/C++ 延伸模組處理編碼的問題
Favicon
PowerShell 的文字編碼
Favicon
笔记3
Favicon
TryHackMe | Windows PowerShell | RSCyberTech
Favicon
Configuring Hyper-V Global Default Directories
Favicon
How I Set Up an Awesome PowerShell Environment for Script Development
Favicon
Azure Function App (Flex Consumption) PowerShell Modules solution
Favicon
File Comparison Made Easy: Detecting New and Changed Files with PowerShell
Favicon
10 Best Practices of PowerShell Code Signing for Signing Your Script
Favicon
PowerShell Script Collection: Automation and Solutions for Everyday Tasks
Favicon
Video: List All Available Windows Features on Windows 11 using CMD & PowerShell
Favicon
Set up Azure Network Security Perimeter with PowerShell
Favicon
Automating Azure Project Setup with PowerShell and GitHub Actions
Favicon
Azure Function App (Flex Consumption) in private VNET via IaC
Favicon
Using PowerShell to Create Service Principals for RBAC
Favicon
Two ways to use Pester to Mock objects with strongly typed parameters
Favicon
PowerShell Automation: Execute Batch GitHub CLI Commands
Favicon
❓ Do you allow wrong input to enter your function?
Favicon
Introducing PowerShell Utility Scripts
Favicon
Cisco DHCP Pool conversion to Windows Server DHCP
Favicon
PowerShell | Script output garbled Chinese characters
Favicon
Rename Multiple Files in Sequence with Just One Click Using PowerShell in Windows! 🚀
Favicon
Redirect Out-File to TestDrive: in your PowerShell Pester test scripts with this one weird trick
Favicon
Using PowerShell for day to day stuff
Favicon
Automating SharePoint Embedded: Using PowerShell to Call Graph API Endpoints
Favicon
Install Ubuntu on WSL 2
Favicon
How to Install WSL from PowerShell on Windows 10 and 11
Favicon
How to Run PowerShell Script in Jenkins Pipeline
Favicon
Identify Which Files are Large.
Favicon
Quick guide to setting up an shortcut/alias in windows

Featured ones: