-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add github action and cloudflare
- Loading branch information
Showing
8 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
name: Deploy | ||
on: | ||
push: | ||
branches: | ||
- main | ||
schedule: | ||
- cron: "0 4,16 * * *" | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
node-version: | ||
- 16.x | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
with: | ||
# 令 GitHub 在 git clone 和 git checkout 后「忘记」使用的 credentials。 | ||
# 如果之后需要以另外的身份(如你的 GitHub Bot)执行 git push 操作时(如部署到 GitHub Pages),必须设置为 false。 | ||
persist-credentials: false | ||
- name: Use Node.js ${{ matrix.node-version }} | ||
uses: actions/setup-node@v1 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
# 缓存 node_modules,缓存机制参见 GitHub 文档:https://help.github.com/en/actions/configuring-and-managing-workflows/caching-dependencies-to-speed-up-workflows | ||
- name: Cache node_modules | ||
uses: actions/cache@v1 # 使用 GitHub 官方的缓存 Action。 | ||
env: | ||
cache-name: blogroll-node-modules | ||
with: | ||
path: node_modules | ||
key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('package-lock.json') }} # 使用 package-lock.json 的 Hash 作为缓存的 key。也可以使用 package.json 代替 | ||
# Wrangler 在构建时会在 workers-site 目录下执行 npm i,因此也要缓存这里的 node_modules | ||
- name: Cache workers-site/node_modules | ||
uses: actions/cache@v1 | ||
env: | ||
cache-name: workers-site-node-modules | ||
with: | ||
path: workers-site/node_modules | ||
key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('workers-site/package-lock.json') }} | ||
- run: npm i # 执行 Blogroll 的依赖安装 | ||
- run: npm run gen # 相当于 node index.js,生成 opml.xml,opml.json 和 data.json | ||
- run: npm run build | ||
- name: Deploy to Cloudflare Workers | ||
uses: cloudflare/[email protected] | ||
with: | ||
apiToken: ${{ secrets.CF_WORKERS_TOKEN }} # 前一步设置的 Secrets 的名称 | ||
# Wrangler Action 也支持使用传统的 Global API Token + Email 的鉴权方式,但不推荐 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
worker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler' | ||
|
||
/** | ||
* The DEBUG flag will do two things that help during development: | ||
* 1. we will skip caching on the edge, which makes it easier to | ||
* debug. | ||
* 2. we will return an error message on exception in your Response rather | ||
* than the default 404.html page. | ||
*/ | ||
const DEBUG = false | ||
|
||
addEventListener('fetch', event => { | ||
try { | ||
event.respondWith(handleEvent(event)) | ||
} catch (e) { | ||
if (DEBUG) { | ||
return event.respondWith( | ||
new Response(e.message || e.toString(), { | ||
status: 500, | ||
}), | ||
) | ||
} | ||
event.respondWith(new Response('Internal Error', { status: 500 })) | ||
} | ||
}) | ||
|
||
async function handleEvent(event) { | ||
const url = new URL(event.request.url) | ||
let options = {} | ||
|
||
/** | ||
* You can add custom logic to how we fetch your assets | ||
* by configuring the function `mapRequestToAsset` | ||
*/ | ||
// options.mapRequestToAsset = handlePrefix(/^\/docs/) | ||
|
||
try { | ||
if (DEBUG) { | ||
// customize caching | ||
options.cacheControl = { | ||
bypassCache: true, | ||
}; | ||
} | ||
const page = await getAssetFromKV(event, options); | ||
|
||
// allow headers to be altered | ||
const response = new Response(page.body, page); | ||
|
||
response.headers.set("X-XSS-Protection", "1; mode=block"); | ||
response.headers.set("X-Content-Type-Options", "nosniff"); | ||
response.headers.set("X-Frame-Options", "DENY"); | ||
response.headers.set("Referrer-Policy", "unsafe-url"); | ||
response.headers.set("Feature-Policy", "none"); | ||
|
||
return response; | ||
|
||
} catch (e) { | ||
// if an error is thrown try to serve the asset at 404.html | ||
if (!DEBUG) { | ||
try { | ||
let notFoundResponse = await getAssetFromKV(event, { | ||
mapRequestToAsset: req => new Request(`${new URL(req.url).origin}/404.html`, req), | ||
}) | ||
|
||
return new Response(notFoundResponse.body, { ...notFoundResponse, status: 404 }) | ||
} catch (e) {} | ||
} | ||
|
||
return new Response(e.message || e.toString(), { status: 500 }) | ||
} | ||
} | ||
|
||
/** | ||
* Here's one example of how to modify a request to | ||
* remove a specific prefix, in this case `/docs` from | ||
* the url. This can be useful if you are deploying to a | ||
* route on a zone, or if you only want your static content | ||
* to exist at a specific path. | ||
*/ | ||
function handlePrefix(prefix) { | ||
return request => { | ||
// compute the default (e.g. / -> index.html) | ||
let defaultAssetKey = mapRequestToAsset(request) | ||
let url = new URL(defaultAssetKey.url) | ||
|
||
// strip the prefix from the path for lookup | ||
url.pathname = url.pathname.replace(prefix, '/') | ||
|
||
// inherit all other props from the default request | ||
return new Request(url.toString(), defaultAssetKey) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"private": true, | ||
"name": "worker", | ||
"version": "1.0.0", | ||
"description": "A template for kick starting a Cloudflare Workers project", | ||
"main": "index.js", | ||
"author": "Ashley Lewis <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@cloudflare/kv-asset-handler": "~0.1.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
name = "blogroll" | ||
type = "webpack" | ||
account_id = "ae4cd2700bbfe3ca1be759f084aadce7" | ||
workers_dev = true | ||
zone_id = "e758113453e9566463c10457ae9182c6" | ||
|
||
[site] | ||
bucket = "./dist" | ||
entry-point = "workers-site" |