Skip to content

Commit

Permalink
Add cdn warming scripts and action (#153)
Browse files Browse the repository at this point in the history
* Add cdn warming scripts and action

* Prettify

* Add secrets
  • Loading branch information
timqian authored Mar 8, 2022
1 parent c439e19 commit 38ca662
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/warmCDN.yml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand Down
48 changes: 48 additions & 0 deletions scripts/cdnWarmer/index.js
Original file line number Diff line number Diff line change
@@ -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;
}
);

0 comments on commit 38ca662

Please sign in to comment.