Configuring CircleCI to deploy a Golang application to Azure involves setting up a continuous integration and continuous deployment (CI/CD) pipeline that automates the testing, building, and deployment of your Go app. Below is a detailed explanation and step-by-step guide on how to configure CircleCI to deploy a Golang app to Azure, covering prerequisites, project setup, Dockerizing the app, Azure configuration, CircleCI configuration, and deployment.
Prerequisites
Before starting, ensure the following are ready:
- Basic knowledge of Go programming language and development environment with Go installed.
- An existing Golang application that you want to deploy.
- Docker installed on your development machine and familiarity with Docker.
- An Azure account with permissions to create and manage resources.
- Azure CLI installed for resource management and deployment commands.
- CircleCI account connected to your source code repository such as GitHub.
- Understanding of CircleCI concepts like orbs, jobs, and workflows.
Preparing Your Go Application
First, ensure your Go application runs successfully locally. You can test this with:
bash
go run main.go
Or, if you have unit tests, run them to make sure everything is working:
bash
go test -v
Dockerizing the Go App
To deploy your Golang app containerized through Azure, you will need to create a Docker image. This involves writing a Dockerfile at the root of your project directory. A typical Dockerfile for a Go app might look like this:
dockerfile
# Use official golang image as build environment
FROM golang:1.18 as builder
WORKDIR /app
# Copy Go modules manifests
COPY go.mod go.sum ./
RUN go mod download
# Copy source code
COPY . .
# Build the Go app
RUN CGO_ENABLED=0 GOOS=linux go build -o main .
# Use a minimal base image for running the app
FROM alpine:latest
# Install certificates if needed
RUN apk --no-cache add ca-certificates
WORKDIR /root/
# Copy binary from builder stage
COPY --from=builder /app/main .
# Expose the port your app runs on
EXPOSE 8080
# Command to run the executable
CMD ["./main"]
This multi-stage Dockerfile first builds the Go binary in the official Go environment and then copies it into a minimal Alpine image to keep the final container lightweight.
Azure Setup
1. Create Azure Container Registry (ACR):
This is a private registry to store your application's Docker images. You can create it via Azure Portal, CLI, or ARM templates. Here's CLI command example:
bash
az acr create --resource-group --name --sku Basic
2. Create Azure Web App for Containers:
This service will host your Dockerized Go app. You can create it with this command:
bash
az webapp create --resource-group --plan --name --deployment-container-image-name .azurecr.io/:
3. Configure Azure Service Principal for Authentication:
Create a service principal with permission to access resources for CI/CD.
bash
az ad sp create-for-rbac --name --role contributor --scopes /subscriptions//resourceGroups/
Make note of the AppId, Password (Client Secret), Tenant, and Subscription ID for CircleCI environment variables.
CircleCI Configuration
In your Go project repository, create a `.circleci/config.yml` file. This file defines the pipeline that CircleCI will execute for continuous integration and deployment.
Here's an example CircleCI config for building, testing, Dockerizing, pushing to Azure Container Registry, and deploying to Azure Web App:
yaml
version: 2.1
orbs:
go: circleci/go@1.7.1
azure-cli: circleci/azure-cli@1.3.2
docker: circleci/docker@2.1.1
executors:
go-executor:
docker:
- image: cimg/go:1.18
working_directory: ~/app
jobs:
build-and-test:
executor: go-executor
steps:
- checkout
- go/load-cache
- run:
name: Download Go modules
command: go mod download
- run:
name: Run tests
command: go test -v ./...
- go/save-cache
build-and-push-image:
docker:
- image: cimg/base:stable
- image: cimg/docker:stable-dind
environment:
DOCKER_DRIVER: overlay2
steps:
- checkout
- setup_remote_docker:
docker_layer_caching: true
- run:
name: Login to Azure Container Registry
command: echo $AZURE_ACR_PASSWORD | docker login $AZURE_ACR_LOGIN_SERVER -u $AZURE_ACR_USERNAME --password-stdin
- run:
name: Build Docker image
command: |
docker build -t $AZURE_ACR_LOGIN_SERVER/$DOCKER_IMAGE_NAME:$CIRCLE_SHA1 .
- run:
name: Push Docker image
command: |
docker push $AZURE_ACR_LOGIN_SERVER/$DOCKER_IMAGE_NAME:$CIRCLE_SHA1
deploy-to-azure:
docker:
- image: circleci/python:3.8
steps:
- checkout
- azure-cli/install
- run:
name: Azure Login with Service Principal
command: |
az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET --tenant $AZURE_TENANT_ID
az account set --subscription $AZURE_SUBSCRIPTION_ID
- run:
name: Update Azure Web App with new Docker image
command: |
az webapp config container set --name $AZURE_WEBAPP_NAME --resource-group $AZURE_RESOURCE_GROUP --docker-custom-image-name $AZURE_ACR_LOGIN_SERVER/$DOCKER_IMAGE_NAME:$CIRCLE_SHA1 --docker-registry-server-url https://$AZURE_ACR_LOGIN_SERVER
workflows:
version: 2
build-test-push-deploy:
jobs:
- build-and-test
- build-and-push-image:
requires:
- build-and-test
- deploy-to-azure:
requires:
- build-and-push-image
Breaking down the above CircleCI config:
- It uses orbs for Go, Azure CLI, and Docker convenience.
- The `build-and-test` job runs Go tests.
- The `build-and-push-image` job builds the Docker container and pushes it to the Azure Container Registry, tagging it with the commit SHA.
- The `deploy-to-azure` job logs into Azure using a service principal and updates the Azure Web App to use the newly pushed Docker image.
Environment Variables and Secrets
To authenticate and connect with Azure, set these environment variables in your CircleCI project settings:
- `AZURE_CLIENT_ID` â Service principal App ID
- `AZURE_CLIENT_SECRET` â Service principal password
- `AZURE_TENANT_ID` â Azure tenant ID
- `AZURE_SUBSCRIPTION_ID` â Azure subscription ID
- `AZURE_RESOURCE_GROUP` â Resource group name for your web app
- `AZURE_WEBAPP_NAME` â Azure Web App name
- `AZURE_ACR_LOGIN_SERVER` â Azure Container Registry login server (e.g., myregistry.azurecr.io)
- `AZURE_ACR_USERNAME` â Azure Container Registry username (can be the service principal or admin user)
- `AZURE_ACR_PASSWORD` â Password for the Azure Container Registry user
- `DOCKER_IMAGE_NAME` â Name for your Docker image repository (e.g., my-go-app)
Managing Docker login credentials securely using environment variables ensures your pipeline can authenticate to Azure services required for deployment.
Push to GitHub and Connect CircleCI
1. Push your Go app code along with the `.circleci/config.yml` file to your GitHub repository.
2. Open CircleCI dashboard, add the project by connecting your GitHub repository.
3. CircleCI will detect the config file and start running the pipeline whenever you push commits.
Deployment Process
1. CircleCI runs tests on your Go application.
2. On successful tests, CircleCI builds the Docker image and pushes it to the Azure Container Registry.
3. The pipeline then triggers Azure CLI commands to update the Azure Web App's container configuration to the new image.
4. Azure Web App restarts and hosts the new version of your Go app.
Azure Web App Monitoring and Troubleshooting
- Monitor the deployment status and logs through the Azure Portal or using Azure CLI.
- Access logs for the web app to diagnose startup failures or runtime issues:
bash
az webapp log tail --name --resource-group
- If deployments fail due to authentication, verify your service principal credentials and roles.
- Verify that your Docker image tags and naming conventions match in CircleCI and Azure.
Alternative Approaches
Instead of deploying directly to Azure Web App for Containers, you could:
- Use Azure Kubernetes Service (AKS) if you prefer container orchestration.
- Deploy to Azure Container Apps if you want a serverless container hosting platform.
- Automate deployments to Azure Functions if your app fits a serverless function model.
Summary
Configuring CircleCI to deploy a Golang app to Azure involves:
- Dockerizing the application for consistent environment packaging.
- Creating and configuring Azure resources including Container Registry and Web App.
- Setting up an Azure service principal for authentication.
- Writing a CircleCI pipeline to run tests, build and push Docker images, and deploy updated containers to Azure.
- Managing environment variables securely in CircleCI.
- Connecting CircleCI to your GitHub repository for continuous integration triggers.
This setup enables automated, reliable deployments of Go applications, leveraging the strengths of CircleCI and Azure together. The process ensures that every commit is verified, packaged, and deployed smoothly to Azure infrastructure, providing an efficient CI/CD workflow for Go applications.
For more detailed instructions, including sample code and configuration files, see CircleCI's official posts on deploying Dockerized Go apps to Azure and Azure Web App deployment workflows.