11name : 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
1112on :
1213 # The advantage of 'pull_request' over 'pull_request_target' is that we
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
2631permissions :
2732 contents : read
@@ -34,7 +39,6 @@ concurrency:
3439
3540jobs :
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
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
0 commit comments