11name : preview-build
22
33on :
4- pull_request :
4+ pull_request_target :
55 types :
66 - opened
77 - synchronize
@@ -28,108 +28,140 @@ on:
2828 default : ' **'
2929 required : false
3030
31- permissions :
31+ permissions :
32+ id-token : write
33+ deployments : write
3234 contents : read
3335 pull-requests : read
3436
3537jobs :
3638 build :
3739 concurrency :
3840 group : ${{ github.workflow }}-${{ github.ref }}
39- cancel-in-progress : true
41+ cancel-in-progress : ${{ github.event_name == 'pull_request_target' }}
4042 runs-on : ubuntu-latest
4143 steps :
4244
4345 - name : Get changed files
44- if : github.event_name == 'pull_request '
46+ if : github.event_name == 'pull_request_target '
4547 id : check-files
4648 uses : tj-actions/changed-files@d6e91a2266cdb9d62096cebf1e8546899c6aa18f # v45.0.6
4749 with :
4850 files : ${{ inputs.path-pattern != '' && inputs.path-pattern || '**' }}
4951
5052 - name : Checkout
51- if : github.event_name != 'pull_request ' || steps.check-files.outputs.any_changed == 'true'
53+ if : github.event_name == 'push ' || steps.check-files.outputs.any_changed == 'true'
5254 uses : actions/checkout@v4
5355 with :
56+ ref : ${{ github.event.pull_request.head.sha || github.ref }}
5457 persist-credentials : false
55-
56- - name : Generate path prefix
58+
59+ - name : Create Deployment
60+ if : github.event_name == 'push' || steps.check-files.outputs.any_changed == 'true'
61+ uses : actions/github-script@v7
62+ id : deployment
63+ env :
64+ PR_NUMBER : ${{ github.event.pull_request.number }}
65+ REF : ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.ref || github.ref_name }}
66+ with :
67+ result-encoding : string
68+ script : |
69+ const { owner, repo } = context.repo;
70+ const prNumber = process.env.PR_NUMBER;
71+ const environment = 'docs-preview';
72+ const deployment = await github.rest.repos.createDeployment({
73+ owner,
74+ repo,
75+ environment,
76+ ref: process.env.REF,
77+ auto_merge: false,
78+ transient_environment: true,
79+ required_contexts: [],
80+ })
81+ await github.rest.repos.createDeploymentStatus({
82+ deployment_id: deployment.data.id,
83+ owner,
84+ repo,
85+ state: "in_progress",
86+ log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
87+ })
88+ return deployment.data.id
89+
90+ - name : Generate env.PATH_PREFIX
91+ if : steps.deployment.outputs.result
5792 env :
5893 PR_NUMBER : ${{ github.event.pull_request.number }}
94+ GITHUB_REF_NAME : ${{ github.ref_name }}
5995 run : |
6096 case "${GITHUB_EVENT_NAME}" in
61- "pull_request ")
97+ "pull_request_target ")
6298 echo "PATH_PREFIX=/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" >> $GITHUB_ENV
6399 ;;
64100 "push")
65101 echo "PATH_PREFIX=/${GITHUB_REPOSITORY}/tree/${GITHUB_REF_NAME}" >> $GITHUB_ENV
66102 if [[ ! "${GITHUB_REF_NAME}" =~ ^(main|master|16\.x)$ ]]; then
67- echo "Unsupported ref name: ${GITHUB_REF_NAME}";
103+ echo "Unsupported ref name: ' ${GITHUB_REF_NAME}' ";
68104 exit 1;
69105 fi
70106 ;;
71107 *)
72- echo "Unsupported event: ${GITHUB_EVENT_NAME}";
108+ echo "Unsupported event: ' ${GITHUB_EVENT_NAME}' ";
73109 exit 1;
74110 ;;
75111 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
102112
103113 - name : Bootstrap Action Workspace
104- if : github.repository == 'elastic/docs-builder' && steps.metadata.outputs.should_deploy == 'true'
114+ if : github.repository == 'elastic/docs-builder' && steps.deployment.outputs.result
115+
105116 uses : ./.github/actions/bootstrap
106117
107118 - name : Set REDESIGN feature flag
108- if : contains(github.event.pull_request.labels.*.name, 'redesign')
119+ if : contains(github.event.pull_request.labels.*.name, 'redesign') && steps.deployment.outputs.result
109120 run : echo "REDESIGN=true" >> $GITHUB_ENV
110121
111122 # we run our artifact directly please use the prebuild
112123 # elastic/docs-builder@main GitHub Action for all other repositories!
113124 - name : Build documentation
114- if : github.repository == 'elastic/docs-builder' && steps.metadata .outputs.should_deploy == 'true'
125+ if : github.repository == 'elastic/docs-builder' && steps.deployment .outputs.result
115126 run : |
116127 dotnet run --project src/docs-builder -- --strict --path-prefix "${PATH_PREFIX}"
117128
118129 - name : Build documentation
119- if : github.repository != 'elastic/docs-builder' && steps.metadata .outputs.should_deploy == 'true'
130+ if : github.repository != 'elastic/docs-builder' && steps.deployment .outputs.result
120131 uses : elastic/docs-builder@main
121132 continue-on-error : ${{ fromJSON(inputs.continue-on-error != '' && inputs.continue-on-error || 'false') }}
122133 with :
123134 prefix : ${{ env.PATH_PREFIX }}
124135 strict : ${{ fromJSON(inputs.strict != '' && inputs.strict || 'true') }}
125136
126- - uses : actions/upload-artifact@v4
127- if : steps.metadata.outputs.should_deploy == 'true'
137+ - uses : elastic/docs-builder/.github/actions/aws-auth@main
138+ if : steps.deployment.outputs.result
139+
140+ - name : Upload to S3
141+ id : s3-upload
142+ if : steps.deployment.outputs.result
143+ run : |
144+ aws s3 sync .artifacts/docs/html "s3://elastic-docs-v3-website-preview${PATH_PREFIX}" --delete
145+ aws cloudfront create-invalidation \
146+ --distribution-id EKT7LT5PM8RKS \
147+ --paths "${PATH_PREFIX}" "${PATH_PREFIX}/*"
148+
149+ - name : Update Link Index
150+ if : steps.s3-upload.outcome == 'success'
151+ uses : elastic/docs-builder/actions/update-link-index@main
152+
153+ - name : Update deployment status
154+ uses : actions/github-script@v7
155+ if : always() && steps.deployment.outputs.result
156+ env :
157+ PR_NUMBER : ${{ github.event.pull_request.number }}
128158 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
159+ script : |
160+ await github.rest.repos.createDeploymentStatus({
161+ owner: context.repo.owner,
162+ repo: context.repo.repo,
163+ deployment_id: ${{ steps.deployment.outputs.result }},
164+ state: "${{ job.status == 'success' && 'success' || 'failure' }}",
165+ environment_url: `https://docs-v3-preview.elastic.dev${process.env.PATH_PREFIX}`,
166+ log_url: `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
167+ })
0 commit comments