diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..481b3e8 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,26 @@ +name: Deploy + +on: + push: + branches: + - main + +jobs: + deploy: + runs-on: ubuntu-latest + name: Deploy + steps: + - uses: actions/checkout@v3 + - name: Setup node + uses: actions/setup-node@v1 + with: + node-version: '20.x' + registry-url: 'https://registry.npmjs.org' + - name: Setup R2-Explorer + run: node packages/github-action/prepareDeploy.js + with: + CF_API_TOKEN: ${{ secrets.CF_API_TOKEN }} + - name: Deploy + uses: cloudflare/wrangler-action@v3 + with: + apiToken: ${{ secrets.CF_API_TOKEN }} diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index 9bfc271..51e19dc 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -35,11 +35,3 @@ node config/preparePublish.js npm run build npm publish --access public ``` - - -## Deploy demo instance - -```bash -cd packages/r2-explorer-demo/ -wrangler deploy -``` diff --git a/packages/r2-explorer-demo/package-lock.json b/packages/github-action/package-lock.json similarity index 99% rename from packages/r2-explorer-demo/package-lock.json rename to packages/github-action/package-lock.json index 8fcfdbd..eeb911c 100644 --- a/packages/r2-explorer-demo/package-lock.json +++ b/packages/github-action/package-lock.json @@ -1,11 +1,11 @@ { - "name": "r2-explorer-demo", + "name": "r2-explorer-github-action", "version": "0.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "r2-explorer-demo", + "name": "r2-explorer-github-action", "version": "0.0.1", "dependencies": { "r2-explorer": "^1.0.0" diff --git a/packages/r2-explorer-demo/package.json b/packages/github-action/package.json similarity index 83% rename from packages/r2-explorer-demo/package.json rename to packages/github-action/package.json index b812b5c..93e0914 100644 --- a/packages/r2-explorer-demo/package.json +++ b/packages/github-action/package.json @@ -1,5 +1,5 @@ { - "name": "r2-explorer-demo", + "name": "r2-explorer-github-action", "version": "0.0.1", "private": true, "devDependencies": { diff --git a/packages/github-action/prepareDeploy.js b/packages/github-action/prepareDeploy.js new file mode 100644 index 0000000..01dff44 --- /dev/null +++ b/packages/github-action/prepareDeploy.js @@ -0,0 +1,73 @@ +// @ts-ignore +const fs = require("fs"); + +const R2EXPLORER_WORKER_NAME = process.env.R2EXPLORER_WORKER_NAME; +const R2EXPLORER_BUCKETS = process.env.R2EXPLORER_BUCKETS; +const R2EXPLORER_CONFIG = process.env.R2EXPLORER_CONFIG; +const R2EXPLORER_DOMAIN = process.env.R2EXPLORER_DOMAIN; +const CF_API_TOKEN = process.env.CF_API_TOKEN; + +if (!R2EXPLORER_WORKER_NAME) { + console.error("R2EXPLORER_WORKER_NAME variable is required to continue!"); + process.exit(1); +} + +if (!R2EXPLORER_BUCKETS) { + console.error("R2EXPLORER_BUCKETS variable is required to continue!"); + process.exit(1); +} + +if (!R2EXPLORER_CONFIG) { + console.error("R2EXPLORER_CONFIG variable is required to continue!"); + process.exit(1); +} + +if (!CF_API_TOKEN) { + console.error("CF_API_TOKEN variable is required to continue!"); + process.exit(1); +} + +let wranglerConfig = ` +name = "${R2EXPLORER_WORKER_NAME}" +compatibility_date = "2023-05-12" +main = "src/index.ts" +`; + +if (R2EXPLORER_DOMAIN) { + wranglerConfig += ` +workers_dev = false +routes = [ + { pattern = "${R2EXPLORER_DOMAIN}", custom_domain = true } +] +`; +} else { + wranglerConfig += ` +workers_dev = true +`; +} + +for (const bucket of R2EXPLORER_BUCKETS.split("\n")) { + const split = bucket.split(":"); + if (split.length !== 2) { + console.error("R2EXPLORER_BUCKETS is not set correctly!"); + console.error(`"${split}" is not in the correct format`); + process.exit(1); + } + + wranglerConfig += ` +[[r2_buckets]] +binding = '${split[0]}' +bucket_name = '${split[1]}' +preview_bucket_name = '${split[1]}' +`; + +} + +fs.writeFileSync("wrangler.toml", wranglerConfig); + + +fs.writeFileSync("src/index.ts", ` +import { R2Explorer } from "r2-explorer"; + +export default R2Explorer(${R2EXPLORER_CONFIG}); +`); diff --git a/packages/r2-explorer-demo/src/index.ts b/packages/r2-explorer-demo/src/index.ts deleted file mode 100644 index 9770e39..0000000 --- a/packages/r2-explorer-demo/src/index.ts +++ /dev/null @@ -1,7 +0,0 @@ -import {R2Explorer} from 'r2-explorer'; - -export default R2Explorer({ - readonly: true, cacheAssets: true, emailRouting: { - targetBucket: 'example-1' - } -}) diff --git a/packages/r2-explorer-demo/wrangler.toml b/packages/r2-explorer-demo/wrangler.toml deleted file mode 100644 index fc4e6c3..0000000 --- a/packages/r2-explorer-demo/wrangler.toml +++ /dev/null @@ -1,16 +0,0 @@ -name = "r2-explorer-demo" -compatibility_date = "2023-05-12" -main = "src/index.ts" - -workers_dev = false -route = { pattern = "demo.r2explorer.dev/api/*", zone_name = "r2explorer.dev" } - -[[r2_buckets]] -binding = 'example-1' -bucket_name = 'r2-explorer-example-1' -preview_bucket_name = 'r2-explorer-example-1' - -[[r2_buckets]] -binding = 'example-2' -bucket_name = 'r2-explorer-example-2' -preview_bucket_name = 'r2-explorer-example-2'