|
| 1 | +name: Docs Build Push |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + secrets: |
| 6 | + AZURE_CREDENTIALS: |
| 7 | + required: true |
| 8 | + AZURE_KEY_VAULT: |
| 9 | + required: true |
| 10 | + inputs: |
| 11 | + environment: |
| 12 | + description: "This will be appended to the baseURL for for production builds. For example, main docs will be `/` where agent would be `/nginx-agent`" |
| 13 | + required: false |
| 14 | + default: preview |
| 15 | + type: string |
| 16 | + production_url_path: |
| 17 | + description: "This will be appended to the baseURL for for production builds. For example, main docs will be `/` where agent would be `/nginx-agent`" |
| 18 | + required: true |
| 19 | + type: string |
| 20 | + preview_url_path: |
| 21 | + description: "Appended to the baseURL for PR preview builds" |
| 22 | + required: true |
| 23 | + type: string |
| 24 | + docs_source_path: |
| 25 | + description: "Directory of built docs files. Hugo default would be `./public/`" |
| 26 | + required: true |
| 27 | + type: string |
| 28 | + docs_build_path: |
| 29 | + description: "Directory where hugo or sphinx build command should be run from" |
| 30 | + required: false |
| 31 | + type: string |
| 32 | + default: ./ |
| 33 | + doc_type: |
| 34 | + type: string |
| 35 | + description: "Type of source docs. Currently supports 'hugo' and 'sphinx'" |
| 36 | + default: hugo |
| 37 | + |
| 38 | +env: |
| 39 | + GO_VERISON: "1.21" # Go version used for `hugo mod get` |
| 40 | + HUGO_VERSION: "0.115.3" # Hugo version used for building docs |
| 41 | + THEME_MODULE: "github.com/nginxinc/nginx-hugo-theme" # Name of source repo for module. For example; github.com/nginxinc/nginx-hugo-theme |
| 42 | + THEME_VERSION: "0.41.10" # Version of theme module. For example; 0.41.6 |
| 43 | + |
| 44 | + PR_NUMBER: ${{github.event.pull_request.number}} |
| 45 | + |
| 46 | + # environments |
| 47 | + DOMAIN_PREVIEW: "frontdoor-test-docs.nginx.com" |
| 48 | + DOMAIN_DEV: "docs-dev.nginx.com" |
| 49 | + DOMAIN_STAGING: "docs-staging.nginx.com" |
| 50 | + DOMAIN_PROD: "docs.nginx.com" |
| 51 | + |
| 52 | +jobs: |
| 53 | + build: |
| 54 | + runs-on: ubuntu-latest |
| 55 | + env: |
| 56 | + # Remapping of inputs to envs |
| 57 | + PRODUCTION_URL_PATH: ${{inputs.production_url_path}} |
| 58 | + PREVIEW_URL_PATH: ${{inputs.preview_url_path}} |
| 59 | + DOCS_SOURCE_PATH: ${{inputs.docs_source_path}} |
| 60 | + EVENT_ACTION: ${{github.event.action}} |
| 61 | + DEPLOYMENT_ENV: ${{inputs.environment}} |
| 62 | + |
| 63 | + concurrency: |
| 64 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 65 | + steps: |
| 66 | + - name: Validate environment inputs |
| 67 | + run: | |
| 68 | + if [[ -z "${DEPLOYMENT_ENV}" ]]; then |
| 69 | + # for use in this step |
| 70 | + export DEPLOYMENT_ENV="preview" |
| 71 | +
|
| 72 | + # for use in subsequent steps |
| 73 | + echo "DEPLOYMENT_ENV=preview" >> "$GITHUB_ENV" |
| 74 | + fi |
| 75 | +
|
| 76 | + if [[ ! "${DEPLOYMENT_ENV}" =~ ^(dev|staging|prod|preview)$ ]]; then |
| 77 | + echo "::error::Invalid environment input: ${DEPLOYMENT_ENV}. Must be one of dev, staging, prod, preview" |
| 78 | + exit 1 |
| 79 | + fi |
| 80 | +
|
| 81 | + if [[ "${DEPLOYMENT_ENV}" == "preview" && -z "${PR_NUMBER}" ]]; then |
| 82 | + echo "PR_NUMBER=${GITHUB_SHA::7}" >> "$GITHUB_ENV" |
| 83 | + echo "::notice::PR_NUMBER set to short commit hash: ${GITHUB_SHA::7}" |
| 84 | + fi |
| 85 | +
|
| 86 | + if [[ -z "${GITHUB_SHA}" ]]; then |
| 87 | + echo "::error::GITHUB_SHA not set. This shouldn't happen!" |
| 88 | + exit 1 |
| 89 | + fi |
| 90 | +
|
| 91 | + - name: Set deployment domain |
| 92 | + id: deployment |
| 93 | + run: | |
| 94 | + case ${DEPLOYMENT_ENV} in |
| 95 | + dev) |
| 96 | + DOMAIN="${DOMAIN_DEV}" |
| 97 | + ;; |
| 98 | + staging) |
| 99 | + DOMAIN="${DOMAIN_STAGING}" |
| 100 | + ;; |
| 101 | + prod) |
| 102 | + DOMAIN="${DOMAIN_PROD}" |
| 103 | + ;; |
| 104 | + preview) |
| 105 | + DOMAIN="${DOMAIN_PREVIEW}" |
| 106 | + ;; |
| 107 | + esac |
| 108 | +
|
| 109 | + echo "DEPLOYMENT_DOMAIN=${DOMAIN}" >> $GITHUB_ENV |
| 110 | +
|
| 111 | + - name: Azure login |
| 112 | + uses: azure/login@6c251865b4e6290e7b78be643ea2d005bc51f69a # v2.1.1 |
| 113 | + with: |
| 114 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 115 | + |
| 116 | + - name: Retrieve secrets from Keyvault |
| 117 | + id: keyvault |
| 118 | + uses: azure/cli@965c8d7571d2231a54e321ddd07f7b10317f34d9 # v2.0.0 |
| 119 | + with: |
| 120 | + inlineScript: | |
| 121 | + secrets_get=(resourceGroupName cdnProfileName cdnName accountName) |
| 122 | + for secret_get in ${secrets_get[@]} |
| 123 | + do |
| 124 | + value=$(az keyvault secret show --name $secret_get --vault-name ${{ secrets.AZURE_KEY_VAULT }} --query value --output tsv) |
| 125 | + echo "::add-mask::$value" |
| 126 | + echo "$secret_get=$value" >> $GITHUB_OUTPUT |
| 127 | + done |
| 128 | +
|
| 129 | + - name: Checkout docs content |
| 130 | + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.7.1 |
| 131 | + |
| 132 | + ### Hugo builds |
| 133 | + |
| 134 | + - name: Setup Go |
| 135 | + uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5.0.2 |
| 136 | + if: inputs.doc_type == 'hugo' |
| 137 | + with: |
| 138 | + go-version: ${{env.GO_VERISON}} |
| 139 | + |
| 140 | + - name: Setup Hugo |
| 141 | + uses: peaceiris/actions-hugo@75d2e84710de30f6ff7268e08f310b60ef14033f # v3.0.0 |
| 142 | + if: inputs.doc_type == 'hugo' |
| 143 | + with: |
| 144 | + hugo-version: ${{env.HUGO_VERSION}} |
| 145 | + |
| 146 | + - name: Build Hugo for PR preview |
| 147 | + if: inputs.doc_type == 'hugo' && (github.event.action == 'synchronize' || github.event.action == 'opened' || env.DEPLOYMENT_ENV == 'preview') |
| 148 | + working-directory: ${{inputs.docs_build_path}} |
| 149 | + run: | |
| 150 | + hugo mod get -v "$THEME_MODULE@v$THEME_VERSION" |
| 151 | + hugo --gc -e production --baseURL="https://${DEPLOYMENT_DOMAIN}${PREVIEW_URL_PATH}/${PR_NUMBER}" |
| 152 | +
|
| 153 | + - name: Build Hugo for environment |
| 154 | + working-directory: ${{inputs.docs_build_path}} |
| 155 | + if: inputs.doc_type == 'hugo' && env.DEPLOYMENT_ENV != 'preview' |
| 156 | + run: | |
| 157 | + hugo mod get "$THEME_MODULE@v$THEME_VERSION" |
| 158 | + hugo --gc -e production --baseURL="https://${DEPLOYMENT_DOMAIN}${PRODUCTION_URL_PATH}" |
| 159 | +
|
| 160 | + ### Sphinx builds |
| 161 | + |
| 162 | + - name: Setup Sphinx |
| 163 | + uses: actions/setup-python@39cd14951b08e74b54015e9e001cdefcf80e669f # v5.1.1 |
| 164 | + if: inputs.doc_type == 'sphinx' |
| 165 | + with: |
| 166 | + python-version: "3.9" |
| 167 | + |
| 168 | + - name: Install python dependencies |
| 169 | + if: inputs.doc_type == 'sphinx' |
| 170 | + run: pip install -r requirements.txt |
| 171 | + |
| 172 | + - name: Setup Gnupg directories |
| 173 | + if: inputs.doc_type == 'sphinx' |
| 174 | + run: | |
| 175 | + mkdir -p /home/runner/.gnupg |
| 176 | + chmod 700 /home/runner/.gnupg |
| 177 | +
|
| 178 | + - name: Build Sphinx for PR preview and production |
| 179 | + working-directory: ${{inputs.docs_build_path}} |
| 180 | + if: inputs.doc_type == 'sphinx' |
| 181 | + run: | |
| 182 | + make deploy |
| 183 | +
|
| 184 | + ### Azure upload |
| 185 | + |
| 186 | + - name: Azure upload PR preview |
| 187 | + uses: azure/cli@965c8d7571d2231a54e321ddd07f7b10317f34d9 # v2.0.0 |
| 188 | + if: github.event.action == 'synchronize' || github.event.action == 'opened' || env.DEPLOYMENT_ENV == 'preview' |
| 189 | + with: |
| 190 | + inlineScript: | |
| 191 | + cd ${{inputs.docs_build_path}} \ |
| 192 | + && az storage blob upload-batch \ |
| 193 | + -s $DOCS_SOURCE_PATH \ |
| 194 | + -d '$web' \ |
| 195 | + --destination-path "dev/${{github.repository}}/previews/${PR_NUMBER}" \ |
| 196 | + --account-name ${{steps.keyvault.outputs.accountName}} \ |
| 197 | + --overwrite \ |
| 198 | + --content-cache-control "max-age=3600" \ |
| 199 | + --auth-mode login |
| 200 | +
|
| 201 | + az afd endpoint purge \ |
| 202 | + --resource-group ${{steps.keyvault.outputs.resourceGroupName}} \ |
| 203 | + --profile-name ${{steps.keyvault.outputs.cdnProfileName}} \ |
| 204 | + --endpoint-name ${{steps.keyvault.outputs.cdnName}} \ |
| 205 | + --domains $DOMAIN_PREVIEW \ |
| 206 | + --content-paths "${PREVIEW_URL_PATH}/*" \ |
| 207 | + --no-wait |
| 208 | +
|
| 209 | + - name: Azure upload to specified environment |
| 210 | + uses: azure/cli@965c8d7571d2231a54e321ddd07f7b10317f34d9 # v2.0.0 |
| 211 | + if: env.DEPLOYMENT_ENV != 'preview' |
| 212 | + with: |
| 213 | + inlineScript: | |
| 214 | + cd ${{inputs.docs_build_path}} \ |
| 215 | + && az storage blob upload-batch \ |
| 216 | + -s $DOCS_SOURCE_PATH \ |
| 217 | + -d '$web' \ |
| 218 | + --destination-path "${DEPLOYMENT_ENV}/${{github.repository}}/latest/" \ |
| 219 | + --account-name ${{steps.keyvault.outputs.accountName}} \ |
| 220 | + --overwrite \ |
| 221 | + --content-cache-control "max-age=3600" \ |
| 222 | + --auth-mode login |
| 223 | +
|
| 224 | + az afd endpoint purge \ |
| 225 | + --resource-group ${{steps.keyvault.outputs.resourceGroupName}} \ |
| 226 | + --profile-name ${{steps.keyvault.outputs.cdnProfileName}} \ |
| 227 | + --endpoint-name ${{steps.keyvault.outputs.cdnName}} \ |
| 228 | + --domains $DEPLOYMENT_DOMAIN \ |
| 229 | + --content-paths "${PRODUCTION_URL_PATH}/*" \ |
| 230 | + --no-wait |
| 231 | +
|
| 232 | + - name: Azure logout |
| 233 | + run: | |
| 234 | + az logout |
| 235 | + if: always() |
| 236 | + |
| 237 | + ### PR preview comment |
| 238 | + |
| 239 | + - name: PR preview comment |
| 240 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
| 241 | + if: env.DEPLOYMENT_ENV == 'preview' && (github.event.action == 'synchronize' || github.event.action == 'opened') |
| 242 | + with: |
| 243 | + script: | |
| 244 | + // If a comment already exists, skip |
| 245 | + const { data: comments } = await github.rest.issues.listComments({ |
| 246 | + issue_number: context.issue.number, |
| 247 | + owner: context.repo.owner, |
| 248 | + repo: context.repo.repo, |
| 249 | + }); |
| 250 | +
|
| 251 | + const existingComment = comments.find(comment => comment.user.login === 'github-actions[bot]' && comment.body.includes('Deploy Preview')); |
| 252 | +
|
| 253 | + if (existingComment) { |
| 254 | + core.info('Preview comment already exists. Skipping...'); |
| 255 | + return; |
| 256 | + } |
| 257 | +
|
| 258 | + const previewHostname = process.env.DOMAIN_PREVIEW; |
| 259 | + const previewUrlPath = process.env.PREVIEW_URL_PATH; |
| 260 | + const prNumber = context.payload.pull_request.number; |
| 261 | +
|
| 262 | + const previewUrl = `https://${previewHostname}${previewUrlPath}/${prNumber}/`; |
| 263 | +
|
| 264 | + const body = `### <span aria-hidden="true">✅</span> Deploy Preview will be available once build job completes! |
| 265 | +
|
| 266 | + | Name | Link | |
| 267 | + |:-:|------------------------| |
| 268 | + |<span aria-hidden="true">😎</span> Deploy Preview | [${previewUrl}](${previewUrl}) | |
| 269 | + ---`; |
| 270 | +
|
| 271 | + await github.rest.issues.createComment({ |
| 272 | + issue_number: context.issue.number, |
| 273 | + owner: context.repo.owner, |
| 274 | + repo: context.repo.repo, |
| 275 | + body: body, |
| 276 | + }); |
| 277 | +
|
| 278 | + - name: Summary |
| 279 | + # TODO(dani): Extract this into a reusable Markdown template for comments and summaries? |
| 280 | + run: | |
| 281 | + echo "### Deployment Summary" >> $GITHUB_STEP_SUMMARY |
| 282 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 283 | + echo "| Task | Result |" >> $GITHUB_STEP_SUMMARY |
| 284 | + echo "|---|---|" >> $GITHUB_STEP_SUMMARY |
| 285 | + echo "| Deployment environment | \`${DEPLOYMENT_ENV}\`|" >> $GITHUB_STEP_SUMMARY |
| 286 | + if [[ "${DEPLOYMENT_ENV}" == "preview" ]]; then |
| 287 | + echo "| Preview URL | [https://${DOMAIN_PREVIEW}${PREVIEW_URL_PATH}/${PR_NUMBER}/](https://${DOMAIN_PREVIEW}${PREVIEW_URL_PATH}/${PR_NUMBER}/) |" >> $GITHUB_STEP_SUMMARY |
| 288 | + else |
| 289 | + echo "| Production URL | [https://${DEPLOYMENT_DOMAIN}${PRODUCTION_URL_PATH}](https://${DEPLOYMENT_DOMAIN}${PRODUCTION_URL_PATH}) |" >> $GITHUB_STEP_SUMMARY |
| 290 | + fi |
| 291 | +
|
| 292 | + # TODO(dani): Add more details to the summary |
0 commit comments