Skip to content

Commit 935aa75

Browse files
authored
Make preview workflow reusable for other repositories (#350)
* Add preview action * test * fix * fix * tes * debug * test * test * fix * fix * fix * Add groups * fix * ok * fix * fix * ok * test * test * fix * refactor * naming * naming * Add workflow_call * fix * cleanup * cleanup * Remove action again * docs * test * Revert docs
1 parent ced7858 commit 935aa75

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

.github/workflows/preview-cleanup.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
name: preview-cleanup
22

33
on:
4+
workflow_call: ~
45
pull_request_target:
56
types: [closed]
67

78
permissions:
89
deployments: write
910
id-token: write
11+
contents: read
1012

1113
jobs:
12-
cleanup:
14+
destroy:
1315
runs-on: ubuntu-latest
1416
steps:
1517
- uses: actions/checkout@v4
16-
- uses: ./.github/actions/aws-auth
18+
- uses: elastic/docs-builder/.github/actions/aws-auth@main
1719
- name: Delete s3 objects
1820
env:
1921
PR_NUMBER: ${{ github.event.pull_request.number }}
@@ -28,7 +30,7 @@ jobs:
2830
const deployments = await github.rest.repos.listDeployments({
2931
owner,
3032
repo,
31-
environment: `preview-${context.issue.number}`
33+
environment: `docs-preview-${context.issue.number}`
3234
});
3335
for (const deployment of deployments.data) {
3436
await github.rest.repos.createDeploymentStatus({

.github/workflows/preview.yml

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
name: preview
22

33
on:
4-
workflow_call: ~
4+
push:
5+
branches:
6+
- main
7+
workflow_call:
8+
inputs:
9+
strict:
10+
type: boolean
11+
default: true
512

613
permissions:
714
id-token: write
8-
pull-requests: write
915
deployments: write
16+
contents: read
1017

1118
jobs:
1219
deploy:
@@ -20,12 +27,10 @@ jobs:
2027
script: |
2128
const { owner, repo } = context.repo;
2229
const deployment = await github.rest.repos.createDeployment({
23-
issue_number: context.issue.number,
2430
owner,
2531
repo,
2632
ref: context.payload.pull_request.head.ref,
27-
environment: `preview-${context.issue.number}`,
28-
description: `Preview deployment for PR ${context.issue.number}`,
33+
environment: `docs-preview-${context.issue.number}`,
2934
auto_merge: false,
3035
required_contexts: [],
3136
})
@@ -34,27 +39,38 @@ jobs:
3439
owner,
3540
repo,
3641
state: "in_progress",
37-
description: "Deployment created",
3842
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}?pr=${context.issue.number}`,
3943
})
4044
return deployment.data.id
4145
4246
- uses: actions/checkout@v4
47+
with:
48+
ref: ${{ github.event.pull_request.head.ref }}
4349

4450
- uses: actions/download-artifact@v4
51+
if: github.repository == 'elastic/docs-builder'
4552
with:
4653
name: docs-builder-binary
4754

4855
# we run our artifact directly please use the prebuild
4956
# elastic/docs-builder@main GitHub Action for all other repositories!
5057
- name: Build documentation
58+
if: github.repository == 'elastic/docs-builder'
5159
env:
52-
PR_NUMBER: ${{ github.event.pull_request.number }}
60+
PR_NUMBER:
5361
run: |
5462
chmod +x ./docs-builder
55-
./docs-builder --strict --path-prefix "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}"
63+
./docs-builder --strict --path-prefix "/${GITHUB_REPOSITORY}/pull/${{ github.event.pull_request.number }}"
64+
65+
- name: Build documentation
66+
if: github.repository != 'elastic/docs-builder'
67+
uses: elastic/docs-builder@main
68+
continue-on-error: ${{ inputs.strict != 'true' }} # Will be removed after the migration phase
69+
with:
70+
prefix: "/${{ github.repository }}/pull/${{ github.event.pull_request.number }}"
71+
strict: ${{ inputs.strict }}
5672

57-
- uses: ./.github/actions/aws-auth
73+
- uses: elastic/docs-builder/.github/actions/aws-auth@main
5874

5975
- name: Upload to S3
6076
env:
@@ -65,29 +81,14 @@ jobs:
6581
6682
- name: Update deployment status
6783
uses: actions/github-script@v7
68-
if: steps.deployment.outputs.result
84+
if: always() && steps.deployment.outputs.result
6985
with:
7086
script: |
7187
await github.rest.repos.createDeploymentStatus({
7288
owner: context.repo.owner,
7389
repo: context.repo.repo,
7490
deployment_id: ${{ steps.deployment.outputs.result }},
75-
state: "success",
76-
description: "Deployment completed",
91+
state: "${{ job.status == 'success' && 'success' || 'failure' }}",
7792
environment_url: `https://docs-v3-preview.elastic.dev/${context.repo.owner}/${context.repo.repo}/pull/${context.issue.number}`,
7893
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}?pr=${context.issue.number}`,
7994
})
80-
81-
- name: Update Deployment Status on Failure
82-
if: failure() && steps.deployment.outputs.result
83-
uses: actions/github-script@v7
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: "failure",
91-
description: "Deployment failed",
92-
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}?pr=${context.issue.number}`,
93-
})

0 commit comments

Comments
 (0)