Skip to content

Commit 8d0bd66

Browse files
authored
Add ability to deploy from main (#460)
* Add ability to deploy on main * Update documentation * Update how-to-set-up-docs-previews.md * Fix order
1 parent de02ce1 commit 8d0bd66

File tree

3 files changed

+81
-45
lines changed

3 files changed

+81
-45
lines changed

.github/workflows/preview-build.yml

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ on:
88
- reopened
99
- labeled
1010
- unlabeled
11+
push:
12+
branches:
13+
- main
1114
workflow_call:
1215
inputs:
1316
strict:
@@ -38,42 +41,67 @@ jobs:
3841
steps:
3942

4043
- name: Get changed files
44+
if: github.event_name == 'pull_request'
4145
id: check-files
4246
uses: tj-actions/changed-files@d6e91a2266cdb9d62096cebf1e8546899c6aa18f # v45.0.6
4347
with:
4448
files: ${{ inputs.path-pattern != '' && inputs.path-pattern || '**' }}
4549

4650
- name: Checkout
47-
if: steps.check-files.outputs.any_changed == 'true'
51+
if: github.event_name != 'pull_request' || steps.check-files.outputs.any_changed == 'true'
4852
uses: actions/checkout@v4
4953
with:
5054
persist-credentials: false
5155

52-
- name: Store PR data
56+
- name: Generate path prefix
5357
env:
5458
PR_NUMBER: ${{ github.event.pull_request.number }}
55-
PR_REF: ${{ github.event.pull_request.head.sha }}
56-
ANY_CHANGED: ${{ steps.check-files.outputs.any_changed }}
5759
run: |
58-
cat << EOF > pull_request.json
60+
case "${GITHUB_EVENT_NAME}" in
61+
"pull_request")
62+
echo "PATH_PREFIX=/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" >> $GITHUB_ENV
63+
;;
64+
"push")
65+
echo "PATH_PREFIX=/${GITHUB_REPOSITORY}/tree/${GITHUB_REF_NAME}" >> $GITHUB_ENV
66+
if [[ ! "${GITHUB_REF_NAME}" =~ ^(main|master|16\.x)$ ]]; then
67+
echo "Unsupported ref name: ${GITHUB_REF_NAME}";
68+
exit 1;
69+
fi
70+
;;
71+
*)
72+
echo "Unsupported event: ${GITHUB_EVENT_NAME}";
73+
exit 1;
74+
;;
75+
esac
76+
77+
- name: Store deployment metadata
78+
id: metadata
79+
env:
80+
PR_NUMBER: ${{ github.event.pull_request.number }}
81+
REF: ${{ github.sha }}
82+
SHOULD_DEPLOY: ${{ github.event_name == 'pull_request' && steps.check-files.outputs.any_changed || 'true' }}
83+
run: |
84+
cat << EOF > deployment_metadata.json
5985
{
60-
"number": ${PR_NUMBER},
61-
"ref": "${PR_REF}",
62-
"any_changed": ${ANY_CHANGED}
86+
"pr_number": "${PR_NUMBER}",
87+
"ref": "${REF}",
88+
"should_deploy": "${SHOULD_DEPLOY}",
89+
"path_prefix": "${PATH_PREFIX}"
6390
}
6491
EOF
92+
echo "should_deploy=${SHOULD_DEPLOY}" >> "${GITHUB_OUTPUT}"
6593
66-
- name: Upload PR data
94+
- name: Upload deployment metadata
6795
uses: actions/upload-artifact@v4
6896
with:
69-
name: pull-request-data
70-
path: pull_request.json
97+
name: deployment_metadata
98+
path: deployment_metadata.json
7199
if-no-files-found: error
72100
retention-days: 1
73101
compression-level: 1
74102

75103
- name: Bootstrap Action Workspace
76-
if: github.repository == 'elastic/docs-builder' && steps.check-files.outputs.any_changed == 'true'
104+
if: github.repository == 'elastic/docs-builder' && steps.metadata.outputs.should_deploy == 'true'
77105
uses: ./.github/actions/bootstrap
78106

79107
- name: Set REDESIGN feature flag
@@ -83,22 +111,20 @@ jobs:
83111
# we run our artifact directly please use the prebuild
84112
# elastic/docs-builder@main GitHub Action for all other repositories!
85113
- name: Build documentation
86-
if: github.repository == 'elastic/docs-builder' && steps.check-files.outputs.any_changed == 'true'
87-
env:
88-
PR_NUMBER: ${{ github.event.pull_request.number }}
114+
if: github.repository == 'elastic/docs-builder' && steps.metadata.outputs.should_deploy == 'true'
89115
run: |
90-
dotnet run --project src/docs-builder -- --strict --path-prefix "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}"
116+
dotnet run --project src/docs-builder -- --strict --path-prefix "${PATH_PREFIX}"
91117
92118
- name: Build documentation
93-
if: github.repository != 'elastic/docs-builder' && steps.check-files.outputs.any_changed == 'true'
119+
if: github.repository != 'elastic/docs-builder' && steps.metadata.outputs.should_deploy == 'true'
94120
uses: elastic/docs-builder@main
95121
continue-on-error: ${{ fromJSON(inputs.continue-on-error != '' && inputs.continue-on-error || 'false') }}
96122
with:
97-
prefix: "/${{ github.repository }}/pull/${{ github.event.pull_request.number }}"
123+
prefix: ${{ env.PATH_PREFIX }}
98124
strict: ${{ fromJSON(inputs.strict != '' && inputs.strict || 'true') }}
99125

100126
- uses: actions/upload-artifact@v4
101-
if: steps.check-files.outputs.any_changed == 'true'
127+
if: steps.metadata.outputs.should_deploy == 'true'
102128
with:
103129
name: docs
104130
path: .artifacts/docs/html/

.github/workflows/preview-deploy.yml

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,53 +14,57 @@ permissions:
1414
actions: read
1515

1616
jobs:
17-
pull-request-data:
17+
deployment-metadata:
1818
if: ${{ github.event.workflow_run.conclusion == 'success' }}
1919
runs-on: ubuntu-latest
2020
outputs:
21-
number: ${{ steps.pull_request.outputs.number }}
22-
ref: ${{ steps.pull_request.outputs.ref }}
23-
any_changed: ${{ steps.pull_request.outputs.any_changed }}
21+
pr_number: ${{ steps.metadata.outputs.pr_number }}
22+
ref: ${{ steps.metadata.outputs.ref }}
23+
should_deploy: ${{ steps.metadata.outputs.should_deploy }}
24+
path_prefix: ${{ steps.metadata.outputs.path_prefix }}
2425
steps:
25-
- name: Download PR data
26+
- name: Download deployment metadata
2627
env:
2728
GH_TOKEN: ${{ github.token }}
2829
run: |
2930
gh run download ${{ github.event.workflow_run.id }} \
3031
--repo "${GITHUB_REPOSITORY}" \
31-
--name pull-request-data
32-
- name: Get PR data
33-
id: pull_request
32+
--name deployment_metadata
33+
- name: Get deployment metadata
34+
id: metadata
3435
run: |
3536
{
36-
echo "number=$(jq -r '.number' pull_request.json)"
37-
echo "ref=$(jq -r '.ref' pull_request.json)"
38-
echo "any_changed=$(jq -r '.any_changed' pull_request.json)"
37+
echo "pr_number=$(jq -r '.pr_number' deployment_metadata.json)"
38+
echo "ref=$(jq -r '.ref' deployment_metadata.json)"
39+
echo "path_prefix=$(jq -r '.path_prefix' deployment_metadata.json)"
40+
echo "should_deploy=$(jq -r '.should_deploy' deployment_metadata.json)"
3941
} >> "${GITHUB_OUTPUT}"
4042
4143
deploy:
42-
needs: pull-request-data
43-
if: needs.pull-request-data.outputs.any_changed == 'true'
44+
needs: deployment-metadata
45+
if: needs.deployment-metadata.outputs.should_deploy == 'true'
4446
runs-on: ubuntu-latest
4547
concurrency:
46-
group: ${{ github.workflow }}-${{ needs.pull-request-data.outputs.number }}
48+
group: ${{ github.workflow }}-${{ needs.deployment-metadata.outputs.pr_number }}
4749
cancel-in-progress: true
4850
steps:
4951
- name: Create Deployment
5052
uses: actions/github-script@v7
5153
id: deployment
5254
env:
53-
PR_NUMBER: ${{ needs.pull-request-data.outputs.number }}
54-
PR_REF: ${{ needs.pull-request-data.outputs.ref }}
55+
PR_NUMBER: ${{ needs.deployment-metadata.outputs.pr_number }}
56+
REF: ${{ needs.deployment-metadata.outputs.ref }}
5557
with:
5658
result-encoding: string
5759
script: |
5860
const { owner, repo } = context.repo;
61+
const prNumber = process.env.PR_NUMBER;
62+
const environment = prNumber ? `docs-preview-${prNumber}` : 'docs-preview';
5963
const deployment = await github.rest.repos.createDeployment({
6064
owner,
6165
repo,
62-
ref: process.env.PR_REF,
63-
environment: `docs-preview-${process.env.PR_NUMBER}`,
66+
environment,
67+
ref: process.env.REF,
6468
auto_merge: false,
6569
required_contexts: [],
6670
})
@@ -86,25 +90,27 @@ jobs:
8690

8791
- name: Upload to S3
8892
env:
89-
PR_NUMBER: ${{ needs.pull-request-data.outputs.number }}
93+
PR_NUMBER: ${{ needs.deployment-metadata.outputs.pr_number }}
94+
PATH_PREFIX: ${{ needs.deployment-metadata.outputs.path_prefix }}
9095
run: |
91-
aws s3 sync ./html "s3://elastic-docs-v3-website-preview/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" --delete
96+
aws s3 sync ./html "s3://elastic-docs-v3-website-preview${PATH_PREFIX}" --delete
9297
aws cloudfront create-invalidation \
9398
--distribution-id EKT7LT5PM8RKS \
94-
--paths "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" "/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}/*"
99+
--paths "${PATH_PREFIX}" "/${PATH_PREFIX}/*"
95100
96101
- name: Update deployment status
97102
uses: actions/github-script@v7
98103
if: always() && steps.deployment.outputs.result
99104
env:
100-
PR_NUMBER: ${{ needs.pull-request-data.outputs.number }}
105+
PR_NUMBER: ${{ needs.deployment-metadata.outputs.pr_number }}
106+
PATH_PREFIX: ${{ needs.deployment-metadata.outputs.path_prefix }}
101107
with:
102108
script: |
103109
await github.rest.repos.createDeploymentStatus({
104110
owner: context.repo.owner,
105111
repo: context.repo.repo,
106112
deployment_id: ${{ steps.deployment.outputs.result }},
107113
state: "${{ job.status == 'success' && 'success' || 'failure' }}",
108-
environment_url: `https://docs-v3-preview.elastic.dev/${context.repo.owner}/${context.repo.repo}/pull/${process.env.PR_NUMBER}`,
114+
environment_url: `https://docs-v3-preview.elastic.dev${process.env.PATH_PREFIX}`,
109115
log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
110116
})

docs/migration/guide/how-to-set-up-docs-previews.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,25 @@ This way you only build and deploy the docs when there are changes to the docs a
2525
name: docs-build <1>
2626

2727
on:
28+
push: <2>
29+
branches:
30+
- main
2831
pull_request: ~
2932

3033
jobs:
3134
docs-preview:
32-
uses: elastic/docs-builder/.github/workflows/preview-build.yml <2>
35+
uses: elastic/docs-builder/.github/workflows/preview-build.yml <3>
3336
with:
34-
path-pattern: docs/** <3>
37+
path-pattern: docs/** <4>
3538
permissions:
3639
contents: read
3740
pull-requests: read
3841
```
3942
4043
1. The naming is important so that the `docs-deploy` workflow is triggered.
41-
2. This should be the path to your docs folder.
44+
2. You can omit the `push` event if you only want to build the docs on PRs.
4245
3. Reusable workflow: [elastic/docs-builder/.github/workflows/preview-build.yml](https://github.com/elastic/docs-builder/blob/main/.github/workflows/preview-build.yml)
46+
4. This should be the path to your docs folder.
4347

4448

4549
::::

0 commit comments

Comments
 (0)