Skip to content

Commit

Permalink
Deploy static files to Azure SWA
Browse files Browse the repository at this point in the history
  • Loading branch information
filipopo committed Dec 11, 2024
1 parent c41df33 commit 07b2756
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 16 deletions.
54 changes: 41 additions & 13 deletions .github/workflows/cd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,69 @@ on:
required: true
type: string

env:
TF_TOKEN_app_terraform_io: ${{ secrets.TERRAFORM_TOKEN }}
SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}
TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
DJANGO_KEY: ${{ secrets.DJANGO_KEY }}
DJANGO_HOSTS: ${{ vars.DJANGO_HOSTS }}
DJANGO_CSRF: ${{ vars.DJANGO_CSRF }}
DJANGO_STATIC: ${{ vars.DJANGO_STATIC }}
DB_NAME: ${{ secrets.DB_NAME }}
DB_USER: ${{ secrets.DB_USER }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
DB_HOST: ${{ secrets.DB_HOST }}

jobs:
prepare-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up CDK
- name: Set up CDK for Terraform
run: npm install -g cdktf-cli

- name: cdktf synth
run: cdktf synth
working-directory: infra
working-directory: cdktf

- name: Set up Terraform
uses: hashicorp/setup-terraform@v3

- name: Make Terraform plan
run: |
terraform init
terraform plan -out=tfplan
working-directory: cdktf/cdktf.out/stacks/urlshortener-stack

- name: Show Terraform plan
run: terraform show tfplan
working-directory: cdktf/cdktf.out/stacks/urlshortener-stack

- name: Upload plan as artifact
uses: actions/upload-artifact@v4
with:
name: cdktf.out
path: infra/cdktf.out
name: cdktf
path: cdktf

deploy:
needs: prepare-deploy
runs-on: ubuntu-latest
environment: 'main'
steps:
- uses: actions/checkout@v4

- name: Download plan artifact
uses: actions/download-artifact@v4
with:
name: cdktf.out
path: infra/
name: cdktf
path: ./

- name: Set up CDK
run: npm install -g cdktf-cli
- name: Set up Terraform
uses: hashicorp/setup-terraform@v3

- name: ckdtf deploy
run: cdktf deploy
working-directory: infra
- name: Deploy
run: |
terraform -chdir=cdktf.out/stacks/urlshortener-stack apply tfplan
sh swa.sh ${{ github.event.inputs.image_tag }}
working-directory: cdktf
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ There are several options for installing the app, here is a non exhaustive list:
terraform import azurerm_mssql_database.Database /subscriptions/SUBSCRIPTION_ID/resourceGroups/url-resource-group/providers/Microsoft.Sql/servers/urlshortener/databases/urlshortener
```

Running `ckdtf deploy` will automagically deploy this application to Azure, `cdktf destroy` will delete the provisioned resources
Running `ckdtf deploy` will deploy this application to Azure, `cdktf destroy` will delete the provisioned resources

The CI/CD pipleine of this repository does this for you
In `DJANGO_DEBUG=false` mode, afer the deploy you need to upload the static files to Azure SWA. The easiest way is to run `sh swa.sh`. the CI/CD pipleine of this repository does this automatically after deploys
</details>

<details>
Expand Down
9 changes: 8 additions & 1 deletion cdktf/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self, scope: Construct, id: str):
)

# Define Static Web App
_ = static_web_app.StaticWebApp(
app_web = static_web_app.StaticWebApp(
self,
'StaticWebApp',
name='urlshortener',
Expand Down Expand Up @@ -135,6 +135,13 @@ def __init__(self, scope: Construct, id: str):
value=app.ingress.fqdn
)

TerraformOutput(
self,
'swa',
sensitive=True,
value=app_web.api_key
)

def template_env(self, env):
res = []

Expand Down
13 changes: 13 additions & 0 deletions cdktf/swa.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

TAG=${1:-latest}
docker pull filipmania/urlshortener:$TAG

docker create --name url-temp filipmania/urlshortener:$TAG
docker cp url-temp:/opt/prod_static ./prod_static
docker rm url-temp

touch prod_static/index.html
SWA_CLI_DEPLOYMENT_TOKEN=$(terraform -chdir=cdktf.out/stacks/urlshortener-stack output -raw swa) \
npx --yes @azure/static-web-apps-cli deploy ./prod_static --env=production
rm -rf prod_static

0 comments on commit 07b2756

Please sign in to comment.