diff --git a/.github/workflows/warmCDN.yml b/.github/workflows/warmCDN.yml new file mode 100644 index 0000000..bc6cb5c --- /dev/null +++ b/.github/workflows/warmCDN.yml @@ -0,0 +1,48 @@ +name: Warm CDN + +on: + # Trigger it when the publish workflow succeeded + workflow_run: + workflows: ['Publish'] + types: + - completed + # Trigger it every day + schedule: + - cron: '0 0 * * *' + # Trigger it manually + workflow_dispatch: + +jobs: + notify: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Install Node.js and npm + uses: actions/setup-node@v1 + with: + node-version: 14.x + registry-url: https://registry.npmjs.org + + - name: Retrieve dependencies from cache + id: cacheNpm + uses: actions/cache@v2 + with: + path: | + ~/.npm + node_modules + key: npm-v14-${{ runner.os }}-refs/heads/master-${{ hashFiles('package.json') }} + restore-keys: npm-v14-${{ runner.os }}-refs/heads/master- + + - name: Install dependencies + if: steps.cacheNpm.outputs.cache-hit != 'true' + run: | + npm update --no-save + npm update --save-dev --no-save + + - name: Run the script + env: + TENCENT_SECRET_KEY: ${{ secrets.TENCENT_SECRET_KEY }} + TENCENT_SECRET_ID: ${{ secrets.TENCENT_SECRET_ID }} + run: npm run warmCDN diff --git a/package.json b/package.json index 75afdc3..b938b6d 100644 --- a/package.json +++ b/package.json @@ -15,13 +15,14 @@ "prettier-check:updated": "pipe-git-updated --ext=css --ext=html --ext=js --ext=json --ext=md --ext=yaml --ext=yml -- prettier -c", "prebuild": "rm -fr dist", "build": "webpack", - "prettify": "prettier --write src/ tests/", + "prettify": "prettier --write src/ tests/ scripts/ .github/workflows/", "test": "jest ./tests --verbose -o", "test:coverage": "jest --silent --coverage --coverageReporters=\"text\" --coverageReporters=\"text-summary\"", "testOne": "jest --verbose -o", "prepare": "husky install", "pkg:build": "node ./scripts/pkg/build.js", - "pkg:upload": "node ./scripts/pkg/upload/index.js" + "pkg:upload": "node ./scripts/pkg/upload/index.js", + "warmCDN": "node ./scripts/cdnWarmer/index.js" }, "eslintConfig": { "extends": "@serverless/eslint-config/node", @@ -96,6 +97,7 @@ "pkg": "^5.5.1", "prettier": "^2.4.1", "process-utils": "^4.0.0", + "tencentcloud-sdk-nodejs": "^4.0.301", "webpack": "^5.52.1", "webpack-cli": "^4.8.0", "webpack-node-externals": "^3.0.0" diff --git a/scripts/cdnWarmer/index.js b/scripts/cdnWarmer/index.js new file mode 100644 index 0000000..5d949fd --- /dev/null +++ b/scripts/cdnWarmer/index.js @@ -0,0 +1,48 @@ +'use strict'; +/** + * Runs after publishing new version & every day regualrly + * 1. Get latest version of binaries + * 2. Warm them up + */ +const tencentcloud = require('tencentcloud-sdk-nodejs'); +const { version } = require('../../package.json'); + +console.log(`Going to warm up CDN for version ${version}`); + +const CdnClient = tencentcloud.cdn.v20180606.Client; + +const clientConfig = { + credential: { + secretId: process.env.TENCENT_SECRET_ID, + secretKey: process.env.TENCENT_SECRET_KEY, + }, + region: '', + profile: { + httpProfile: { + endpoint: 'cdn.tencentcloudapi.com', + }, + }, +}; + +const client = new CdnClient(clientConfig); +const params = { + Urls: [ + 'https://slt-binary-sv-1300963013.file.myqcloud.com/latest-tag', + `https://slt-binary-sv-1300963013.file.myqcloud.com/${version}/serverless-tencent-macos-x64`, + `https://slt-binary-sv-1300963013.file.myqcloud.com/${version}/serverless-tencent-macos-armv6`, + `https://slt-binary-sv-1300963013.file.myqcloud.com/${version}/serverless-tencent-linux`, + `https://slt-binary-sv-1300963013.file.myqcloud.com/${version}/serverless-tencent-win-x64`, + ], +}; + +// eslint-disable-next-line new-cap +client.PushUrlsCache(params).then( + (data) => { + console.log(data); + console.log(`${version} is warmed up successfully`); + }, + (err) => { + console.error('error', err); + throw err; + } +);