Feature/32 deploy api as azure function app #34

Merged
noahspannbauer merged 7 commits from feature/32-deploy-api-as-azure-function-app into main 2024-11-29 23:51:16 -05:00
17 changed files with 395 additions and 207 deletions

62
.github/workflows/api.yaml vendored Normal file
View File

@@ -0,0 +1,62 @@
name: 'API Build and Deploy'
on:
workflow_call:
inputs:
calling_workflow:
required: true
type: string
jobs:
build:
runs-on: ubuntu-latest
environment: ${{ inputs.calling_workflow }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: true
- name: 'Setup Node'
uses: actions/setup-node@v4
with:
node-version: ${{ vars.NODE_VERSION }}
- name: Install
run: |
npm install
- name: Build
shell: bash
run: |
npm run build -w api
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log into Docker Hub
uses: docker/login-action@v3
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push to registry
uses: docker/build-push-action@v6
with:
push: true
tags: noahspan/flying-api:${{ github.run_id }}
file: ./api/Dockerfile
context: ./api
deploy:
runs-on: ubuntu-latest
environment: ${{ inputs.calling_workflow }}
needs: build
steps:
- name: Log in to Azure
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Update Container App
run: |
az container app update --name flying-api --resource-group noahspan --image noahspan-flying:${{ github.run_id }}

48
.github/workflows/app.yaml vendored Normal file
View File

@@ -0,0 +1,48 @@
name: 'App Build and Deploy'
on:
workflow_call:
inputs:
calling_workflow:
required: true
type: string
deployment_environment:
required: true
type: string
jobs:
build-and-deploy:
runs-on: ubuntu-latest
environment: ${{ inputs.calling_workflow }}
env:
VITE_API_URL: ${{ secrets.VITE_API_URL }}
VITE_CLIENT_ID: ${{ secrets.VITE_CLIENT_ID }}
VITE_TENANT_ID: ${{ secrets.VITE_TENANT_ID }}
VITE_REDIRECT_URL: ${{ secrets.VITE_REDIRECT_URL }}
steps:
- uses: actions/checkout@v4
with:
submodules: true
- name: 'Setup Node'
uses: actions/setup-node@v4
with:
node-version: ${{ vars.NODE_VERSION }}
- name: Install
run: |
npm install
- name: Build
run: |
npm run build -w app
- name: Deploy
id: builddeploy
uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }}
repo_token: ${{ secrets.GITHUB_TOKEN }}
action: 'upload'
skip_app_build: true
app_location: ${{ vars.APP_LOCATION }}
deployment_environment: ${{ inputs.deployment_environment }}

21
.github/workflows/dev.yaml vendored Normal file
View File

@@ -0,0 +1,21 @@
name: Dev
on:
push:
branches:
- feature/**
jobs:
build-deploy-api:
name: build-deploy-api
uses: ./.github/workflows/api.yaml
with:
calling_workflow: dev
secrets: inherit
build-deploy-app:
name: build-deploy-app
uses: ./.github/workflows/app.yaml
with:
calling_workflow: dev
deployment_environment: development
secrets: inherit

View File

@@ -1,43 +0,0 @@
name: Feature
on:
push:
branches:
- feature/**
env:
VITE_CLIENT_ID: ${{ vars.VITE_CLIENT_ID }}
VITE_TENANT_ID: ${{ vars.VITE_TENANT_ID }}
VITE_REDIRECT_URL: ${{ vars.VITE_REDIRECT_URL }}
jobs:
build_and_deploy_job:
runs-on: ubuntu-latest
name: Build and Deploy Job
steps:
- uses: actions/checkout@v4
with:
submodules: true
- name: Install
run: |
npm install
- name: Build API
run: |
npm run build -w api
- name: Build and Deploy
id: builddeploy
uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }}
repo_token: ${{ secrets.GITHUB_TOKEN }}
action: 'upload'
app_build_command: ${{ vars.APP_BUILD_COMMAND }}
app_location: ${{ vars.APP_LOCATION }}
# api_build_command: ${{ vars.API_BUILD_COMMAND }}
api_location: ${{ vars.API_LOCATION }}
output_location: ${{ vars.OUTPUT_LOCATION }}
deployment_environment: 'development'
skip_api_build: true

37
.github/workflows/pull_request.yaml vendored Normal file
View File

@@ -0,0 +1,37 @@
name: Pull Request
on:
pull_request:
types: [opened, synchronize, reopened, closed]
branches:
- main
jobs:
build-deploy-api:
if: github.event_name == 'pull_request' && github.event.action != 'closed'
name: build-deploy-api
uses: ./.github/workflows/api.yaml
with:
calling_workflow: dev
secrets: inherit
build-deploy-app:
if: github.event_name == 'pull_request' && github.event.action != 'closed'
name: build-deploy-app
uses: ./.github/workflows/app.yaml
with:
calling_workflow: dev
deployment_environment: staging
secrets: inherit
close-pull-request-job:
if: github.event_name == 'pull_request' && github.event.action == 'closed'
runs-on: ubuntu-latest
name: Close Pull Request Job
steps:
- name: Close Pull Request
id: closepullrequest
uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }}
action: 'close'

View File

@@ -1,43 +0,0 @@
name: Pull Request
on:
pull_request:
types: [opened, synchronize, reopened, closed]
branches:
- main
jobs:
build_and_deploy_job:
if: github.event_name == 'pull_request' && github.event.action != 'closed'
runs-on: ubuntu-latest
name: Build and Deploy Job
steps:
- uses: actions/checkout@v4
with:
submodules: true
lfs: false
- name: Build And Deploy
id: builddeploy
uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }}
repo_token: ${{ secrets.GITHUB_TOKEN }}
action: 'upload'
app_build_command: ${{ vars.APP_BUILD_COMMAND }}
app_location: ${{ vars.APP_LOCATION }}
api_build_command: ${{ vars.API_BUILD_COMMAND }}
api_location: ${{ vars.API_LOCATION}}
output_location: ${{ vars.OUTPUT_LOCATION }}
deployment_environment: 'staging'
close_pull_request_job:
if: github.event_name == 'pull_request' && github.event.action == 'closed'
runs-on: ubuntu-latest
name: Close Pull Request Job
steps:
- name: Close Pull Request
id: closepullrequest
uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }}
action: 'close'

View File

@@ -1,2 +1,3 @@
local.settings.json
node_modules node_modules
test test

View File

@@ -1,50 +1,11 @@
# To enable ssh & remote debugging on app service change the base image to the one below # To enable ssh & remote debugging on app service change the base image to the one below
# FROM mcr.microsoft.com/azure-functions/node:4-node18-appservice # FROM mcr.microsoft.com/azure-functions/node:4-node18-appservice
# FROM mcr.microsoft.com/azure-functions/node:4-node18
# ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
# AzureFunctionsJobHost__Logging__Console__IsEnabled=true
# COPY . /home/site/wwwroot
# RUN cd /home/site/wwwroot && \
# npm install
# EXPOSE 7071
# CMD ["npm", "run", "start:azure"]
# FROM mcr.microsoft.com/azure-functions/node:4-node18 AS build
# WORKDIR /home/site/wwwroot
# COPY . .
# RUN npm install && \
# npm run build
FROM mcr.microsoft.com/azure-functions/node:4-node18 FROM mcr.microsoft.com/azure-functions/node:4-node18
WORKDIR /home/site/wwwroot ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
AzureFunctionsJobHost__Logging__Console__IsEnabled=true
COPY . . COPY . /home/site/wwwroot
RUN npm install -g azure-functions-core-tools@4 --unsafe-perm true RUN cd /home/site/wwwroot && \
RUN npm install npm install
# ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
# AzureFunctionsJobHost__Logging__Console__IsEnabled=true
# AzureFunctionsJobHost__extensions__cors__allowedOrigins="[\"*\"]" \
# AzureFunctionsJobHost__extensions__cors__supportCredentials=false
# COPY --from=build /home/site/wwwroot/node_modules /home/site/wwwroot/node_modules
# COPY --from=build /home/site/wwwroot/dist /home/site/wwwroot/dist
# EXPOSE 3000
# CMD ["node", "/home/site/wwwroot/dist/src/main.js"]
CMD ["npm", "run", "start:azure"]

View File

@@ -105,4 +105,10 @@ export class AppController {
return error; return error;
} }
} }
@Public()
@Get('hello')
async getHello(): Promise<string> {
return this.appService.getHello();
}
} }

View File

@@ -8,6 +8,7 @@ async function bootstrap() {
const httpService = new HttpService(); const httpService = new HttpService();
const app = await NestFactory.create(AppModule); const app = await NestFactory.create(AppModule);
app.setGlobalPrefix('api');
app.useGlobalFilters(new HttpExceptionFilter()); app.useGlobalFilters(new HttpExceptionFilter());
httpService.axiosRef.interceptors.response.use( httpService.axiosRef.interceptors.response.use(
(response) => { (response) => {

View File

@@ -12,36 +12,36 @@ const App: React.FC<unknown> = () => {
const httpClient: AxiosInstance = useHttpClient(); const httpClient: AxiosInstance = useHttpClient();
const appContext = useAppContext(); const appContext = useAppContext();
useEffect(() => { // useEffect(() => {
const getFeatureFlags = async () => { // const getFeatureFlags = async () => {
try { // try {
const featureFlagKeys: string = 'flying-pilots'; // const featureFlagKeys: string = 'flying-pilots';
const response: AxiosResponse = await httpClient.get( // const response: AxiosResponse = await httpClient.get(
`api/featureFlags?keys=${featureFlagKeys}&label=${import.meta.env.MODE}` // `api/featureFlags?keys=${featureFlagKeys}&label=${import.meta.env.MODE}`
); // );
const featureFlags: { key: string; enabled: boolean }[] = response.data; // const featureFlags: { key: string; enabled: boolean }[] = response.data;
if (featureFlags.length > 0) { // if (featureFlags.length > 0) {
appContext.dispatch({ // appContext.dispatch({
type: 'SET_FEATURE_FLAGS', // type: 'SET_FEATURE_FLAGS',
payload: featureFlags // payload: featureFlags
}); // });
} // }
} catch (error) { // } catch (error) {
console.log(error); // console.log(error);
} // }
}; // };
getFeatureFlags(); // getFeatureFlags();
}, []); // }, []);
return ( return (
<div> <div>
<SiteNav /> <SiteNav />
<Routes> <Routes>
{useFeatureFlag('flying-pilots')?.enabled && ( {/* {useFeatureFlag('flying-pilots')?.enabled && ( */}
<Route path="/pilots" element={<Pilots />} /> <Route path="/pilots" element={<Pilots />} />
)} {/* )} */}
<Route path="/" element={<Logbook />} /> <Route path="/" element={<Logbook />} />
</Routes> </Routes>
</div> </div>

View File

@@ -1,22 +1,27 @@
import axios, { AxiosInstance, CreateAxiosDefaults } from 'axios'; import axios, { AxiosInstance, CreateAxiosDefaults } from 'axios';
export const useHttpClient = () => { export const useHttpClient = () => {
let config: CreateAxiosDefaults<any>; let config: CreateAxiosDefaults<any> = {
baseURL: import.meta.env.VITE_API_URL,
headers: {
'Content-Type': 'application/json'
}
};
if (import.meta.env.DEV) { // if (import.meta.env.DEV) {
config = { // config = {
baseURL: import.meta.env.VITE_API_URL, // baseURL: import.meta.env.VITE_API_URL,
headers: { // headers: {
'Content-Type': 'application/json' // 'Content-Type': 'application/json'
} // }
}; // };
} else { // } else {
config = { // config = {
headers: { // headers: {
'Content-Type': 'application/json' // 'Content-Type': 'application/json'
} // }
}; // };
} // }
const httpClient: AxiosInstance = axios.create(config); const httpClient: AxiosInstance = axios.create(config);

View File

@@ -2,20 +2,20 @@
# Manual edits may be lost in future updates. # Manual edits may be lost in future updates.
provider "registry.terraform.io/hashicorp/azurerm" { provider "registry.terraform.io/hashicorp/azurerm" {
version = "3.104.2" version = "4.11.0"
hashes = [ hashes = [
"h1:1J+ajk1s1qfjViKYSOYDb8HLOh2RIn/TAK/2s3orPuE=", "h1:2YQgcxXJ7ZpJyo7bcKhhMrUEg4s/kUA1lcbYoFuN/qM=",
"zh:05b4a3572ce2b881fef5ec64756b060e8ce6c24c260182acf4adec38a6b29204", "zh:026808a5ff8bce161518d503bfc57c4a95637d67e923a94382c8e878c96aaf00",
"zh:0d5118f6ad64278a52b720cdbf1a6b7ab7ea1ad5bd3d9607cb558d8d25280906", "zh:13473ebb56ed701fdd8c288a220cef3ec6ee170fb1ac45c6ce5a612848e64690",
"zh:2196f49d73bf862a046b24e143f5d658bbb01bfb4e8582a88eb3907ff4f69730", "zh:36667374d31509456fd928f651fc1ccc7438c53bc99cf9ec3b6ec6e7f791394e",
"zh:285c1a65bf3b70859110c2bbafefa4483d450840282a57f349b81b17367bbb26", "zh:5f44e16aab36a93391ce81b9a93b694fecf11f71615f2414ee40bb5e211d3dbb",
"zh:2efbd00970952761d60043c41e983dc6930678ef179de2b27ed00437fa711703", "zh:9310e860f9236d0f7171e05444ca85e239f0938b9fb08ec3bfd9712a14013308",
"zh:6b7e26e6ba3a639e2a26b2e64f4629e28a44a9572f4203c30cb1c611f37ddb21", "zh:aaf6ea1f68526a175e84424710b06dd6cf8987b404206cc581692560c1530810",
"zh:8149b7aada49cac3ef49d7595d2fc2e3a573f4c01d272a6a4111efa089f2e44f", "zh:b6d1965af0aed85f3eccaaec5dae90f59632bf07e2bf5b7473359a7c761872a5",
"zh:9674f741d7be268778a0f0a59174130800f8977747ef16a1dd6446031c7ae8d4", "zh:c642675ea2d8e1f1bb440016238ab25fa4270cb155b01e90598161488df47128",
"zh:aed0e78df3c5de8eaa8c8cacb4e3c48ec26683f2e35dd42eabc1242592fad247", "zh:d22d07834c2a5da6ce7054699d4f708277fccb63436cfbf6c90c58cddddba408",
"zh:c0c97188d9a5a26c5ce2dbcc1c6b31fb73469bb2e422e64a1dda25c9355c341c", "zh:eceb91d652ea9145531129c7da50603e9415812f639acbf1720d51f878798fb8",
"zh:e883eca472593e34f2f93282973c148114eab19fceb8348fc82e91293b247118", "zh:f26bf55ce68c1ed6e316ee70652bc3cc357987ea4b3caf6f835405850c6897e0",
"zh:f569b65999264a9416862bca5cd2a6177d94ccb0424f3a4ef424428912b9cb3c", "zh:f569b65999264a9416862bca5cd2a6177d94ccb0424f3a4ef424428912b9cb3c",
] ]
} }

View File

@@ -0,0 +1,91 @@
resource "azurerm_container_app" "container_app_dev" {
name = "flying-api"
container_app_environment_id = azurerm_container_app_environment.container_app_environment_dev.id
resource_group_name = data.azurerm_resource_group.resource_group.name
revision_mode = "Single"
registry {
server = "index.docker.io"
username = var.DOCKER_IO_USERNAME
password_secret_name = "docker-io-password"
}
template {
min_replicas = 0
max_replicas = 1
container {
name = "flying-api-dev"
image = "noahspan/flying-api:v0.0.1"
cpu = 0.25
memory = "0.5Gi"
env {
name = "APP_CONFIG_URL"
value = "https://noahspann-appconfig.azconfig.io"
}
env {
name = "AZURE_STORAGE_ACCOUNT_KEY"
secret_name = "azure-storage-account-key"
}
env {
name = "AZURE_STORAGE_ACCOUNT_NAME"
value = "noahspanflyingdev"
}
env {
name = "AZURE_STROAGE_ACCOUNT_URL"
value = "https://noahspanflyingdev.table.core.windows.net/"
}
env {
name = "CLIENT_ID"
secret_name = "client-id"
}
env {
name = "CLIENT_SECRET"
secret_name = "client-secret"
}
env {
name = "TENANT_ID"
value = "0f23652e-4b15-420f-991e-3d6fc769a31d"
}
}
}
ingress {
allow_insecure_connections = false
external_enabled = true
target_port = 80
transport = "auto"
traffic_weight {
latest_revision = true
percentage = 100
}
}
secret {
name = "azure-storage-account-key"
value = var.AZURE_STORAGE_ACCOUNT_KEY
}
secret {
name = "client-id"
value = var.CLIENT_ID
}
secret {
name = "client-secret"
value = var.CLIENT_SECRET
}
secret {
name = "docker-io-password"
value = var.DOCKER_IO_PASSWORD
}
}

View File

@@ -0,0 +1,14 @@
resource "azurerm_log_analytics_workspace" "log_analytics_workspace" {
name = "noahspan-flying-log-analytics-workspace"
location = data.azurerm_resource_group.resource_group.location
resource_group_name = data.azurerm_resource_group.resource_group.name
sku = "PerGB2018"
retention_in_days = 30
}
resource "azurerm_container_app_environment" "container_app_environment_dev" {
name = "flying-dev"
location = data.azurerm_resource_group.resource_group.location
resource_group_name = data.azurerm_resource_group.resource_group.name
log_analytics_workspace_id = azurerm_log_analytics_workspace.log_analytics_workspace.id
}

View File

@@ -1,21 +1,13 @@
# module "static_web_app" {
# source = "github.com/noahspannbauer/noahspan-root/infrastructure/modules/static_web_app"
# region = var.REGION
# resource_group_name = data.azurerm_resource_group.resource_group.name
# static_web_app_name = var.STATIC_WEB_APP_NAME
# custom_domain_name_count = var.CUSTOM_DOMAIN_NAME_COUNT
# domain_name = var.DOMAIN_NAME
# subdomain_name = var.SUBDOMAIN_NAME
# }
resource "azurerm_static_web_app" "static_web_app" { resource "azurerm_static_web_app" "static_web_app" {
name = var.STATIC_WEB_APP_NAME name = var.STATIC_WEB_APP_NAME
resource_group_name = data.azurerm_resource_group.resource_group.name resource_group_name = data.azurerm_resource_group.resource_group.name
location = var.REGION location = var.REGION
sku_size = "Free"
sku_tier = "Free"
} }
resource "azurerm_dns_cname_record" "dns_cname_record" { resource "azurerm_dns_cname_record" "dns_cname_record_app" {
name = var.SUBDOMAIN_NAME name = var.APP_SUBDOMAIN_NAME
zone_name = data.azurerm_dns_zone.dns_zone.name zone_name = data.azurerm_dns_zone.dns_zone.name
resource_group_name = data.azurerm_resource_group.resource_group.name resource_group_name = data.azurerm_resource_group.resource_group.name
ttl = 14400 ttl = 14400
@@ -24,8 +16,8 @@ resource "azurerm_dns_cname_record" "dns_cname_record" {
resource "azurerm_static_web_app_custom_domain" "static_web_app_custom_domain" { resource "azurerm_static_web_app_custom_domain" "static_web_app_custom_domain" {
static_web_app_id = azurerm_static_web_app.static_web_app.id static_web_app_id = azurerm_static_web_app.static_web_app.id
domain_name = "${var.SUBDOMAIN_NAME}.${var.DOMAIN_NAME}" domain_name = "${var.APP_SUBDOMAIN_NAME}.${var.DOMAIN_NAME}"
validation_type = "cname-delegation" validation_type = "cname-delegation"
depends_on = [ azurerm_dns_cname_record.dns_cname_record ] depends_on = [ azurerm_dns_cname_record.dns_cname_record_app ]
} }

View File

@@ -1,3 +1,42 @@
variable "API_SUBDOMAIN_NAME" {
type = string
}
variable "APP_SUBDOMAIN_NAME" {
type = string
}
variable "AZURE_STORAGE_ACCOUNT_KEY" {
type = string
sensitive = true
}
variable "CLIENT_ID" {
type = string
}
variable "CLIENT_SECRET" {
type = string
sensitive = true
}
variable "DOCKER_IO_PASSWORD" {
type = string
sensitive = true
}
variable "DOCKER_IO_USERNAME" {
type = string
}
variable "DOMAIN_NAME" {
type = string
}
variable "CUSTOM_DOMAIN_NAME_COUNT" {
type = number
}
variable "REGION" { variable "REGION" {
type = string type = string
} }
@@ -10,19 +49,15 @@ variable "STATIC_WEB_APP_NAME" {
type = string type = string
} }
variable "DOMAIN_NAME" {
type = string
}
variable "SUBDOMAIN_NAME" {
type = string
}
variable "CUSTOM_DOMAIN_NAME_COUNT" {
type = number
}
variable "STORAGE_ACCOUNT_NAME" { variable "STORAGE_ACCOUNT_NAME" {
type = string type = string
} }
variable "TENANT_ID" {
type = string
}
variable "WORKSPACE" {
type = string
}