|
| 1 | +# Parameters |
| 2 | +SOURCE_FILES ?= $(shell find . -type f -name '*.bicep' -print) |
| 3 | +OUT_DIR ?= ./artifacts |
| 4 | +BICEP_MAIN ?= ./main.bicep |
| 5 | +BICEP_PARAMETERS ?= ./main.bicepparam |
| 6 | + |
| 7 | +# Git |
| 8 | +GIT_REVISION ?= $(shell git rev-parse --short HEAD) |
| 9 | +GIT_TAG ?= $(shell git describe --tags --abbrev=0 --always | sed -e s/v//g) |
| 10 | + |
| 11 | +# Azure |
| 12 | +SUBSCRIPTION_ID ?= $(shell az account show --query id --output tsv) |
| 13 | +SUBSCRIPTION_NAME ?= $(shell az account show --query name --output tsv) |
| 14 | +TENANT_ID ?= $(shell az account show --query tenantId --output tsv) |
| 15 | +OBJECT_ID ?= $(shell az account show --query user.name --output tsv) |
| 16 | +USER_ID ?= $(shell az ad user show --id $(OBJECT_ID) --query id --output tsv) |
| 17 | +RESOURCE_GROUP_NAME ?= rg-workshop-azure-openai |
| 18 | +LOCATION ?= japaneast |
| 19 | +DEPLOYMENT_NAME ?= main |
| 20 | + |
| 21 | +.PHONY: help |
| 22 | +help: |
| 23 | + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' |
| 24 | +.DEFAULT_GOAL := help |
| 25 | + |
| 26 | +.PHONY: info |
| 27 | +info: ## show information |
| 28 | + @echo "GIT_REVISION: $(GIT_REVISION)" |
| 29 | + @echo "GIT_TAG: $(GIT_TAG)" |
| 30 | + @echo "SUBSCRIPTION_ID: $(SUBSCRIPTION_ID)" |
| 31 | + @echo "SUBSCRIPTION_NAME: $(SUBSCRIPTION_NAME)" |
| 32 | + @echo "TENANT_ID: $(TENANT_ID)" |
| 33 | + @echo "OBJECT_ID: $(OBJECT_ID)" |
| 34 | + @echo "USER_ID: $(USER_ID)" |
| 35 | + |
| 36 | +.PHONY: install-deps-dev |
| 37 | +install-deps-dev: ## install dependencies for development |
| 38 | + @which az || echo "Please install Azure CLI: https://github.com/Azure/azure-cli#installation" |
| 39 | + @az bicep upgrade |
| 40 | + |
| 41 | +.PHONY: format |
| 42 | +format: ## format codes |
| 43 | + @$(foreach file,$(SOURCE_FILES),az bicep format --file $(file) --insert-final-newline;) |
| 44 | + |
| 45 | +.PHONY: lint |
| 46 | +lint: ## lint codes |
| 47 | + @echo "lint: Skip since not implemented yet" |
| 48 | + |
| 49 | +.PHONY: build |
| 50 | +build: ## build a bicep file |
| 51 | + @mkdir -p $(OUT_DIR) |
| 52 | + @az bicep build \ |
| 53 | + --file $(BICEP_MAIN) \ |
| 54 | + --outfile $(OUT_DIR)/$(GIT_REVISION).json |
| 55 | + |
| 56 | +.PHONY: test |
| 57 | +test: deployment-what-if ## test codes |
| 58 | + |
| 59 | +.PHONY: ci-test |
| 60 | +ci-test: install-deps-dev lint build test ## ci test |
| 61 | + |
| 62 | +.PHONY: create-resource-group |
| 63 | +create-resource-group: ## create resource group |
| 64 | + az group create \ |
| 65 | + --name $(RESOURCE_GROUP_NAME) \ |
| 66 | + --location $(LOCATION) |
| 67 | + |
| 68 | +.PHONY: delete-resource-group |
| 69 | +delete-resource-group: ## delete resource group |
| 70 | + az group delete --name $(RESOURCE_GROUP_NAME) --yes --no-wait |
| 71 | + |
| 72 | +.PHONY: deployment-what-if |
| 73 | +deployment-what-if: ## execute a deployment What-If operation at resource group scope |
| 74 | + az deployment group what-if \ |
| 75 | + --resource-group $(RESOURCE_GROUP_NAME) \ |
| 76 | + --template-file $(BICEP_MAIN) \ |
| 77 | + --parameters $(BICEP_PARAMETERS) |
| 78 | + |
| 79 | +.PHONY: deployment-create |
| 80 | +deployment-create: ## start a deployment at resource group |
| 81 | + az deployment group create \ |
| 82 | + --resource-group $(RESOURCE_GROUP_NAME) \ |
| 83 | + --template-file $(BICEP_MAIN) \ |
| 84 | + --parameters $(BICEP_PARAMETERS) |
| 85 | + |
| 86 | +.PHONY: deployment-output |
| 87 | +deployment-output: ## show deployment output |
| 88 | + az deployment group show \ |
| 89 | + --resource-group $(RESOURCE_GROUP_NAME) \ |
| 90 | + --name $(DEPLOYMENT_NAME) \ |
| 91 | + --query properties.outputs.deploymentInfo.value |
| 92 | + |
| 93 | +.PHONY: deploy |
| 94 | +deploy: create-resource-group deployment-what-if deployment-create ## deploy resources |
| 95 | + |
| 96 | +.PHONY: destroy |
| 97 | +destroy: delete-resource-group ## destroy resources |
| 98 | + |
| 99 | +# Generate deployment credentials: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/deploy-github-actions?tabs=userlevel%2CCLI#generate-deployment-credentials |
| 100 | +.PHONY: create-for-rbac |
| 101 | +create-for-rbac: ## create service principal for RBAC |
| 102 | + az ad sp create-for-rbac \ |
| 103 | + --name test-baseline-environment-on-azure-bicep \ |
| 104 | + --role contributor \ |
| 105 | + --scopes /subscriptions/$(SUBSCRIPTION_ID)/resourceGroups/$(RESOURCE_GROUP_NAME) \ |
| 106 | + --sdk-auth > $(OUT_DIR)/azure-credentials.json |
| 107 | + |
| 108 | +# Configure the GitHub secrets: https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/deploy-github-actions?tabs=userlevel%2CCLI#configure-the-github-secrets |
| 109 | +.PHONY: configure-github-secrets |
| 110 | +configure-github-secrets: ## configure GitHub secrets |
| 111 | + gh secret set AZURE_CREDENTIALS < $(OUT_DIR)/azure-credentials.json |
| 112 | + gh secret set AZURE_SUBSCRIPTION --body $(SUBSCRIPTION_ID) |
| 113 | + gh secret set AZURE_RG --body $(RESOURCE_GROUP_NAME) |
0 commit comments