Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions .github/workflows/documentation-deploy-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
name: documentation-deploy-preview

on:
workflow_run:
workflows: ["documentation"]
types: [completed]

concurrency:
group: gh-pages-deploy
cancel-in-progress: false

jobs:
prepare:
if: ${{ github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-24.04

name: Prepare deploy

outputs:
pr_number: ${{ steps.metadata.outputs.pr_number }}
build_environments: ${{ steps.metadata.outputs.build_environments }}

steps:
- name: Download PR metadata
uses: actions/download-artifact@v4
with:
name: pr-metadata
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}

- name: Parse metadata
id: metadata
run: |
echo "pr_number=$(jq -r '.pr_number' pr-metadata.json)" >> $GITHUB_OUTPUT
echo "build_environments=$(jq -c '.build_environments' pr-metadata.json)" >> $GITHUB_OUTPUT
shell: bash

deploy-preview:
if: ${{ needs.prepare.outputs.build_environments != '[]' }}
needs: [prepare]
runs-on: ubuntu-24.04

name: Deploy preview to GitHub Pages

permissions:
contents: write

steps:
- name: Checkout sources
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

- name: Download build artifacts
uses: actions/download-artifact@v4
with:
pattern: docusaurus-build-*
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}

- name: Deploy to gh-pages
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"

git fetch origin gh-pages:gh-pages 2>/dev/null || true

DEPLOY_DIR=$(mktemp -d)
if git show-ref --verify --quiet refs/heads/gh-pages; then
git worktree add "$DEPLOY_DIR" gh-pages
else
git worktree add --orphan -b gh-pages "$DEPLOY_DIR"
fi

touch "$DEPLOY_DIR/.nojekyll"

PR_NUMBER="${{ needs.prepare.outputs.pr_number }}"
for env in $(echo '${{ needs.prepare.outputs.build_environments }}' | jq -r '.[]'); do
rm -rf "$DEPLOY_DIR/previews/pr-${PR_NUMBER}/${env}"
mkdir -p "$DEPLOY_DIR/previews/pr-${PR_NUMBER}/${env}"
cp -r "docusaurus-build-${env}/." "$DEPLOY_DIR/previews/pr-${PR_NUMBER}/${env}/"
done

cd "$DEPLOY_DIR"
git add .
git diff --cached --quiet || git commit -m "deploy: preview for PR #${PR_NUMBER}"
git push origin gh-pages
shell: bash

comment-preview:
if: ${{ always() && needs.deploy-preview.result == 'success' }}
needs: [prepare, deploy-preview]
runs-on: ubuntu-24.04

name: Add comment for preview

permissions:
pull-requests: write

steps:
- name: Prepare preview URLs
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
script: |
const preview_urls = ${{ needs.prepare.outputs.build_environments }}.map((environment) => {
return `:rocket: Deployed preview to https://centreon.github.io/centreon-documentation/previews/pr-${{ needs.prepare.outputs.pr_number }}/${environment}/`;
});
core.exportVariable('datetime', new Date().toUTCString());
core.exportVariable('preview_urls', preview_urls.join('\n'));

- name: Leave a comment after deployment
uses: marocchino/sticky-pull-request-comment@773744901bac0e8cbb5a0dc842800d45e9b2b405 # v2.9.4
with:
header: pr-preview
number: ${{ needs.prepare.outputs.pr_number }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
message: "\
**PR Previews**

:---:

${{ env.preview_urls }}

at ${{ env.datetime }}


> **_NOTE:_** Previews are deleted after 30 days of inactivity
"
98 changes: 21 additions & 77 deletions .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ jobs:
- name: Set up Node
uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0
with:
node-version: 22
node-version: 24
cache: pnpm
cache-dependency-path: pnpm-lock.yaml

Expand Down Expand Up @@ -255,95 +255,39 @@ jobs:
shell: bash

- name: Store docusaurus build in cache
uses: actions/cache/save@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
uses: actions/cache/save@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
with:
path: |
build
key: docusaurus-build-${{ matrix.environment }}-${{ github.head_ref || github.ref_name }}

deploy-preview:
if: ${{ github.event_name == 'pull_request' }}
needs: [get-versions, build]
runs-on: infrastructure

strategy:
fail-fast: false
matrix:
environment: ${{ fromJson(needs.get-versions.outputs.build_environments) }}

name: Deploy preview ${{ matrix.environment }}

steps:
- name: Checkout sources
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

- name: Restore build from cache
uses: actions/cache/restore@cdf6c1fa76f9f475f3d7449005a359c84ca0f306 # v5.0.3
- name: Upload build artifact
if: ${{ github.event_name == 'pull_request' }}
uses: actions/upload-artifact@v4
with:
path: |
build
key: docusaurus-build-${{ matrix.environment }}-${{ github.head_ref || github.ref_name }}
fail-on-cache-miss: true

- name: Setup awscli
run: |
if ! command -v aws >/dev/null 2>&1; then
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
sudo unzip -q awscliv2.zip
sudo ./aws/install
fi
shell: bash

- name: Deploy to https://docs-preview-int.centreon.com
run: |
echo "datetime=$(date '+%Y-%m-%d %H:%M %Z')" >> $GITHUB_ENV

export $(printf "AWS_ACCESS_KEY_ID=%s AWS_SECRET_ACCESS_KEY=%s AWS_SESSION_TOKEN=%s" \
$(aws sts assume-role \
--role-arn ${{ secrets.CLOUDFRONT_ROLE_DOC_PRODUCTION }} \
--role-session-name InvalidDocCache \
--query "Credentials.[AccessKeyId,SecretAccessKey,SessionToken]" \
--output text))

aws s3 sync --delete build s3://centreon-documentation-preview-pr/previews/pr-${{ github.event.pull_request.number }}/${{ matrix.environment }}/

aws cloudfront create-invalidation --distribution-id ${{ secrets.CLOUDFRONT_ID_DOC_PREVIEW }} --paths "/previews/pr-${{ github.event.pull_request.number }}/${{ matrix.environment }}/*"
shell: bash
name: docusaurus-build-${{ matrix.environment }}
path: build
retention-days: 1

comment-preview:
needs: [get-versions, deploy-preview]
save-pr-metadata:
if: ${{ github.event_name == 'pull_request' }}
needs: [get-versions]
runs-on: ubuntu-24.04

name: Add comment for preview
name: Save PR metadata

steps:
- name: Prepare environment variables for comment
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
script: |
const preview_urls = ${{ needs.get-versions.outputs.build_environments }}.map((environment) => {
return `:rocket: Deployed preview to https://docs-preview-int.centreon.com/previews/pr-${{ github.event.pull_request.number }}/${environment}/`;
})

core.exportVariable('datetime', new Date().toUTCString());
core.exportVariable('preview_urls',preview_urls.join('\n'));
- name: Save PR metadata
run: |
echo '{"pr_number":"${{ github.event.pull_request.number }}","build_environments":${{ needs.get-versions.outputs.build_environments }}}' > pr-metadata.json
shell: bash

- name: Leave a comment after deployment
uses: marocchino/sticky-pull-request-comment@773744901bac0e8cbb5a0dc842800d45e9b2b405 # v2.9.4
- name: Upload PR metadata
uses: actions/upload-artifact@v4
with:
header: pr-preview
message: "\
**PR Previews**

:---:

${{ env.preview_urls }}

at ${{ env.datetime }}


> **_NOTE:_** Previews are deleted after 30 days of inactivity
"
name: pr-metadata
path: pr-metadata.json
retention-days: 1

deploy-staging:
if: ${{ github.ref_name == 'staging' && github.event_name != 'pull_request' && contains(fromJson(needs.get-versions.outputs.build_environments), 'staging') }}
Expand Down
Loading
Loading