diff --git a/.github/workflows/cd.yaml b/.github/workflows/cd.yaml index c590711..586493e 100644 --- a/.github/workflows/cd.yaml +++ b/.github/workflows/cd.yaml @@ -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 diff --git a/README.md b/README.md index 7351e81..2b6ddd6 100644 --- a/README.md +++ b/README.md @@ -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
diff --git a/cdktf/main.py b/cdktf/main.py index 7994177..8898262 100755 --- a/cdktf/main.py +++ b/cdktf/main.py @@ -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', @@ -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 = [] diff --git a/cdktf/swa.sh b/cdktf/swa.sh new file mode 100644 index 0000000..0256cc7 --- /dev/null +++ b/cdktf/swa.sh @@ -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