Skip to content

Instantly share code, notes, and snippets.

@magodo
Last active November 19, 2023 03:08
Show Gist options
  • Save magodo/287009b96ebfa6b8c173e62d6907fa0f to your computer and use it in GitHub Desktop.
Save magodo/287009b96ebfa6b8c173e62d6907fa0f to your computer and use it in GitHub Desktop.
terraform config to automatically setup an Azure static site
name: Azure Static Web Apps CI/CD
on:
push:
branches:
- main
pull_request:
types: [opened, synchronize, reopened, closed]
branches:
- main
jobs:
build_and_deploy_job:
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed')
runs-on: ubuntu-latest
name: Build and Deploy Job
steps:
- uses: actions/checkout@v2
with:
submodules: true
- name: setup vue environment file
run: |
echo "VUE_APP_NOT_SECRET_CODE=some_value" > $GITHUB_WORKSPACE/.env
- name: Build And Deploy
id: builddeploy
uses: Azure/static-web-apps-deploy@v0.0.1-preview
with:
azure_static_web_apps_api_token: $${{ secrets.${ api_token_var } }}
repo_token: $${{ secrets.GITHUB_TOKEN }} # Used for Github integrations (i.e. PR comments)
action: "upload"
###### Repository/Build Configurations - These values can be configured to match you app requirements. ######
# For more information regarding Static Web App workflow configurations, please visit: https://aka.ms/swaworkflowconfig
app_location: "${ app_location }" # App source code path
api_location: "${ api_location }" # Api source code path - optional
output_location: "${ output_location }" # Built app content directory - optional
###### End of Repository/Build Configurations ######
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@v0.0.1-preview
with:
azure_static_web_apps_api_token: $${{ secrets.${ api_token_var } }}
action: "close"
provider "azurerm" {
features {}
}
output hostname {
value = azurerm_static_site.test.default_host_name
}
resource "azurerm_resource_group" "test" {
name = "example"
location = "west europe"
}
resource "azurerm_static_site" "test" {
name = "example"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
}
locals {
api_token_var = "AZURE_STATIC_WEB_APPS_API_TOKEN"
}
variable "github_token" {}
variable "github_owner" {}
provider "github" {
token = var.github_token
owner = var.github_owner
}
resource "github_actions_secret" "test" {
repository = "my-first-static-web-app"
secret_name = local.api_token_var
plaintext_value = azurerm_static_site.test.api_key
}
# This will cause github provider crash, until https://github.com/integrations/terraform-provider-github/pull/732 is merged.
resource "github_repository_file" "foo" {
repository = "my-first-static-web-app"
branch = "main"
file = ".github/workflows/azure-static-web-app.yml"
content = templatefile("./azure-static-web-app.tpl",
{
app_location = "/"
api_location = "api"
output_location = ""
api_token_var = local.api_token_var
}
)
commit_message = "Add workflow (by Terraform)"
commit_author = "magodo"
commit_email = "wztdyl@sina.com"
overwrite_on_create = true
}
@pstrasser
Copy link

Thank you very much! Very useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment