Skip to content

Build and deploy to server #20507

Build and deploy to server

Build and deploy to server #20507

Workflow file for this run

name: Build and deploy to server
on:
push:
branches: ["master"]
paths-ignore:
- ".github/**"
- ".docs/**"
workflow_dispatch:
inputs:
hash:
description: "Docs Staging Hash"
type: string
default: main
required: true
fallback:
description: "Fallback by hash"
type: boolean
default: false
required: false
nocache:
description: "Build without cache"
type: boolean
default: false
required: false
cos_backup:
description: "To backup COS"
type: boolean
default: false
required: false
concurrency: ci-prod
jobs:
build:
runs-on: ubuntu-latest
name: Build website
permissions:
contents: read
actions: write
steps:
- name: Maximize build space
run: |
df -h
sudo rm -rf /usr/share/dotnet
sudo rm -rf /usr/local/lib/android
sudo rm -rf /opt/ghc
sudo rm -rf /opt/hostedtoolcache/CodeQL
sudo docker image prune --all --force
sudo docker builder prune -a
df -h
- uses: actions/checkout@v3
with:
ref: "master"
- uses: actions/setup-node@v4
with:
node-version: 22
cache: "yarn"
- name: Update submodule
id: submodule
run: |
git submodule init
git submodule update --remote
cd docs
git checkout main
cd ..
echo "submodule_sha=$(git submodule status | awk '{print $1}' | sed -r 's/[+-]+//g')" >> $GITHUB_OUTPUT
echo "submodule_status=$(git submodule status)" >> $GITHUB_OUTPUT
echo "git_sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
echo "build_date=$(date)" >> $GITHUB_OUTPUT
- name: Install deps
run: |
yarn
df -h
- name: Calc gatsby cache key
id: calc-cache-key
run: |
if ${{ !!inputs.fallback }}
then
echo "key=${{ inputs.hash }}" >> $GITHUB_OUTPUT
else
echo "key=${{ steps.submodule.outputs.submodule_sha }}" >> $GITHUB_OUTPUT
fi
- name: Restore gatsby cache
id: gatsby-cache
uses: actions/cache/restore@v4
with:
path: |
.cache
public
key: ${{ runner.os }}-gatsby-cache-docs-staging-${{ steps.calc-cache-key.outputs.key }}
restore-keys: |
${{ runner.os }}-gatsby-cache-docs-staging-
- name: Clean up temporary files
run: |
sudo rm -rf /tmp/*
sudo rm -rf /home/runner/work/_temp/*
- name: Clean cache
if: ${{ inputs.nocache }}
run: |
yarn clean
- name: Build website
if: ${{ !inputs.fallback || (steps.gatsby-cache.outputs.cache-hit != 'true') }}
id: build-website
env:
CI: true
GATSBY_ALGOLIA_APPLICATION_ID: ${{ secrets.ALGOLIA_APPLICATION_ID }}
GATSBY_ALGOLIA_API_KEY: ${{ secrets.ALGOLIA_API_KEY }}
GATSBY_DOC_BUILD_DATE: ${{ steps.submodule.outputs.build_date }}
GATSBY_DOC_BUILD_GIT_SHA: ${{ steps.submodule.outputs.git_sha }}
GATSBY_DOC_BUILD_SUBMODULE_SHA: ${{ steps.submodule.outputs.submodule_status }}
GATSBY_SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
run: |
yarn build
- name: Delete gatsby cache
if: ${{ steps.build-website.outcome == 'success' }}
uses: actions/github-script@v7
env:
CACHE_KEY: ${{ runner.os }}-gatsby-cache-docs-staging-${{ steps.calc-cache-key.outputs.key }}
CACHE_REF: ${{ github.ref }}
with:
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
const key = process.env.CACHE_KEY;
const ref = process.env.CACHE_REF;
core.info(`Deleting actions cache for key="${key}" ref="${ref}"`);
const caches = [];
let page = 1;
while (true) {
const resp = await github.request("GET /repos/{owner}/{repo}/actions/caches", {
owner,
repo,
key,
ref,
per_page: 100,
page,
});
const pageCaches = resp?.data?.actions_caches ?? [];
caches.push(...pageCaches);
if (pageCaches.length < 100) break;
page += 1;
}
const targets = caches.filter((cache) => cache.key === key && cache.ref === ref);
if (targets.length === 0) {
core.info("No matching caches found, skip delete.");
return;
}
for (const cache of targets) {
core.info(`Deleting cache id=${cache.id} key=${cache.key} ref=${cache.ref}`);
try {
await github.request("DELETE /repos/{owner}/{repo}/actions/caches/{cache_id}", {
owner,
repo,
cache_id: cache.id,
});
} catch (error) {
if (error.status === 404) {
core.info(`Cache id=${cache.id} already deleted.`);
continue;
}
throw error;
}
}
core.info(`Deleted ${targets.length} cache(s).`);
- name: Save gatsby cache
if: ${{ steps.build-website.outcome == 'success' }}
uses: actions/cache/save@v4
with:
path: |
.cache
public
key: ${{ runner.os }}-gatsby-cache-docs-staging-${{ steps.calc-cache-key.outputs.key }}
- name: Check output
id: check-output
run: |
sudo apt install tree
tree public
- name: Install coscli
run: |
wget https://cosbrowser.cloud.tencent.com/software/coscli/coscli-linux-amd64
mv coscli-linux-amd64 coscli
chmod 755 coscli
- name: Deploy to COS
if: ${{ !inputs.cos_backup }}
run: |
./coscli sync public/ cos://${{ secrets.TENCENTCLOUD_BUCKET_ID }} \
--init-skip \
--recursive \
--routines 16 \
--secret-id ${{ secrets.TENCENTCLOUD_SECRET_ID }} \
--secret-key ${{ secrets.TENCENTCLOUD_SECRET_KEY }} \
--endpoint cos.na-ashburn.myqcloud.com \
--delete \
--exclude ".*\.(md|txt)$" \
--force
- name: Deploy md to COS
if: ${{ !inputs.cos_backup }}
run: |
./coscli sync public/ cos://${{ secrets.TENCENTCLOUD_BUCKET_ID }} \
--init-skip \
--recursive \
--routines 16 \
--secret-id ${{ secrets.TENCENTCLOUD_SECRET_ID }} \
--secret-key ${{ secrets.TENCENTCLOUD_SECRET_KEY }} \
--endpoint cos.na-ashburn.myqcloud.com \
--include ".*\.(md|txt)$" \
--meta "Content-Type:text/plain; charset=utf-8" \
--force
- name: Deploy to COS backup
if: ${{ inputs.cos_backup }}
run: |
./coscli sync public/ cos://${{ secrets.TENCENTCLOUD_BACKUP_BUCKET_ID }} \
--init-skip \
--recursive \
--routines 16 \
--secret-id ${{ secrets.TENCENTCLOUD_SECRET_ID }} \
--secret-key ${{ secrets.TENCENTCLOUD_SECRET_KEY }} \
--endpoint cos.ap-tokyo.myqcloud.com \
--delete \
--exclude ".*\.(md|txt)$" \
--force
- name: Deploy md to COS backup
if: ${{ inputs.cos_backup }}
run: |
./coscli sync public/ cos://${{ secrets.TENCENTCLOUD_BACKUP_BUCKET_ID }} \
--init-skip \
--recursive \
--routines 16 \
--secret-id ${{ secrets.TENCENTCLOUD_SECRET_ID }} \
--secret-key ${{ secrets.TENCENTCLOUD_SECRET_KEY }} \
--endpoint cos.ap-tokyo.myqcloud.com \
--include ".*\.(md|txt)$" \
--meta "Content-Type:text/plain; charset=utf-8" \
--force
cdn-refresh:
needs: build
runs-on: ubuntu-latest
name: Refresh CDN Cache
env:
TENCENTCLOUD_SECRET_ID: ${{ secrets.TENCENTCLOUD_SECRET_ID }}
TENCENTCLOUD_SECRET_KEY: ${{ secrets.TENCENTCLOUD_SECRET_KEY }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Python environment
uses: actions/setup-python@v5
with:
python-version: "3.12"
architecture: "x64"
- name: Install Tencent Cloud CLI
run: |
pipx install tccli
- name: Purge production CDN cache
run: |
tccli teo CreatePurgeTask --cli-unfold-argument --ZoneId ${{ secrets.TENCENTCLOUD_EO_DOCS_ZONEID }} --Type purge_prefix --Method invalidate --Targets 'https://docs.pingcap.com'