diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 6c0f8f4..e5c07e3 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -33,6 +33,7 @@ jobs: - run: npm run integration-ci env: HLX_FASTLY_AUTH: ${{ secrets.HLX_FASTLY_AUTH }} + CLOUDFLARE_AUTH: ${{ secrets.CLOUDFLARE_AUTH }} - name: Semantic Release (Dry Run) run: npm run semantic-release-dry diff --git a/src/CloudflareDeployer.js b/src/CloudflareDeployer.js index bd4fb92..94e3aca 100644 --- a/src/CloudflareDeployer.js +++ b/src/CloudflareDeployer.js @@ -89,6 +89,8 @@ export default class CloudflareDeployer extends BaseDeployer { await this.updatePackageParams(id, this.cfg.packageParams); await this.restoreSettings(settings); + + await this.enableSubdomain(); } async getSettings() { @@ -120,6 +122,22 @@ export default class CloudflareDeployer extends BaseDeployer { return res.ok; } + async enableSubdomain() { + const res = await this.fetch(`https://api.cloudflare.com/client/v4/accounts/${this._cfg.accountID}/workers/scripts/${this.fullFunctionName}/subdomain`, { + method: 'POST', + headers: { + Authorization: `Bearer ${this._cfg.auth}`, + 'content-type': 'application/json', + }, + body: JSON.stringify({ enabled: true }), + }); + if (!res.ok) { + const { errors } = await res.json(); + this.log.warn(`Unable to enable workers.dev subdomain: ${errors[0]?.message || 'unknown error'}`); + } + return res.ok; + } + async updatePackageParams(id, params) { const kvlist = Object.entries(params).map(([key, value]) => ({ key, value, @@ -169,7 +187,7 @@ export default class CloudflareDeployer extends BaseDeployer { ? this.testRequest({ url: `https://${this.fullFunctionName}.${this._cfg.testDomain}.workers.dev`, idHeader: 'CF-RAY', - retry404: 0, + retry404: 5, }) : undefined; } diff --git a/test/cloudflare.integration.js b/test/cloudflare.integration.js index 6eabcc3..f016c81 100644 --- a/test/cloudflare.integration.js +++ b/test/cloudflare.integration.js @@ -35,7 +35,7 @@ describe('Cloudflare Integration Test', () => { await fse.remove(testRoot); }); - it.skip('Deploy a pure action to Cloudflare', async () => { + it('Deploy a pure action to Cloudflare', async () => { await fse.copy(path.resolve(__rootdir, 'test', 'fixtures', 'edge-action'), testRoot); process.chdir(testRoot); // need to change .cwd() for yargs to pickup `wsk` in package.json const builder = await new CLI() @@ -47,8 +47,9 @@ describe('Cloudflare Integration Test', () => { '--plugin', path.resolve(__rootdir, 'src', 'index.js'), '--arch', 'edge', '--cloudflare-email', 'lars@trieloff.net', - '--cloudflare-account-id', 'b4adf6cfdac0918eb6aa5ad033da0747', - '--cloudflare-test-domain', 'rockerduck', + '--cloudflare-account-id', '155ec15a52a18a14801e04b019da5e5a', + '--cloudflare-test-domain', 'minivelos', + '--cloudflare-auth', process.env.CLOUDFLARE_AUTH, '--package.params', 'HEY=ho', '--package.params', 'ZIP=zap', '--update-package', 'true', @@ -64,6 +65,6 @@ describe('Cloudflare Integration Test', () => { const res = await builder.run(); assert.ok(res); const out = builder.cfg._logger.output; - assert.ok(out.indexOf('https://simple-package--simple-project.rockerduck.workers.dev') > 0, out); + assert.ok(out.indexOf('https://simple-package--simple-project.minivelos.workers.dev') > 0, out); }).timeout(10000000); }); diff --git a/test/deploy.test.js b/test/deploy.test.js index 0d257bb..c24a360 100644 --- a/test/deploy.test.js +++ b/test/deploy.test.js @@ -56,7 +56,9 @@ describe('Deploy Test', () => { }) .reply(200, JSON.stringify({ result: { id: 'test-namespace' } })) .put('/client/v4/accounts/123/workers/scripts/default--test-worker') - .reply(200); + .reply(200) + .post('/client/v4/accounts/123/workers/scripts/default--test-worker/subdomain') + .reply(200, JSON.stringify({ result: { enabled: true, previews_enabled: true } })); process.chdir(testRoot); // need to change .cwd() for yargs to pickup `wsk` in package.json const builder = await new CLI() @@ -120,7 +122,9 @@ describe('Deploy Test', () => { bodies.settings = b; return true; }) - .reply(200); + .reply(200) + .post('/client/v4/accounts/123/workers/scripts/default--test-worker/subdomain') + .reply(200, JSON.stringify({ result: { enabled: true, previews_enabled: true } })); process.chdir(testRoot); // need to change .cwd() for yargs to pickup `wsk` in package.json const builder = await new CLI()