Skip to content

Commit 36e107f

Browse files
committed
Configure Terraform with OIDC and add Azure Static Web App deployment
1 parent 8912eb3 commit 36e107f

File tree

7 files changed

+141
-3
lines changed

7 files changed

+141
-3
lines changed

.github/workflows/deploy.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Deploy to Azure Static Web App
2+
on:
3+
push:
4+
branches: [main]
5+
pull_request:
6+
types: [opened, synchronize, reopened, closed]
7+
branches: [main]
8+
9+
jobs:
10+
deploy:
11+
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed')
12+
runs-on: ubuntu-latest
13+
name: Deploy to Azure Static Web App
14+
environment: production
15+
permissions:
16+
id-token: write
17+
contents: read
18+
pull-requests: write
19+
20+
steps:
21+
- name: Checkout
22+
uses: actions/checkout@v4
23+
with:
24+
submodules: true
25+
lfs: false
26+
27+
- name: Azure Login
28+
uses: azure/login@v1
29+
with:
30+
client-id: ${{ secrets.AZURE_CLIENT_ID }}
31+
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
32+
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
33+
34+
- name: Deploy Static Web App
35+
uses: Azure/static-web-apps-deploy@v1
36+
with:
37+
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }}
38+
repo_token: ${{ secrets.GITHUB_TOKEN }}
39+
action: "upload"
40+
app_location: "/"
41+
output_location: ""
42+
43+
close_pull_request:
44+
if: github.event_name == 'pull_request' && github.event.action == 'closed'
45+
runs-on: ubuntu-latest
46+
name: Close Pull Request
47+
steps:
48+
- name: Close Pull Request
49+
uses: Azure/static-web-apps-deploy@v1
50+
with:
51+
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }}
52+
action: "close"

.github/workflows/terraform.yml

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ jobs:
5757
name: 'Terraform'
5858
runs-on: ubuntu-latest
5959
environment: production
60+
permissions:
61+
id-token: write
62+
contents: read
6063

6164
# Use the Bash shell regardless whether the GitHub Actions runner is ubuntu-latest, macos-latest, or windows-latest
6265
defaults:
@@ -68,14 +71,25 @@ jobs:
6871
- name: Checkout
6972
uses: actions/checkout@v4
7073

74+
# Azure Login with OIDC
75+
- name: Azure Login
76+
uses: azure/login@v2
77+
with:
78+
client-id: ${{ secrets.AZURE_CLIENT_ID }}
79+
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
80+
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
81+
7182
# Install the latest version of Terraform CLI and configure the Terraform CLI configuration file with a Terraform Cloud user API token
7283
- name: Setup Terraform
7384
uses: hashicorp/setup-terraform@v1
74-
with:
75-
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
7685

7786
# Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc.
7887
- name: Terraform Init
88+
env:
89+
ARM_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
90+
ARM_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
91+
ARM_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
92+
ARM_USE_OIDC: "true"
7993
run: terraform init
8094

8195
# Checks that all Terraform configuration files adhere to a canonical format
@@ -84,10 +98,20 @@ jobs:
8498

8599
# Generates an execution plan for Terraform
86100
- name: Terraform Plan
101+
env:
102+
ARM_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
103+
ARM_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
104+
ARM_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
105+
ARM_USE_OIDC: "true"
87106
run: terraform plan -input=false
88107

89108
# On push to "main", build or change infrastructure according to Terraform configuration files
90109
# Note: It is recommended to set up a required "strict" status check in your repository for "Terraform Cloud". See the documentation on "strict" required status checks for more information: https://help.github.com/en/github/administering-a-repository/types-of-required-status-checks
91110
- name: Terraform Apply
92-
if: github.ref == 'refs/heads/"main"' && github.event_name == 'push'
111+
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
112+
env:
113+
ARM_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
114+
ARM_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
115+
ARM_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
116+
ARM_USE_OIDC: "true"
93117
run: terraform apply -auto-approve -input=false

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Terraform
2+
.terraform/
3+
.terraform.lock.hcl
4+
*.tfstate
5+
*.tfstate.*
6+
*.tfvars
7+
*.tfvars.json
8+
crash.log
9+
crash.*.log
10+
override.tf
11+
override.tf.json
12+
*_override.tf
13+
*_override.tf.json
14+
.terraformrc
15+
terraform.rc

README.md

Whitespace-only changes.

index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>Hello World</h1>
2+
<h2>Austen Stone</h2>

main.tf

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Resource Group
2+
resource "azurerm_resource_group" "rg" {
3+
name = "rg-github-actions-terraform"
4+
location = "East US"
5+
}
6+
7+
# Azure Static Web App
8+
resource "azurerm_static_web_app" "swa" {
9+
name = "swa-github-actions-terraform"
10+
resource_group_name = azurerm_resource_group.rg.name
11+
location = azurerm_resource_group.rg.location
12+
sku_tier = "Free"
13+
sku_size = "Free"
14+
}
15+
16+
# Output the deployment token (sensitive)
17+
output "static_web_app_api_key" {
18+
value = azurerm_static_web_app.swa.api_key
19+
sensitive = true
20+
}
21+
22+
output "static_web_app_url" {
23+
value = azurerm_static_web_app.swa.default_host_name
24+
}
25+
26+
output "static_web_app_id" {
27+
value = azurerm_static_web_app.swa.id
28+
}

providers.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
terraform {
2+
required_providers {
3+
azurerm = {
4+
source = "hashicorp/azurerm"
5+
version = "4.51.0"
6+
}
7+
}
8+
}
9+
10+
provider "azurerm" {
11+
features {}
12+
use_oidc = true
13+
# Authentication via OIDC using environment variables:
14+
# ARM_CLIENT_ID, ARM_TENANT_ID, ARM_SUBSCRIPTION_ID, ARM_USE_OIDC
15+
}
16+
# Add a user to the organization
17+
#https://azure.microsoft.com/en-us/products/app-service/static

0 commit comments

Comments
 (0)