Skip to content

Commit ac8e7e5

Browse files
Public Repo Azure Preview Environments (github#25206)
* add public deploy workflow, update destroy to work on pull_request_target Co-authored-by: Peter Bengtsson <[email protected]>
1 parent 67b43ec commit ac8e7e5

6 files changed

+60
-21
lines changed

.dockerignore

+2
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ tests/
1010
lib/rest/static/dereferenced
1111
# Folder is cloned during the preview + prod workflows, the assets are merged into other locations for use before the build
1212
docs-early-access/
13+
# During the preview deploy untrusted user code may be cloned into this directory
14+
user-code/

.github/workflows/azure-preview-env-deploy.yml

+47-15
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
name: Azure - Deploy Preview Environment
22

3-
# **What it does**: Build and deploy to an Azure preview environment
4-
# **Why we have it**: It's our preview environment deploy mechanism, only applicable to docs-internal
3+
# **What it does**: Build and deploy an Azure preview environment for this PR
4+
# **Why we have it**: It's our preview environment deploy mechanism, to docs-internal and docs public repo
55
# **Who does it impact**: All contributors.
66

7-
# This whole workflow is only guaranteed to be secure in the *private
8-
# repo* and because we repo-sync these files over the to the public one,
9-
# IT'S IMPORTANT THAT THIS WORKFLOW IS ONLY ENABLED IN docs-internal!
7+
# !!!
8+
# ! This worflow has access to secrets, runs in the public repository, and clones untrusted user code.
9+
# ! Modify with extreme caution
10+
# !!!
1011

1112
on:
1213
# The advantage of 'pull_request' over 'pull_request_target' is that we
@@ -15,13 +16,17 @@ on:
1516
# From a security point of view, its arguably safer this way because
1617
# unlike 'pull_request_target', these only have secrets if the pull
1718
# request creator has permission to access secrets.
18-
pull_request:
19+
pull_request_target:
1920
workflow_dispatch:
2021
inputs:
2122
PR_NUMBER:
2223
description: 'PR Number'
2324
type: string
2425
required: true
26+
COMMIT_REF:
27+
description: 'The commit SHA to build'
28+
type: string
29+
required: true
2530

2631
permissions:
2732
contents: read
@@ -34,7 +39,6 @@ concurrency:
3439

3540
jobs:
3641
build-and-deploy-azure-preview:
37-
if: ${{ github.repository == 'github/docs-internal' }}
3842
name: Build and deploy Azure preview environment
3943
runs-on: ubuntu-latest
4044
timeout-minutes: 15
@@ -47,7 +51,11 @@ jobs:
4751
url: ${{ env.APP_URL }}
4852
env:
4953
PR_NUMBER: ${{ github.event.number || github.event.inputs.PR_NUMBER }}
50-
ENABLE_EARLY_ACCESS: ${{ github.repository == 'github/docs-internal' }}
54+
COMMIT_REF: ${{ github.event.pull_request.head.sha || github.event.inputs.COMMIT_REF }}
55+
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
56+
IS_INTERNAL_BUILD: ${{ github.repository == 'github/docs-internal' }}
57+
# This may also run in forked repositories, not just 'github/docs'
58+
IS_PUBLIC_BUILD: ${{ github.repository != 'github/docs-internal' }}
5159

5260
steps:
5361
- name: 'Az CLI login'
@@ -65,10 +73,19 @@ jobs:
6573
- name: Set up Docker Buildx
6674
uses: docker/setup-buildx-action@94ab11c41e45d028884a99163086648e898eed25
6775

68-
- name: Check out repo
76+
- if: ${{ env.IS_PUBLIC_BUILD }}
77+
name: Check out main branch
78+
uses: actions/checkout@1e204e9a9253d643386038d443f96446fa156a97
79+
with:
80+
ref: 'main'
81+
persist-credentials: 'false'
82+
lfs: 'true'
83+
84+
- if: ${{ env.IS_INTERNAL_BUILD }}
85+
name: Check out PR code
6986
uses: actions/checkout@1e204e9a9253d643386038d443f96446fa156a97
7087
with:
71-
ref: ${{ github.event.pull_request.head.sha }}
88+
ref: ${{ env.COMMIT_REF }}
7289
# To prevent issues with cloning early access content later
7390
persist-credentials: 'false'
7491
lfs: 'true'
@@ -84,14 +101,14 @@ jobs:
84101
- name: 'Set env vars'
85102
run: |
86103
# Image tag is unique to each workflow run so that it always triggers a new deployment
87-
echo "DOCKER_IMAGE=${{ secrets.NONPROD_REGISTRY_SERVER }}/${IMAGE_REPO}:${{ github.event.pull_request.head.sha }}-${{ github.run_number }}-${{ github.run_attempt }}" >> $GITHUB_ENV
104+
echo "DOCKER_IMAGE=${{ secrets.NONPROD_REGISTRY_SERVER }}/${IMAGE_REPO}:${{ env.COMMIT_REF }}-${{ github.run_number }}-${{ github.run_attempt }}" >> $GITHUB_ENV
88105
89-
- if: ${{ env.ENABLE_EARLY_ACCESS }}
106+
- if: ${{ env.IS_INTERNAL_BUILD }}
90107
name: Determine which docs-early-access branch to clone
91108
id: 'check-early-access'
92109
uses: actions/github-script@2b34a689ec86a68d8ab9478298f91d5401337b7d
93110
env:
94-
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
111+
BRANCH_NAME: ${{ env.BRANCH_NAME }}
95112
with:
96113
github-token: ${{ secrets.DOCUBOT_REPO_PAT }}
97114
result-encoding: string
@@ -116,7 +133,7 @@ jobs:
116133
return 'main'
117134
}
118135
119-
- if: ${{ env.ENABLE_EARLY_ACCESS }}
136+
- if: ${{ env.IS_INTERNAL_BUILD }}
120137
name: Clone docs-early-access
121138
uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579
122139
with:
@@ -125,10 +142,25 @@ jobs:
125142
path: docs-early-access
126143
ref: ${{ steps.check-early-access.outputs.result }}
127144

128-
- if: ${{ env.ENABLE_EARLY_ACCESS }}
145+
- if: ${{ env.IS_INTERNAL_BUILD }}
129146
name: Merge docs-early-access repo's folders
130147
run: .github/actions-scripts/merge-early-access.sh
131148

149+
- if: ${{ env.IS_PUBLIC_BUILD }}
150+
name: Check out user code to temp directory
151+
uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579
152+
with:
153+
path: ./user-code
154+
ref: ${{ env.COMMIT_REF }}
155+
156+
# Move acceptable user changes into our main branch checkout
157+
- if: ${{ env.IS_PUBLIC_BUILD }}
158+
name: Move acceptable user changes
159+
run: |
160+
rsync -rptovR ./user-code/content/./**/*.md ./content
161+
rsync -rptovR ./user-code/assets/./**/*.png ./assets
162+
rsync -rptovR ./user-code/data/./**/*.{yml,md} ./data
163+
132164
# In addition to making the final image smaller, we also save time by not sending unnecessary files to the docker build context
133165
- name: 'Prune for preview env'
134166
run: .github/actions-scripts/prune-for-preview-env.sh

.github/workflows/azure-preview-env-destroy.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ name: Azure - Destroy Preview Env
55
# **Who does it impact**: All contributors.
66

77
on:
8-
pull_request:
8+
pull_request_target:
99
types:
1010
- closed
1111
- locked
@@ -16,10 +16,12 @@ on:
1616
type: string
1717
required: true
1818

19+
permissions:
20+
contents: read
21+
1922
jobs:
2023
destory-azure-preview-env:
2124
name: Destroy
22-
if: ${{ github.repository == 'github/docs-internal' }}
2325
runs-on: ubuntu-latest
2426
timeout-minutes: 5
2527
env:
@@ -59,5 +61,6 @@ jobs:
5961
# Remove all GitHub deployments from this environment and remove the environment
6062
- uses: strumwolf/delete-deployment-environment@45c821e46baa405e25410700fe2e9643929706a0
6163
with:
64+
# The token provided by the workflow does not have the permissions to delete created environments
6265
token: ${{ secrets.DOCUBOT_REPO_PAT }}
6366
environment: preview-env-${{ env.PR_NUMBER }}

.github/workflows/main-preview-docker-cache.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ jobs:
2424
timeout-minutes: 15
2525
env:
2626
ENABLE_EARLY_ACCESS: ${{ github.repository == 'github/docs-internal' }}
27-
NONPROD_REGISTRY_USERNAME: ghdocs
28-
NONPROD_REGISTRY_NAME: ghdocs
2927
DOCKER_IMAGE_CACHE_REF: ${{ secrets.NONPROD_REGISTRY_SERVER }}/${{ github.repository }}:main-preview
3028

3129
steps:
@@ -38,7 +36,7 @@ jobs:
3836
uses: azure/docker-login@81744f9799e7eaa418697cb168452a2882ae844a
3937
with:
4038
login-server: ${{ secrets.NONPROD_REGISTRY_SERVER }}
41-
username: ${{ env.NONPROD_REGISTRY_USERNAME }}
39+
username: ${{ secrets.NONPROD_REGISTRY_USERNAME }}
4240
password: ${{ secrets.NONPROD_REGISTRY_PASSWORD }}
4341

4442
- name: Set up Docker Buildx

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ blc_output.log
1717
blc_output_internal.log
1818
broken_links.md
1919
lib/redirects/.redirects-cache_*.json
20+
21+
# During the preview deploy untrusted user code may be cloned into this directory
22+
# We ignore it from git to keep things deterministic
23+
user-code/

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ FROM base as all_deps
2323

2424
COPY --chown=node:node package.json package-lock.json ./
2525

26-
RUN npm ci --no-optional
26+
RUN npm ci --no-optional --registry https://registry.npmjs.org/
2727

2828
# For Next.js v12+
2929
# This the appropriate necessary extra for node:16-alpine

0 commit comments

Comments
 (0)