|
| 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 | + production_url_path: |
| 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: true |
| 14 | + type: string |
| 15 | + preview_url_path: |
| 16 | + description: "Appended to the baseURL for PR preview builds" |
| 17 | + required: true |
| 18 | + type: string |
| 19 | + docs_source_path: |
| 20 | + description: "Directory of built docs files. Hugo default would be `./public/`" |
| 21 | + required: true |
| 22 | + type: string |
| 23 | + cdn_content_path: |
| 24 | + description: "The content path to be purged. Change to main docs repo currently requires a full '/*' purge, unless we add logic to determine specifically which docs were updated" |
| 25 | + required: true |
| 26 | + type: string |
| 27 | + doc_type: |
| 28 | + type: string |
| 29 | + description: "Type of source docs. Currently supports 'hugo' and 'sphinx'" |
| 30 | + default: hugo |
| 31 | + |
| 32 | +env: |
| 33 | + GO_VERISON: "1.21" # Go version used for `hugo mod get` |
| 34 | + HUGO_VERSION: "0.115.3" # Hugo version used for building docs |
| 35 | + THEME_MODULE: "github.com/nginxinc/nginx-hugo-theme" # Name of source repo for module. For example; github.com/nginxinc/nginx-hugo-theme |
| 36 | + THEME_VERSION: "0.41.8" # Version of theme module. For example; 0.41.6 |
| 37 | + |
| 38 | + PR_NUMBER: ${{github.event.pull_request.number}} |
| 39 | + |
| 40 | +jobs: |
| 41 | + build: |
| 42 | + runs-on: ubuntu-latest |
| 43 | + env: |
| 44 | + # Remapping of inputs to envs |
| 45 | + PRODUCTION_URL_PATH: ${{inputs.production_url_path}} |
| 46 | + PREVIEW_URL_PATH: ${{inputs.preview_url_path}} |
| 47 | + DOCS_SOURCE_PATH: ${{inputs.docs_source_path}} |
| 48 | + CDN_CONTENT_PATH: ${{inputs.cdn_content_path}} |
| 49 | + EVENT_ACTION: ${{github.event.action}} |
| 50 | + PR_MERGED: ${{github.event_name == 'push' && github.ref == 'refs/heads/main'}} |
| 51 | + concurrency: |
| 52 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 53 | + steps: |
| 54 | + - name: Azure login |
| 55 | + uses: azure/login@6c251865b4e6290e7b78be643ea2d005bc51f69a # v2.1.1 |
| 56 | + with: |
| 57 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 58 | + |
| 59 | + - name: Retrieve secrets from Keyvault |
| 60 | + id: keyvault |
| 61 | + uses: azure/cli@965c8d7571d2231a54e321ddd07f7b10317f34d9 # v2.0.0 |
| 62 | + with: |
| 63 | + inlineScript: | |
| 64 | + secrets_get=(productionHostname previewHostname resourceGroupName cdnProfileName cdnName accountName) |
| 65 | + for secret_get in ${secrets_get[@]} |
| 66 | + do |
| 67 | + value=$(az keyvault secret show --name $secret_get --vault-name ${{ secrets.AZURE_KEY_VAULT }} --query value --output tsv) |
| 68 | + echo "::add-mask::$value" |
| 69 | + echo "$secret_get=$value" >> $GITHUB_OUTPUT |
| 70 | + done |
| 71 | +
|
| 72 | + - name: PR preview comment |
| 73 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
| 74 | + if: github.event.action == 'opened' |
| 75 | + with: |
| 76 | + script: | |
| 77 | + const previewHostname = "${{steps.keyvault.outputs.previewHostname}}"; |
| 78 | + const previewUrlPath = process.env.PREVIEW_URL_PATH; |
| 79 | + const prNumber = context.payload.pull_request.number; |
| 80 | + const previewUrl = `https://${previewHostname}${previewUrlPath}${prNumber}/`; |
| 81 | +
|
| 82 | + const body = `### <span aria-hidden="true">✅</span> Deploy Preview will be available once build job completes! |
| 83 | +
|
| 84 | + | Name | Link | |
| 85 | + |:-:|------------------------| |
| 86 | + |<span aria-hidden="true">😎</span> Deploy Preview | [${previewUrl}](${previewUrl}) | |
| 87 | + ---`; |
| 88 | +
|
| 89 | + await github.rest.issues.createComment({ |
| 90 | + issue_number: context.issue.number, |
| 91 | + owner: context.repo.owner, |
| 92 | + repo: context.repo.repo, |
| 93 | + body: body, |
| 94 | + }); |
| 95 | +
|
| 96 | + - name: Checkout docs content |
| 97 | + uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.7.1 |
| 98 | + |
| 99 | + - name: Setup Go |
| 100 | + uses: actions/setup-go@cdcb36043654635271a94b9a6d1392de5bb323a7 # v5.0.1 |
| 101 | + with: |
| 102 | + go-version: ${{env.GO_VERISON}} |
| 103 | + # should only run for hugo |
| 104 | + |
| 105 | + # hugo builds |
| 106 | + - name: Setup Hugo |
| 107 | + uses: peaceiris/actions-hugo@75d2e84710de30f6ff7268e08f310b60ef14033f # v3.0.0 |
| 108 | + if: inputs.doc_type == 'hugo' |
| 109 | + with: |
| 110 | + hugo-version: ${{env.HUGO_VERSION}} |
| 111 | + |
| 112 | + - name: Build Hugo for PR preview |
| 113 | + if: inputs.doc_type == 'hugo' && (github.event.action == 'synchronize' || github.event.action == 'opened') |
| 114 | + run: | |
| 115 | + hugo mod get -v "$THEME_MODULE@v$THEME_VERSION" |
| 116 | + hugo --gc -e production --baseURL="https://${{steps.keyvault.outputs.previewHostname}}$PREVIEW_URL_PATH$PR_NUMBER" |
| 117 | +
|
| 118 | + - name: Build Hugo for production |
| 119 | + if: inputs.doc_type == 'hugo' && env.PR_MERGED == 'true' |
| 120 | + run: | |
| 121 | + hugo mod get "$THEME_MODULE@v$THEME_VERSION" |
| 122 | + hugo --gc -e production --baseURL="https://${{steps.keyvault.outputs.productionHostname}}$PRODUCTION_URL_PATH" |
| 123 | +
|
| 124 | + # sphinx builds |
| 125 | + |
| 126 | + - name: Setup Sphinx |
| 127 | + uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0 |
| 128 | + if: inputs.doc_type == 'sphinx' |
| 129 | + with: |
| 130 | + python-version: '3.9' |
| 131 | + |
| 132 | + - name: Install python dependencies |
| 133 | + if: inputs.doc_type == 'sphinx' |
| 134 | + run: pip install -r requirements.txt |
| 135 | + |
| 136 | + - name: Setup Gnupg directories |
| 137 | + if: inputs.doc_type == 'sphinx' |
| 138 | + run: | |
| 139 | + mkdir -p /home/runner/.gnupg |
| 140 | + chmod 700 /home/runner/.gnupg |
| 141 | +
|
| 142 | + - name: Build Sphinx for PR preview and production |
| 143 | + if: inputs.doc_type == 'sphinx' |
| 144 | + run: | |
| 145 | + make deploy |
| 146 | +
|
| 147 | + - name: Azure upload PR preview |
| 148 | + uses: azure/cli@965c8d7571d2231a54e321ddd07f7b10317f34d9 # v2.0.0 |
| 149 | + if: github.event.action == 'synchronize' || github.event.action == 'opened' |
| 150 | + with: |
| 151 | + inlineScript: | |
| 152 | + az storage blob upload-batch -s $DOCS_SOURCE_PATH -d '$web' --destination-path "dev/${{github.repository}}/previews/${PR_NUMBER}" --account-name ${{steps.keyvault.outputs.accountName}} --overwrite --auth-mode login |
| 153 | +
|
| 154 | + - name: Azure upload for production |
| 155 | + uses: azure/cli@965c8d7571d2231a54e321ddd07f7b10317f34d9 # v2.0.0 |
| 156 | + if: env.PR_MERGED == 'true' |
| 157 | + with: |
| 158 | + inlineScript: | |
| 159 | + az storage blob upload-batch -s $DOCS_SOURCE_PATH -d '$web' --destination-path "prod/${{github.repository}}/latest/" --account-name ${{steps.keyvault.outputs.accountName}} --overwrite --auth-mode login |
| 160 | +
|
| 161 | + - name: Azure purge CDN endpoint |
| 162 | + uses: azure/cli@965c8d7571d2231a54e321ddd07f7b10317f34d9 # v2.0.0 |
| 163 | + with: |
| 164 | + inlineScript: | |
| 165 | + az cdn endpoint purge --content-paths $CDN_CONTENT_PATH --profile-name ${{steps.keyvault.outputs.cdnProfileName}} --name ${{steps.keyvault.outputs.cdnName}} --resource-group ${{steps.keyvault.outputs.resourceGroupName}} |
| 166 | +
|
| 167 | + - name: Azure logout |
| 168 | + run: | |
| 169 | + az logout |
| 170 | + if: always() |
0 commit comments