Skip to content

Commit b34054b

Browse files
authored
fix: support for serverless framework v2 (#95)
* update plugin implementation work with serverless v2 by not registering an explicit deploy command (🤦‍♂️, fixes #92) * continues to work with serverless framework v1.x * update examples to use serverless framework v2 and to pull the latest version of the plugin * added end-to-end test to validate successful deploy
1 parent ef004f5 commit b34054b

File tree

9 files changed

+103
-17
lines changed

9 files changed

+103
-17
lines changed

.github/workflows/build.yml

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: build
22

33
on:
44
push:
5-
branches: [main]
5+
branches: [main, beta, next]
66
pull_request:
77
branches: [main]
88

@@ -25,7 +25,7 @@ jobs:
2525
run: |
2626
npm run lint
2727
28-
build_and_test:
28+
unit_tests:
2929
runs-on: ubuntu-20.04
3030
strategy:
3131
matrix:
@@ -37,6 +37,7 @@ jobs:
3737
uses: actions/setup-node@v2
3838
with:
3939
node-version: ${{ matrix.node }}
40+
cache: "npm"
4041

4142
- name: install dependencies
4243
run: |
@@ -55,8 +56,8 @@ jobs:
5556
flag-name: nodejs-${{ matrix.node }}
5657
parallel: true
5758

58-
finish_tests:
59-
needs: build_and_test
59+
update_code_coverage:
60+
needs: unit_tests
6061
runs-on: ubuntu-20.04
6162
steps:
6263
- name: Coveralls Finished
@@ -65,8 +66,90 @@ jobs:
6566
github-token: ${{ secrets.GITHUB_TOKEN }}
6667
parallel-finished: true
6768

69+
e2e_tests:
70+
needs: unit_tests
71+
runs-on: ubuntu-20.04
72+
steps:
73+
- uses: actions/checkout@v1
74+
75+
- name: Use Node.js v12.x
76+
uses: actions/setup-node@v1
77+
with:
78+
node-version: v12.x
79+
80+
#- name: debug deploy_package
81+
# uses: actions/bin/debug@master
82+
- name: build plugin locally
83+
run: |
84+
cd "$GITHUB_WORKSPACE"
85+
npm i
86+
npm pack
87+
88+
- name: prepare to run end-to-end deploy test
89+
env:
90+
AWS_KEY_ID: ${{ secrets.AWS_KEY_ID }}
91+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
92+
run: |
93+
cd "$GITHUB_WORKSPACE/examples/basic"
94+
npm i
95+
# now update to use the local plugin built above
96+
npm install --save file://../../serverless-aws-static-file-handler-0.0.0.tgz
97+
./node_modules/.bin/serverless config credentials --provider aws --key $AWS_KEY_ID --secret $AWS_SECRET_ACCESS_KEY --profile serverless --overwrite
98+
99+
- name: run end-to-end deploy and test endpoints
100+
run: |
101+
cd "$GITHUB_WORKSPACE/examples/basic"
102+
npm run deploy
103+
# get the APIG endpoint URL:
104+
APIG_URL=$(./node_modules/.bin/serverless info --aws-profile serverless | sed -nr 's#^.*(https://.+/dev)/png$#\1#p')
105+
106+
# CURL to some known good endpoints expecting 200:
107+
function TEST_HTTP ()
108+
{
109+
URL_PATH=$1
110+
if [[ -z "$URL_PATH" ]]; then
111+
echo "path not specified"
112+
return 1
113+
fi
114+
EXPECT_CODE=$2
115+
if [[ -z "$EXPECT_CODE" ]]; then
116+
EXPECT_CODE=200
117+
fi
118+
119+
HTTP_CODE=$(curl -s -w '%{http_code}' --compressed --output /dev/null "$APIG_URL/$URL_PATH")
120+
if [[ "$HTTP_CODE" = "$EXPECT_CODE" ]]; then
121+
echo "/$URL_PATH succeeded (returned expected code $HTTP_CODE)"
122+
else
123+
echo "FAILURE: Expected /$URL_PATH to have code $EXPECT_CODE but it was $HTTP_CODE"
124+
exit $HTTP_CODE
125+
fi
126+
}
127+
128+
# 200; these all should succeed
129+
TEST_HTTP png
130+
TEST_HTTP jpg
131+
TEST_HTTP binary/glyphicons-halflings-regular.woff2
132+
TEST_HTTP binary/subdir/png.png
133+
134+
# 403
135+
TEST_HTTP "ff404.png" 403
136+
TEST_HTTP "jpeg404.jpg" 403
137+
TEST_HTTP "subdir404/ff.png" 403
138+
TEST_HTTP "subdir/ff404.png" 403
139+
140+
# 404
141+
TEST_HTTP "binary/404-glyphicons-halflings-regular.woff2" 404
142+
TEST_HTTP "binary/subdir/404-png.png" 404
143+
144+
- name: destroy serverless stack
145+
# Run this step even if the prior one failed (to clean up)
146+
if: ${{ always() }}
147+
run: |
148+
cd "$GITHUB_WORKSPACE/examples/basic"
149+
npm run destroy
150+
68151
deploy_package:
69-
needs: finish_tests
152+
needs: e2e_tests
70153
runs-on: ubuntu-20.04
71154
steps:
72155
- uses: actions/checkout@v1

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,6 @@ examples/basic/.serverless
6565
/junk.js
6666
/todo.md
6767

68-
.serverless/
68+
.serverless/
69+
70+
.DS_Store

examples/basic/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
"reset": "rm -rfd ./node_modules/ && npm i && npm run deploy"
1212
},
1313
"devDependencies": {
14-
"serverless": "^1.46.1"
14+
"serverless": "^2.52.1"
1515
},
1616
"dependencies": {
17-
"serverless-aws-static-file-handler": "=2.0.8"
17+
"serverless-aws-static-file-handler": ">=3.0.2-beta.1"
1818
}
1919
}

examples/basic/serverless.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ custom:
1313
provider:
1414
name: aws
1515
runtime: nodejs14.x
16+
lambdaHashingVersion: 20201221
1617

1718
functions:
1819
html:

examples/serverless-offline/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
"reset": "rm -rfd ./node_modules/ && npm i && npm run deploy"
1111
},
1212
"devDependencies": {
13-
"serverless": "^1.46.1",
13+
"serverless": "^2.52.1",
1414
"serverless-offline": "^7.0.0"
1515
},
1616
"dependencies": {
17-
"serverless-aws-static-file-handler": "=2.0.8"
17+
"serverless-aws-static-file-handler": ">=3.0.2-beta.1"
1818
}
1919
}

examples/serverless-offline/serverless.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ custom:
1414
provider:
1515
name: aws
1616
runtime: nodejs14.x
17+
lambdaHashingVersion: 20201221
1718

1819
functions:
1920
html:

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
"serverless": "^2.46.0",
4242
"sinon": "^10.0.0"
4343
},
44+
"peerDependencies": {
45+
"serverless": "^2.46.0 || ^1.46.1"
46+
},
4447
"engines": {
4548
"node": ">=10"
4649
},

src/plugins/BinaryMediaTypes.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ class BinaryMediaTypes {
1010
this.serverless = serverless
1111
this.options = options
1212
this.provider = this.serverless.getProvider("aws")
13-
this.commands = {
14-
deploy: {
15-
lifecycleEvents: ["resources"],
16-
},
17-
}
1813
this.hooks = {
1914
"package:compileEvents": this.packageCompileEvents.bind(this),
2015
}
@@ -65,6 +60,7 @@ class BinaryMediaTypes {
6560
}
6661

6762
packageCompileEvents() {
63+
this.log("Preparing to add binary media types (package:compileEvents)...")
6864
const restApi = this.getRestApi()
6965
this.addBinaryMediaTypes(restApi)
7066
}

src/test/BinaryMediaTypes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ describe("BinaryMediaTypes", function () {
7878
p.serverless.service.provider.compiledCloudFormationTemplate.Resources = []
7979
const hook = getHook(p)
8080
expect(hook).to.not.throw()
81-
expect(logSpy.callCount).to.equal(1)
81+
expect(logSpy.callCount).to.equal(2)
8282
expect(
83-
logSpy.calledOnceWithExactly(
83+
logSpy.calledWithExactly(
8484
sinon.match(
8585
/Amazon API Gateway RestApi resource not found. No BinaryMediaTypes will be added.$/
8686
)

0 commit comments

Comments
 (0)