Skip to content

Commit

Permalink
Relocate from CMeeg/next-azure repo
Browse files Browse the repository at this point in the history
  • Loading branch information
CMeeg committed Oct 5, 2023
1 parent b82f7de commit 823b963
Show file tree
Hide file tree
Showing 56 changed files with 8,449 additions and 21 deletions.
81 changes: 81 additions & 0 deletions .azdo/pipelines/azure-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Run when commits are pushed to main branch
# TODO: Change this back to `main` when everything is done and tested - change it in the `init` step also
trigger:
branches:
include:
- v3

# Azure Pipelines workflow to deploy to Azure using azd
# To configure required secrets for connecting to Azure, run `azd pipeline config --provider azdo`

pool:
vmImage: ubuntu-latest

variables:
# `env_name` is used in deployment jobs to target the correct environment - this default value will get overridden below based on the source branch
- name: 'env_name'
value: 'unknown'

# The "production" environment is targeted when the source branch is `main`
# TODO: Change this back to `main` when everything is done and tested - change it in the `init` step also
- ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/v3') }}:
- group: production
- name: 'env_name'
value: 'production'

jobs:
- job: init
steps:
- pwsh: |
Write-Output "environment is set to '$(env_name)'"
displayName: Show environment
- deployment: deploy
dependsOn: init
condition: ne(variables['env_name'], 'unknown')
environment: '$(env_name)'
# Use azd provided container image that has azd, infra, multi-language build tools pre-installed
container: mcr.microsoft.com/azure-dev-cli-apps:latest
strategy:
runOnce:
deploy:
steps:
- checkout: self

- pwsh: |
Write-Output "environment is set to '$(env_name)'"
displayName: Show environment
- pwsh: |
.azure/scripts/create-env-local.ps1
displayName: Create .env.local file
- pwsh: |
azd config set auth.useAzCliAuth "true"
displayName: Configure AZD to Use AZ CLI Authentication
- task: AzureCLI@2
displayName: Provision Infrastructure
inputs:
azureSubscription: azconnection
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
azd provision --no-prompt
env:
AZURE_ENV_NAME: $(AZURE_ENV_NAME)
AZURE_LOCATION: $(AZURE_LOCATION)
AZURE_SUBSCRIPTION_ID: $(AZURE_SUBSCRIPTION_ID)

- task: AzureCLI@2
displayName: Deploy Application
inputs:
azureSubscription: azconnection
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
azd deploy --no-prompt
env:
AZURE_ENV_NAME: $(AZURE_ENV_NAME)
AZURE_LOCATION: $(AZURE_LOCATION)
AZURE_SUBSCRIPTION_ID: $(AZURE_SUBSCRIPTION_ID)
40 changes: 40 additions & 0 deletions .azure/hooks/postprovision.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
function Merge-EnvFiles {
param(
[Parameter(Mandatory=$true)]
[string]$base,
[Parameter(Mandatory=$true)]
[string]$with,
[Parameter(Mandatory=$true)]
[string]$output
)

$hash = @{}

Get-Content $base,$with | ForEach-Object {
$key, $value = $_ -split '=', 2
$hash[$key] = $value
}

$hash.Keys | Sort-Object | ForEach-Object {
"$_=$($hash[$_])"
} | Out-File $output
}

$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path

# Before we can build the app we need to merge the local env file (if one exists) with the azd env file as it contains values required for the build to succeed
$envLocal = Join-Path $scriptDir "../../.env.local"
$envAzd = Join-Path $scriptDir "../${env:AZURE_ENV_NAME}/.env"

# The result will be output to this location and the Dockerfile will copy and rename it to .env.local during the build step
$envAzure = Join-Path $scriptDir "../../.env.azure"

if (!(Test-Path $envLocal -PathType Leaf)) {
# local env file does not exist so just copy the azd env file to .env.azure
Copy-Item $envAzd -Destination $envAzure

return
}

# Merge the azd env file (output from running `azd provision`) with the local env file - if there are duplicate keys the values from the azd env file will take precedence
Merge-EnvFiles -base $envLocal -with $envAzd -output $envAzure
4 changes: 4 additions & 0 deletions .azure/hooks/preprovision.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path

# Run script to generate an `env-vars.json` file used by the infra scripts
& $(Join-Path $scriptDir "../scripts/create-infra-env-vars.ps1")
47 changes: 47 additions & 0 deletions .azure/scripts/create-env-local.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path

# Create a `.env.local` file by merging values from the current environment with a template

$templatePath = Join-Path $scriptDir "../../.env.local.template"

if (!(Test-Path $templatePath -PathType Leaf)) {
# Template file does not exist so we can't go any further

return
}

$outputPath = Join-Path $scriptDir "../../.env.local"

if (Test-Path $outputPath -PathType Leaf) {
# We only want to create the `.env.local` file if it does not already exist
# In the development environment the developer should be in control of their `.env.local` file so it should exist
# In CI the `.env.local` file should not exist as it should not be comitted to the repo

return
}

# Read the template file

$template = Get-Content -raw $templatePath | ConvertFrom-StringData

# For each key in the template, check if there is a corresponding environment variable and if so, add it to an object

$envVars = @{}

$template.GetEnumerator() | ForEach-Object {
$key = $_.Name

if (Test-Path "env:$key") {
$value = Get-ChildItem "env:$key" | Select-Object -ExpandProperty Value

$envVars[$key] = $value
} else {
$envVars[$key] = ""
}
}

# Write the object to the output file in env file format

$envVars.Keys | Sort-Object | ForEach-Object {
"$_=$($envVars[$_])"
} | Out-File $outputPath
69 changes: 69 additions & 0 deletions .azure/scripts/create-infra-env-vars.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
function Remove-Quotes {
param(
[Parameter(Mandatory=$true)]
[string]$value,
[string]$quoteChar = '"'
)

if ($value.StartsWith($quoteChar) -and $value.EndsWith($quoteChar)) {
return $value.Substring(1, $value.Length - 2)
}

return $value
}

function Read-EnvVars {
param(
[Parameter(Mandatory=$true)]
[string]$path
)

$envVars = @{}

if (!(Test-Path $path -PathType Leaf)) {
# File does not exist so there is nothing to do

return $envVars
}

$content = Get-Content -raw $path | ConvertFrom-StringData

$content.GetEnumerator() | Foreach-Object {
$key, $value = $_.Name, $_.Value

if (($null -eq $value) -or ($value -eq "")) {
$envVars[$key] = ""
} else {
$value = Remove-Quotes -value $value -quoteChar '"'
$value = Remove-Quotes -value $value -quoteChar "'"

$envVars[$key] = $value
}
}

return $envVars
}

$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path

# Read `.env`, `.env.production` and `.env.local` files into memory

$envPath = Join-Path $scriptDir "../../.env"
$env = Read-EnvVars -path $envPath

$envProductionPath = Join-Path $scriptDir "../../.env.production"
$envProduction = Read-EnvVars -path $envProductionPath

$envLocalPath = Join-Path $scriptDir "../../.env.local"
$envLocal = Read-EnvVars -path $envLocalPath

# Merge `.env.production` and `.env.local` into `.env` (duplicate keys will be overwritten)

$env += $envProduction
$env += $envLocal

# Produce a `env-vars.json` file that can be used by the infra scripts

$outputPath = Join-Path $scriptDir "../../infra/env-vars.json"

$env | ConvertTo-Json | Out-File -FilePath $outputPath -Encoding utf8
30 changes: 30 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "next-azure",
"image": "mcr.microsoft.com/devcontainers/javascript-node:18-bullseye",
"features": {
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
"ghcr.io/azure/azure-dev/azd:latest": {}
},
"customizations": {
"vscode": {
"extensions": [
"dbaeumer.vscode-eslint",
"EditorConfig.EditorConfig",
"esbenp.prettier-vscode",
"GitHub.vscode-github-actions",
"ms-azuretools.azure-dev",
"ms-azuretools.vscode-bicep",
"ms-azuretools.vscode-docker",
"ms-vscode.js-debug",
"ms-vscode.vscode-node-azure-pack",
"ms-vscode-remote.remote-containers"
]
}
},
"forwardPorts": [3000],
"postCreateCommand": "",
"remoteUser": "node",
"hostRequirements": {
"memory": "4gb"
}
}
9 changes: 9 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.git
.next
.vscode
node_modules
.dockerignore
Dockerfile
npm-debug.log
README.md
TODO.md
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{js,json,jsx,ts,tsx}]
indent_style = space
indent_size = 2
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Set defaults for all environments here
SERVICE_WEB_MIN_LOG_LEVEL=30
PROJECT_NAME="na"
SERVICE_WEB_SERVICE_NAME="web"
6 changes: 6 additions & 0 deletions .env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Set defaults for the development build (i.e. `next dev`) here
APP_ENV="dev"
BASE_URL="http://localhost:3000"
NEXT_COMPRESS="true"
NODE_ENV="development"
SERVICE_WEB_MIN_LOG_LEVEL=10
13 changes: 13 additions & 0 deletions .env.local.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This file is a template for `.env.local`
# For development copy this file to `.env.local`, fill in the values of things you need, remove the value of anything you don't but keep the keys in place
# In CI this file is used to create a `.env.local` specific to the target environment so it's important to keep this file up to date as you add new env vars for your app or infra and update the `main.bicep` file accordingly
# You can include values in here as an example, but don't include any secrets or sensitive data that you don't want to commit to your repo - any values set here will be ignored when creating the `.env.local` file in CI

# web app settings
## infra settings
SERVICE_WEB_CONTAINER_CPU_CORE_COUNT=
SERVICE_WEB_CONTAINER_MEMORY=
SERVICE_WEB_CONTAINER_MIN_REPLICAS=
SERVICE_WEB_CONTAINER_MAX_REPLICAS=
SERVICE_WEB_CUSTOM_DOMAIN_NAME=
SERVICE_WEB_CUSTOM_DOMAIN_CERT_ID=
3 changes: 3 additions & 0 deletions .env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Set defaults for the production build (i.e. `next dev`) here
NEXT_COMPRESS="false"
NODE_ENV="production"
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["next/core-web-vitals", "plugin:prettier/recommended"]
}
Loading

0 comments on commit 823b963

Please sign in to comment.