This repo is intended to be used as a tool in a workshop where participants use a slightly altered version to build, test and deploy a .NET application to Azure Container Apps using Codespaces, Terraform and GitHub Actions.
2 Methods of deployment
- deploy.sh script uses Azure CLI to deploy.
- terraform, using azurerm and azapi providers, which essentially wraps the Azure API with a terraform/bicep blend.
HTTPS with Git Credential manager should automatically be forwarded to the Codespace/Devcontainer.
For ssh key configuration inside the container:
Linux
eval "$(ssh-agent -s)"
ssh-add $HOME/.ssh/id_rsa
docker ps
docker cp ~/.ssh/id_rsa <container id>:/home/vscode/.ssh
Windows
# Make sure you're running as an Administrator
Set-Service ssh-agent -StartupType Automatic
Start-Service ssh-agent
Get-Service ssh-agent
ssh-add $HOME/.ssh/github_rsa
docker cp "$env.USERPROFILE"/.ssh/id_rsa <container id>:/home/vscode/.ssh
macOS
ssh-add $HOME/.ssh/github_rsa
terraform {
backend "azurerm" {
resource_group_name = "<STATE RESOURE GROUP>"
storage_account_name = "<SA NAME>
container_name = "<CONTAINER NAME>"
key = "<GITHUB_USERNAME>"
}
}
locals {
location = "eastus"
}
resource "azurerm_resource_group" "rg" {
name = "<NAME>"
location = local.location
}
resource "azurerm_log_analytics_workspace" "laws" {
name = "<NAME>"
location = local.location
resource_group_name = azurerm_resource_group.rg.name
sku = "PerGB2018"
retention_in_days = 30
}
resource "azapi_resource" "container_app_environment" {
name = "<NAME>"
location = local.location
parent_id = azurerm_resource_group.rg.id
type = "Microsoft.App/managedEnvironments@2022-01-01-preview"
body = jsonencode({
properties = {
appLogsConfiguration = {
destination = "log-analytics"
logAnalyticsConfiguration = {
customerId = azurerm_log_analytics_workspace.laws.workspace_id
sharedKey = azurerm_log_analytics_workspace.laws.primary_shared_key
}
}
}
})
}
resource "azapi_resource" "container_app" {
name = "loco-logo"
location = local.location
parent_id = azurerm_resource_group.rg.id
type = "Microsoft.App/containerApps@2022-01-01-preview"
body = jsonencode({
properties = {
managedEnvironmentId = azapi_resource.container_app_environment.id
configuration = {
ingress = {
targetPort = <PORT>
external = true
}
}
template = {
containers = [
{
image = <IMAGE TAG>
name = <NAME>
}
]
}
}
})
}
dotnet run
- https://localhost:/swagger
- Test
- Add 'blinky' or other endpoints. Alter 'hello' endpoint. Build, run, test using native dotnet cli tools.
- Build docker image inside Codespaces container
- Test usinng native docker tools.
Takeaways: Notice how quick it is to be working in a dev environment that can do everything we need. Notice that not only is the right version of the dotnet core SDK, targeting pack and runtime installed, but we also have the aspnet runtime as well. Notice that docker is configured and "just works"
- New Workflow/Action
- Use a Starter Workflow called ".NET Build and Push"
- Configure app specifics, trigger workflow with code change, PR to trigger a build and push image to GitHub Packages (ghcr.io)
Takeaways: Codespaces + GitHub is all you need to dev, build, automate from a web browser. Build Artifact and Image both consumeable from GitHub for any testing, troubleshooting if need be.
- Create a simple terraform configuration using boilerplate here.
- Azure intro, show the subscription, state storage container, resource groups to target.
- Explain Container Apps as managed Kubernetes.
- Backend configuration, container apps deployment config
Takeaways: No need to install Terraform, mess with PATH variable or other environment variables. No reboots, no need to get authorization to run a binary on your machine. Azure Container Apps are a great way to start using containerized workloads now, without the need to become experts in Kubernetes right away. Things that work in your Codespaces dev environment work very much the same way in Container Apps, because the architecture is the same. General Infrastructure as Code introduction.
- New Workflow/Action
- Starter workflow is "Terraform Workflow"
- Overview of Repo/Org secrets and how they simlify CI/CD
- Using image built in previous workflow in this workflow. Edit terraform code in Codespaces, push changes to trigger Action.
Takeaways: We have reusable code for our build, publish, consume and deploy steps. How much time have we saved by using Starter Workflows? How easily could we roll back in the case of a post-deployment bug? What would that look like? (just a PR).
- Uh oh! There's a crippling bug! (Dev, test, deploy all from Codespaces)
- Code fix via Codespaces.
- Terraform updated to reflect our fixed build.
- Deploy, all triggered inside Codespaces.
Takeaways: What you'd have to do as a new dev without Codespaces:
- install the right .NET SDK, Targetting packs, aspnet and .net core runtimes. (.NET is one of the simler languages to get started with.)
- Configure access, get Docker installed and running, which could mean WSL, hyper-v setup.
- Install and configure Terraform.
- Install az cli
- Install vscode and various extensions for each language
Time saved taking advantage of Starter Workflows