11name : preview-build
22
33on :
4- pull_request :
4+ pull_request_target :
55 types :
66 - opened
77 - synchronize
@@ -41,21 +41,53 @@ jobs:
4141 steps :
4242
4343 - name : Get changed files
44- if : github.event_name == 'pull_request '
44+ if : github.event_name == 'pull_request_target '
4545 id : check-files
4646 uses : tj-actions/changed-files@d6e91a2266cdb9d62096cebf1e8546899c6aa18f # v45.0.6
4747 with :
4848 files : ${{ inputs.path-pattern != '' && inputs.path-pattern || '**' }}
4949
5050 - name : Checkout
51- if : github.event_name != 'pull_request ' || steps.check-files.outputs.any_changed == 'true'
51+ if : github.event_name == 'push ' || steps.check-files.outputs.any_changed == 'true'
5252 uses : actions/checkout@v4
5353 with :
5454 persist-credentials : false
55-
56- - name : Generate path prefix
55+
56+ - name : Create Deployment
57+ if : steps.check-files.outputs.any_changed == 'true'
58+ uses : actions/github-script@v7
59+ id : deployment
60+ env :
61+ PR_NUMBER : ${{ github.event.pull_request.number }}
62+ REF : ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.ref_name }}
63+ with :
64+ result-encoding : string
65+ script : |
66+ const { owner, repo } = context.repo;
67+ const prNumber = process.env.PR_NUMBER;
68+ const environment = prNumber ? `docs-preview-${prNumber}` : 'docs-preview';
69+ const deployment = await github.rest.repos.createDeployment({
70+ owner,
71+ repo,
72+ environment,
73+ ref: process.env.REF,
74+ auto_merge: false,
75+ required_contexts: [],
76+ })
77+ await github.rest.repos.createDeploymentStatus({
78+ deployment_id: deployment.data.id,
79+ owner,
80+ repo,
81+ state: "in_progress",
82+ log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
83+ })
84+ return deployment.data.id
85+
86+ - name : Generate env.PATH_PREFIX
87+ if : steps.check-files.outputs.any_changed == 'true'
5788 env :
5889 PR_NUMBER : ${{ github.event.pull_request.number }}
90+ GITHUB_REF_NAME : ${{ github.ref_name }}
5991 run : |
6092 case "${GITHUB_EVENT_NAME}" in
6193 "pull_request")
@@ -64,72 +96,63 @@ jobs:
6496 "push")
6597 echo "PATH_PREFIX=/${GITHUB_REPOSITORY}/tree/${GITHUB_REF_NAME}" >> $GITHUB_ENV
6698 if [[ ! "${GITHUB_REF_NAME}" =~ ^(main|master|16\.x)$ ]]; then
67- echo "Unsupported ref name: ${GITHUB_REF_NAME}";
99+ echo "Unsupported ref name: ' ${GITHUB_REF_NAME}' ";
68100 exit 1;
69101 fi
70102 ;;
71103 *)
72- echo "Unsupported event: ${GITHUB_EVENT_NAME}";
104+ echo "Unsupported event: ' ${GITHUB_EVENT_NAME}' ";
73105 exit 1;
74106 ;;
75107 esac
76-
77- - name : Store deployment metadata
78- id : metadata
79- env :
80- PR_NUMBER : ${{ github.event.pull_request.number }}
81- REF : ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || 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
85- {
86- "pr_number": "${PR_NUMBER}",
87- "ref": "${REF}",
88- "should_deploy": "${SHOULD_DEPLOY}",
89- "path_prefix": "${PATH_PREFIX}"
90- }
91- EOF
92- echo "should_deploy=${SHOULD_DEPLOY}" >> "${GITHUB_OUTPUT}"
93-
94- - name : Upload deployment metadata
95- uses : actions/upload-artifact@v4
96- with :
97- name : deployment_metadata
98- path : deployment_metadata.json
99- if-no-files-found : error
100- retention-days : 1
101- compression-level : 1
102108
103109 - name : Bootstrap Action Workspace
104- if : github.repository == 'elastic/docs-builder' && steps.metadata.outputs.should_deploy == 'true'
110+ if : github.repository == 'elastic/docs-builder' && steps.deployment.outputs.result
111+
105112 uses : ./.github/actions/bootstrap
106113
107114 - name : Set REDESIGN feature flag
108- if : contains(github.event.pull_request.labels.*.name, 'redesign')
115+ if : contains(github.event.pull_request.labels.*.name, 'redesign') && steps.deployment.outputs.result
109116 run : echo "REDESIGN=true" >> $GITHUB_ENV
110117
111118 # we run our artifact directly please use the prebuild
112119 # elastic/docs-builder@main GitHub Action for all other repositories!
113120 - name : Build documentation
114- if : github.repository == 'elastic/docs-builder' && steps.metadata .outputs.should_deploy == 'true'
121+ if : github.repository == 'elastic/docs-builder' && steps.deployment .outputs.result
115122 run : |
116123 dotnet run --project src/docs-builder -- --strict --path-prefix "${PATH_PREFIX}"
117124
118125 - name : Build documentation
119- if : github.repository != 'elastic/docs-builder' && steps.metadata .outputs.should_deploy == 'true'
126+ if : github.repository != 'elastic/docs-builder' && steps.deployment .outputs.result
120127 uses : elastic/docs-builder@main
121128 continue-on-error : ${{ fromJSON(inputs.continue-on-error != '' && inputs.continue-on-error || 'false') }}
122129 with :
123130 prefix : ${{ env.PATH_PREFIX }}
124131 strict : ${{ fromJSON(inputs.strict != '' && inputs.strict || 'true') }}
125132
126- - uses : actions/upload-artifact@v4
127- if : steps.metadata.outputs.should_deploy == 'true'
133+ - uses : elastic/docs-builder/.github/actions/aws-auth@main
134+ if : steps.deployment.outputs.result
135+
136+ - name : Upload to S3
137+ if : steps.deployment.outputs.result
138+ run : |
139+ aws s3 sync ./html "s3://elastic-docs-v3-website-preview${PATH_PREFIX}" --delete
140+ aws cloudfront create-invalidation \
141+ --distribution-id EKT7LT5PM8RKS \
142+ --paths "${PATH_PREFIX}" "${PATH_PREFIX}/*"
143+
144+ - name : Update deployment status
145+ uses : actions/github-script@v7
146+ if : always() && steps.deployment.outputs.result
147+ env :
148+ PR_NUMBER : ${{ github.event.pull_request.number }}
128149 with :
129- name : docs
130- path : .artifacts/docs/html/
131- if-no-files-found : error
132- retention-days : 1
133- # The lower the compression-level, the faster the artifact will be uploaded.
134- # But the size of the artifact will be larger.
135- compression-level : 1
150+ script : |
151+ await github.rest.repos.createDeploymentStatus({
152+ owner: context.repo.owner,
153+ repo: context.repo.repo,
154+ deployment_id: ${{ steps.deployment.outputs.result }},
155+ state: "${{ job.status == 'success' && 'success' || 'failure' }}",
156+ environment_url: `https://docs-v3-preview.elastic.dev${process.env.PATH_PREFIX}`,
157+ log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
158+ })
0 commit comments