Skip to content

Commit

Permalink
fix: support replace deployment and cos assets
Browse files Browse the repository at this point in the history
  • Loading branch information
yugasun authored and Yuga Sun committed Oct 20, 2020
1 parent b2f784d commit 85e9b7c
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,7 @@ jobs:
- name: Running integration tests
run: npm run test
env:
SERVERLESS_PLATFORM_VENDOR: tencent
GLOBAL_ACCELERATOR_NA: true
TENCENT_SECRET_ID: ${{ secrets.TENCENT_SECRET_ID }}
TENCENT_SECRET_KEY: ${{ secrets.TENCENT_SECRET_KEY }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ dist
.idea
build/
.env*
!.env.example
env.js
package-lock.json
test
yarn.lock
test/src/node_modules
test/src/.next
test/src/package-lock.json
test/src/package-lock.json
14 changes: 7 additions & 7 deletions docs/configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ inputs:
acl:
permissions: public-read
sources:
- src: .next
targetDir: /_next
- src: .next/static
targetDir: /_next/static
- src: public
targetDir: /
cdnConf:
Expand Down Expand Up @@ -233,11 +233,11 @@ Refer to: https://cloud.tencent.com/document/product/628/14906

##### COS 配置

| 参数名称 | 是否必选 | 类型 | 默认值 | 描述 |
| -------- | :------: | :------: | :----------------------------------------------------------------------: | :------------------------------- |
| bucket | 是 | string | | COS 存储同名称,没有将自动创建 |
| acl | 否 | Object | | 存储桶权限配置,参考 [acl](#acl) |
| sources | 否 | Object[] | `[{src: '.next', targetDir: '/_next'}, {src: 'public', targetDir: '/'}]` | 需要托管到 COS 的静态资源目录 |
| 参数名称 | 是否必选 | 类型 | 默认值 | 描述 |
| -------- | :------: | :------: | :------------------------------------------------------------------------------------: | :------------------------------- |
| bucket | 是 | string | | COS 存储同名称,没有将自动创建 |
| acl | 否 | Object | | 存储桶权限配置,参考 [acl](#acl) |
| sources | 否 | Object[] | `[{src: '.next/static', targetDir: '/_next/static'}, {src: 'public', targetDir: '/'}]` | 需要托管到 COS 的静态资源目录 |

###### acl

Expand Down
4 changes: 4 additions & 0 deletions example/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
TENCENT_SECRET_ID=123
TENCENT_SECRET_KEY=123

STATIC_URL=https://nextjs-demo-123456789.cos.ap-guangzhou.myqcloud.com
2 changes: 1 addition & 1 deletion example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

# misc
.DS_Store
.env*
.env

# debug
npm-debug.log*
Expand Down
2 changes: 1 addition & 1 deletion example/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Home = () => (
<div className="container">
<Head>
<title>Create Next App</title>
<link rel="icon" href="/favicon.ico" />
<link rel="icon" href={`${process.env.STATIC_URL}/favicon.ico`} />
</Head>

<main>
Expand Down
6 changes: 5 additions & 1 deletion example/serverless.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
org: orgDemo
app: appDemo
stage: dev
component: nextjs
component: nextjs@dev
name: nextjsDemo

inputs:
Expand All @@ -17,3 +17,7 @@ inputs:
- http
- https
environment: release
staticConf:
cosConf:
replace: true
bucket: nextjs-demo
24 changes: 23 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"publishConfig": {
"access": "public"
},
"description": "Tencent Cloud Next.js Serverless Component",
"scripts": {
"test": "jest ./tests/integration.test.js --testEnvironment node",
"commitlint": "commitlint -f HEAD@{15}",
Expand Down Expand Up @@ -57,5 +58,26 @@
"lint-staged": "^10.0.8",
"prettier": "^1.19.1",
"semantic-release": "^17.0.4"
}
},
"directories": {
"doc": "docs",
"example": "example",
"test": "tests"
},
"repository": {
"type": "git",
"url": "git+https://github.com/serverless-components/tencent-nextjs.git"
},
"keywords": [
"serverless-nextjs",
"nextjs",
"serverless",
"serverless-framework",
"serverless-components",
"tencent-cloud"
],
"bugs": {
"url": "https://github.com/serverless-components/tencent-nextjs/issues"
},
"homepage": "https://github.com/serverless-components/tencent-nextjs#readme"
}
2 changes: 1 addition & 1 deletion serverless.component.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: nextjs
version: 0.1.7
version: 0.1.8
author: 'Tencent Cloud, Inc.'
org: 'Tencent Cloud, Inc.'
description: Deploy a serverless Next.js application onto Tencent SCF and API Gateway.
Expand Down
2 changes: 1 addition & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const CONFIGS = {
namespace: 'default',
description: 'Created by Serverless Component',
defaultStatics: [
{ src: '.next', targetDir: '/_next' },
{ src: '.next/static', targetDir: '/_next/static' },
{ src: 'public', targetDir: '/' }
],
defaultCdnConf: {
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"dependencies": {
"adm-zip": "^0.4.16",
"download": "^8.0.0",
"tencent-component-toolkit": "^1.17.0",
"tencent-component-toolkit": "^1.17.4",
"type": "^2.1.0"
}
}
6 changes: 5 additions & 1 deletion src/serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,16 @@ class ServerlessComopnent extends Component {
if (zipPath) {
console.log(`Deploy static for ${CONFIGS.compFullname} application`)
// 1. deploy to cos
const staticCosInputs = await prepareStaticCosInputs(this, inputs, appId, zipPath)
const { staticCosInputs, bucket } = await prepareStaticCosInputs(this, inputs, appId, zipPath)

const cos = new Cos(credentials, region)
const cosOutput = {
region
}
// flush bucket
if (inputs.cosConf.replace) {
await cos.flushBucketFiles(bucket)
}
for (let i = 0; i < staticCosInputs.length; i++) {
const curInputs = staticCosInputs[i]
console.log(`Starting deploy directory ${curInputs.src} to cos bucket ${curInputs.bucket}`)
Expand Down
5 changes: 4 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ const prepareStaticCosInputs = async (instance, inputs, appId, codeZipPath) => {
staticCosInputs.push(cosInputs)
}
}
return staticCosInputs
return {
bucket: `${bucketName}-${appId}`,
staticCosInputs
}
} catch (e) {
throw new TypeError(
`UTILS_${CONFIGS.compName.toUpperCase()}_prepareStaticCosInputs`,
Expand Down
5 changes: 4 additions & 1 deletion tests/integration.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
require('dotenv').config()
const path = require('path')
require('dotenv').config({
path: path.join(__dirname, '..', '.env.test')
})
const { generateId, getServerlessSdk } = require('./utils')
const axios = require('axios')

Expand Down

0 comments on commit 85e9b7c

Please sign in to comment.