Skip to content

Commit 81ac155

Browse files
committed
Use gh api instead of github-script
Because it's easier to reuse the command locally
1 parent a8084b5 commit 81ac155

File tree

2 files changed

+74
-48
lines changed

2 files changed

+74
-48
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: preview-cleanup
2+
3+
on:
4+
pull_request_target:
5+
types: [closed]
6+
7+
permissions:
8+
deployments: write
9+
10+
jobs:
11+
cleanup:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Delete preview deployments
15+
run: |
16+
deployments=$(gh api "/repos/elastic/docs-builder/deployments?environment=preview-${{ github.event.pull_request.number }}" \
17+
-H "Accept: application/vnd.github+json" \
18+
-H "X-GitHub-Api-Version: 2022-11-28" \
19+
| jq -r '.[] | .id')
20+
21+
for deployment in $deployments; do
22+
gh api "/repos/elastic/docs-builder/deployments/$deployment/statuses" \
23+
-X POST \
24+
-H "Accept: application/vnd.github+json" \
25+
-H "X-GitHub-Api-Version: 2022-11-28" \
26+
-f state="inactive" \
27+
-f description="Marking deployment as inactive"
28+
29+
gh api "/repos/elastic/docs-builder/deployments/$deployment" -X DELETE
30+
done

.github/workflows/preview.yml

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,30 @@ jobs:
1616
steps:
1717

1818
- name: Create Deployment
19-
uses: actions/github-script@v7
2019
id: deployment
21-
with:
22-
result-encoding: string
23-
script: |
24-
const response = await github.rest.repos.createDeployment({
25-
issue_number: context.issue.number,
26-
owner: context.repo.owner,
27-
repo: context.repo.repo,
28-
ref: "${{ github.event.pull_request.head.ref }}",
29-
environment: `preview-${context.issue.number}`,
30-
description: `Preview deployment for PR ${context.issue.number}`,
31-
auto_merge: false,
32-
required_contexts: [],
33-
})
20+
env:
21+
GH_TOKEN: ${{ github.token }}
22+
run: |
23+
deployment_id=$(gh api "/repos/${{ github.repository }}/deployments" \
24+
-X POST \
25+
-H "Accept: application/vnd.github+json" \
26+
-H "X-GitHub-Api-Version: 2022-11-28" \
27+
-f ref="${{ github.event.pull_request.head.ref }}" \
28+
-f environment="preview-${{ github.event.pull_request.number }}" \
29+
-f description="Preview deployment for PR ${{ github.event.pull_request.number }}" \
30+
-f auto_merge=false \
31+
-f required_contexts='[]' \
32+
--jq '.id')
3433
35-
await github.rest.repos.createDeploymentStatus({
36-
deployment_id: response.data.id,
37-
owner: context.repo.owner,
38-
repo: context.repo.repo,
39-
state: "in_progress",
40-
description: "Deployment created",
41-
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}?pr=${context.issue.number}`,
42-
})
34+
gh api "/repos/${{ github.repository }}/deployments/$deployment_id/statuses" \
35+
-X POST \
36+
-H "Accept: application/vnd.github+json" \
37+
-H "X-GitHub-Api-Version: 2022-11-28" \
38+
-f state="in_progress" \
39+
-f description="Deployment created" \
40+
-f log_url="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}?pr=${{ github.event.pull_request.number }}"
4341
44-
return response.data.id
42+
echo "id=$deployment_id" >> $GITHUB_OUTPUT
4543
4644
- uses: actions/download-artifact@v4
4745
with:
@@ -79,30 +77,28 @@ jobs:
7977
aws cloudfront create-invalidation --distribution-id EKT7LT5PM8RKS --paths "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}/*"
8078
8179
- name: Update deployment status
82-
uses: actions/github-script@v7
83-
if: steps.deployment.outputs.result
84-
with:
85-
script: |
86-
await github.rest.repos.createDeploymentStatus({
87-
owner: context.repo.owner,
88-
repo: context.repo.repo,
89-
deployment_id: ${{ steps.deployment.outputs.result }},
90-
state: "success",
91-
description: "Deployment completed",
92-
environment_url: `https://d2euvt1bxklciq.cloudfront.net/${{ github.repository }}/pull/${{ github.event.pull_request.number}}`,
93-
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}?pr=${context.issue.number}`,
94-
})
80+
if: steps.deployment.outputs.id
81+
env:
82+
GH_TOKEN: ${{ github.token }}
83+
run: |
84+
gh api "/repos/${{ github.repository }}/deployments/${{ steps.deployment.outputs.id }}/statuses" \
85+
-X POST \
86+
-H "Accept: application/vnd.github+json" \
87+
-H "X-GitHub-Api-Version: 2022-11-28" \
88+
-f state="success" \
89+
-f description="Deployment completed" \
90+
-f environment_url="https://d2euvt1bxklciq.cloudfront.net/${{ github.repository }}/pull/${{ github.event.pull_request.number}}" \
91+
-f log_url="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}?pr=${{ github.event.pull_request.number }}"
9592
9693
- name: Update Deployment Status on Failure
97-
if: failure() && steps.deployment.outputs.result
98-
uses: actions/github-script@v7
99-
with:
100-
script: |
101-
await github.rest.repos.createDeploymentStatus({
102-
owner: context.repo.owner,
103-
repo: context.repo.repo,
104-
deployment_id: ${{ steps.deployment.outputs.result }},
105-
state: "failure",
106-
description: "Deployment failed",
107-
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}?pr=${context.issue.number}`,
108-
})
94+
if: failure() && steps.deployment.outputs.id
95+
env:
96+
GH_TOKEN: ${{ github.token }}
97+
run: |
98+
gh api "/repos/${{ github.repository }}/deployments/${{ steps.deployment.outputs.id }}/statuses" \
99+
-X POST \
100+
-H "Accept: application/vnd.github+json" \
101+
-H "X-GitHub-Api-Version: 2022-11-28" \
102+
-f state="failure" \
103+
-f description="Deployment failed" \
104+
-f log_url="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}?pr=${{ github.event.pull_request.number }}"

0 commit comments

Comments
 (0)