Skip to content

Commit ae0d9df

Browse files
authored
ci: create preview deploys with swa url (#126)
1 parent 8e1d641 commit ae0d9df

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

.github/workflows/azure-static-web-apps-delightful-cliff-0acdaf71e.yml

+78
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
permissions:
1818
id-token: write
1919
contents: read
20+
deployments: write
2021
steps:
2122
- uses: actions/checkout@v4
2223
- run: npm i -g --force corepack && corepack enable
@@ -52,6 +53,53 @@ jobs:
5253
github_id_token: ${{ steps.idtoken.outputs.result }}
5354
###### End of Repository/Build Configurations ######
5455

56+
- name: Extract deployment URL
57+
if: github.event_name == 'pull_request'
58+
id: extract_url
59+
run: |
60+
# Extract URL from builddeploy step output
61+
DEPLOYMENT_URL=$(echo '${{ steps.builddeploy.outputs.static_web_app_url }}' | grep -o 'https://[^ ]*')
62+
echo "PREVIEW_URL=$DEPLOYMENT_URL" >> $GITHUB_ENV
63+
echo "preview_url=$DEPLOYMENT_URL" >> $GITHUB_OUTPUT
64+
65+
- name: Create GitHub deployment for PR
66+
if: github.event_name == 'pull_request'
67+
uses: actions/github-script@v7
68+
with:
69+
github-token: ${{ secrets.GITHUB_TOKEN }}
70+
script: |
71+
const previewUrl = '${{ steps.extract_url.outputs.preview_url }}';
72+
if (!previewUrl) {
73+
console.log('No preview URL found, skipping deployment creation');
74+
return;
75+
}
76+
77+
console.log(`Creating deployment with preview URL: ${previewUrl}`);
78+
79+
const deployment = await github.rest.repos.createDeployment({
80+
owner: context.repo.owner,
81+
repo: context.repo.repo,
82+
ref: context.payload.pull_request.head.ref,
83+
environment: `pr-${context.payload.pull_request.number}`,
84+
auto_merge: false,
85+
required_contexts: [],
86+
description: 'Azure Static Web Apps Preview',
87+
task: 'deployment',
88+
transient_environment: true,
89+
production_environment: false
90+
});
91+
92+
await github.rest.repos.createDeploymentStatus({
93+
owner: context.repo.owner,
94+
repo: context.repo.repo,
95+
deployment_id: deployment.data.id,
96+
state: 'success',
97+
environment_url: previewUrl,
98+
log_url: previewUrl,
99+
description: 'Deployment succeeded',
100+
auto_inactive: false
101+
});
102+
55103
- name: Clear Cloudflare cache
56104
if: github.event_name == 'push'
57105
uses: Cyb3r-Jak3/[email protected]
@@ -66,6 +114,7 @@ jobs:
66114
permissions:
67115
id-token: write
68116
contents: read
117+
deployments: write
69118
steps:
70119
- uses: actions/checkout@v4
71120
- run: npm i -g --force corepack && corepack enable
@@ -89,3 +138,32 @@ jobs:
89138
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_DELIGHTFUL_CLIFF_0ACDAF71E }}
90139
github_id_token: ${{ steps.idtoken.outputs.result }}
91140
action: "close"
141+
142+
- name: Deactivate GitHub deployment for PR
143+
uses: actions/github-script@v7
144+
with:
145+
github-token: ${{ secrets.GITHUB_TOKEN }}
146+
script: |
147+
const prNumber = context.payload.pull_request.number;
148+
const environment = `pr-${prNumber}`;
149+
150+
console.log(`Deactivating deployment for environment: ${environment}`);
151+
152+
// Get all deployments for this environment
153+
const deployments = await github.rest.repos.listDeployments({
154+
owner: context.repo.owner,
155+
repo: context.repo.repo,
156+
environment: environment
157+
});
158+
159+
// Mark each deployment as inactive
160+
for (const deployment of deployments.data) {
161+
await github.rest.repos.createDeploymentStatus({
162+
owner: context.repo.owner,
163+
repo: context.repo.repo,
164+
deployment_id: deployment.id,
165+
state: 'inactive',
166+
description: 'Pull request closed',
167+
auto_inactive: false
168+
});
169+
}

0 commit comments

Comments
 (0)