Skip to content

Commit

Permalink
Use gh api instead of github-script
Browse files Browse the repository at this point in the history
Because it's easier to reuse the command locally
  • Loading branch information
reakaleek committed Jan 22, 2025
1 parent a8084b5 commit 81ac155
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 48 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/preview-cleanup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: preview-cleanup

on:
pull_request_target:
types: [closed]

permissions:
deployments: write

jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Delete preview deployments
run: |
deployments=$(gh api "/repos/elastic/docs-builder/deployments?environment=preview-${{ github.event.pull_request.number }}" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
| jq -r '.[] | .id')
for deployment in $deployments; do
gh api "/repos/elastic/docs-builder/deployments/$deployment/statuses" \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-f state="inactive" \
-f description="Marking deployment as inactive"
gh api "/repos/elastic/docs-builder/deployments/$deployment" -X DELETE
done
92 changes: 44 additions & 48 deletions .github/workflows/preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,30 @@ jobs:
steps:

- name: Create Deployment
uses: actions/github-script@v7
id: deployment
with:
result-encoding: string
script: |
const response = await github.rest.repos.createDeployment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
ref: "${{ github.event.pull_request.head.ref }}",
environment: `preview-${context.issue.number}`,
description: `Preview deployment for PR ${context.issue.number}`,
auto_merge: false,
required_contexts: [],
})
env:
GH_TOKEN: ${{ github.token }}
run: |
deployment_id=$(gh api "/repos/${{ github.repository }}/deployments" \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-f ref="${{ github.event.pull_request.head.ref }}" \
-f environment="preview-${{ github.event.pull_request.number }}" \
-f description="Preview deployment for PR ${{ github.event.pull_request.number }}" \
-f auto_merge=false \
-f required_contexts='[]' \
--jq '.id')
await github.rest.repos.createDeploymentStatus({
deployment_id: response.data.id,
owner: context.repo.owner,
repo: context.repo.repo,
state: "in_progress",
description: "Deployment created",
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}?pr=${context.issue.number}`,
})
gh api "/repos/${{ github.repository }}/deployments/$deployment_id/statuses" \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-f state="in_progress" \
-f description="Deployment created" \
-f log_url="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}?pr=${{ github.event.pull_request.number }}"
return response.data.id
echo "id=$deployment_id" >> $GITHUB_OUTPUT
- uses: actions/download-artifact@v4
with:
Expand Down Expand Up @@ -79,30 +77,28 @@ jobs:
aws cloudfront create-invalidation --distribution-id EKT7LT5PM8RKS --paths "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}/*"
- name: Update deployment status
uses: actions/github-script@v7
if: steps.deployment.outputs.result
with:
script: |
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: ${{ steps.deployment.outputs.result }},
state: "success",
description: "Deployment completed",
environment_url: `https://d2euvt1bxklciq.cloudfront.net/${{ github.repository }}/pull/${{ github.event.pull_request.number}}`,
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}?pr=${context.issue.number}`,
})
if: steps.deployment.outputs.id
env:
GH_TOKEN: ${{ github.token }}
run: |
gh api "/repos/${{ github.repository }}/deployments/${{ steps.deployment.outputs.id }}/statuses" \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-f state="success" \
-f description="Deployment completed" \
-f environment_url="https://d2euvt1bxklciq.cloudfront.net/${{ github.repository }}/pull/${{ github.event.pull_request.number}}" \
-f log_url="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}?pr=${{ github.event.pull_request.number }}"
- name: Update Deployment Status on Failure
if: failure() && steps.deployment.outputs.result
uses: actions/github-script@v7
with:
script: |
await github.rest.repos.createDeploymentStatus({
owner: context.repo.owner,
repo: context.repo.repo,
deployment_id: ${{ steps.deployment.outputs.result }},
state: "failure",
description: "Deployment failed",
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}?pr=${context.issue.number}`,
})
if: failure() && steps.deployment.outputs.id
env:
GH_TOKEN: ${{ github.token }}
run: |
gh api "/repos/${{ github.repository }}/deployments/${{ steps.deployment.outputs.id }}/statuses" \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-f state="failure" \
-f description="Deployment failed" \
-f log_url="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}?pr=${{ github.event.pull_request.number }}"

0 comments on commit 81ac155

Please sign in to comment.