17
17
permissions :
18
18
id-token : write
19
19
contents : read
20
+ deployments : write
20
21
steps :
21
22
- uses : actions/checkout@v4
22
23
- run : npm i -g --force corepack && corepack enable
52
53
github_id_token : ${{ steps.idtoken.outputs.result }}
53
54
# ##### End of Repository/Build Configurations ######
54
55
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
+
55
103
- name : Clear Cloudflare cache
56
104
if : github.event_name == 'push'
57
105
uses :
Cyb3r-Jak3/[email protected]
66
114
permissions :
67
115
id-token : write
68
116
contents : read
117
+ deployments : write
69
118
steps :
70
119
- uses : actions/checkout@v4
71
120
- run : npm i -g --force corepack && corepack enable
@@ -89,3 +138,32 @@ jobs:
89
138
azure_static_web_apps_api_token : ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_DELIGHTFUL_CLIFF_0ACDAF71E }}
90
139
github_id_token : ${{ steps.idtoken.outputs.result }}
91
140
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