Skip to content

Commit 8ddbbf1

Browse files
authored
feat(publishing): Auto publish repository to npm in CI [SIM-123] (#203)
1 parent f5758d2 commit 8ddbbf1

File tree

13 files changed

+2615
-134
lines changed

13 files changed

+2615
-134
lines changed

.circleci/config.yml

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
version: 2
1+
version: 2.1
2+
3+
default_env: &default_env
4+
working_directory: ~/set-protocol-v2
5+
docker:
6+
- image: circleci/node:14.18
7+
environment:
8+
NODE_OPTIONS: --max_old_space_size=8192
9+
10+
commands:
11+
setup_job:
12+
steps:
13+
- restore_cache:
14+
key: compiled-env-{{ .Environment.CIRCLE_SHA1 }}
215

316
jobs:
417
checkout_and_compile:
5-
docker:
6-
- image: circleci/node:12.16.0
7-
environment:
8-
NODE_OPTIONS: --max_old_space_size=8192
18+
<<: *default_env
919
resource_class: large
10-
working_directory: ~/set-protocol-v2
1120
steps:
1221
- checkout
1322
- restore_cache:
@@ -30,16 +39,10 @@ jobs:
3039
paths:
3140
- ~/set-protocol-v2
3241
test:
33-
docker:
34-
- image: circleci/node:12.16.0
35-
working_directory: ~/set-protocol-v2
42+
<<: *default_env
3643
parallelism: 3
3744
steps:
38-
- restore_cache:
39-
key: compiled-env-{{ .Environment.CIRCLE_SHA1 }}
40-
- run:
41-
name: Set Up Environment Variables
42-
command: cp .env.default .env
45+
- setup_job
4346
- run:
4447
name: Test RPC
4548
command: yarn chain
@@ -51,35 +54,23 @@ jobs:
5154
yarn test ${TEST_FILES}
5255
5356
test_forked_network:
54-
docker:
55-
- image: circleci/node:12.16.0
56-
working_directory: ~/set-protocol-v2
57+
<<: *default_env
5758
steps:
58-
- restore_cache:
59-
key: compiled-env-{{ .Environment.CIRCLE_SHA1 }}
60-
- run:
61-
name: Set Up Environment Variables
62-
command: cp .env.default .env
59+
- setup_job
6360
- run:
6461
name: Hardhat Test
6562
command: yarn test:fork
6663

6764
coverage:
68-
docker:
69-
- image: circleci/node:12.16.0
70-
working_directory: ~/set-protocol-v2
65+
<<: *default_env
7166
# When changing the parallelism value, you also
7267
# need to update the `persist_to_workspace` paths
7368
# in this job (below) as well as the list of files passed
7469
# to istanbul-combine in the `report_coverage` job
7570
parallelism: 5
7671
resource_class: medium+
7772
steps:
78-
- restore_cache:
79-
key: compiled-env-{{ .Environment.CIRCLE_SHA1 }}
80-
- run:
81-
name: Set Up Environment Variables
82-
command: cp .env.default .env
73+
- setup_job
8374
- run:
8475
name: Create shared coverage outputs folder
8576
command: mkdir -p /tmp/coverage
@@ -104,14 +95,11 @@ jobs:
10495
- cov_4.json
10596

10697
report_coverage:
107-
docker:
108-
- image: circleci/node:12.16.0
109-
working_directory: ~/set-protocol-v2
98+
<<: *default_env
11099
steps:
100+
- setup_job
111101
- attach_workspace:
112102
at: /tmp/coverage
113-
- restore_cache:
114-
key: compiled-env-{{ .Environment.CIRCLE_SHA1 }}
115103
- run:
116104
name: Combine coverage reports
117105
command: |
@@ -127,6 +115,22 @@ jobs:
127115
command: |
128116
cat coverage/lcov.info | node_modules/.bin/coveralls
129117
118+
release_latest:
119+
<<: *default_env
120+
steps:
121+
- setup_job
122+
- run:
123+
name: Publish "latest" release
124+
command: yarn publish:ci:latest
125+
126+
release_hardhat:
127+
<<: *default_env
128+
steps:
129+
- setup_job
130+
- run:
131+
name: Publish "hardhat" release
132+
command: yarn publish:ci:hardhat
133+
130134
workflows:
131135
version: 2
132136
build-and-test:
@@ -144,3 +148,13 @@ workflows:
144148
- report_coverage:
145149
requires:
146150
- coverage
151+
152+
# Publish `hardhat` before `latest` second so that repo's package version is set correctly on exit
153+
- release_hardhat:
154+
requires:
155+
- checkout_and_compile
156+
- release_latest:
157+
requires:
158+
- release_hardhat
159+
160+

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
coverage.json
1818
coverage/
1919
.env
20+
.releaserc.json
2021

2122
/.coverage_cache
2223
/.coverage_contracts

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,18 @@ perpSetup = getPerpV2Fixture(...);
6767
[22]: https://www.npmjs.com/package/hardhat
6868
[23]: https://www.npmjs.com/package/typechain
6969

70+
## Semantic Release
71+
72+
This repository uses [semantic-release][10] to automatically publish in CI on merge to master. To trigger
73+
a release, use the following naming convention in your PR description (or in your squash & merge commit
74+
description):
75+
76+
+ patch release (e.g 1.0.1 -> 1.0.2): `fix(topic): description`
77+
+ example: `fix(perpV2Viewer): return uint256 instead of int256`
78+
+ feature release (e.g 1.1.0 -> 1.2.0): `feat(feature_name): description`
79+
+ example: `feat(PerpV2BasisTrading): Add PerpV2 Basis Trading Module`
80+
81+
7082
## Contributing
7183
We highly encourage participation from the community to help shape the development of Set. If you are interested in developing on top of Set Protocol or have any questions, please ping us on [Discord](https://discord.gg/ZWY66aR).
7284

@@ -116,3 +128,5 @@ If you follow these guidelines when reporting an issue to us, we commit to:
116128
(including an initial confirmation of your report within 72 hours of submission).
117129

118130
* Grant a monetary reward based on the OWASP risk assessment methodology.
131+
132+
[10]: https://semantic-release.gitbook.io/semantic-release/v/beta/

contracts/lib/AddressArrayUtils.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
limitations under the License.
1515
1616
SPDX-License-Identifier: Apache License, Version 2.0
17+
1718
*/
1819

1920
pragma solidity 0.6.10;

package.json

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"version": "0.1.16",
44
"description": "",
55
"main": "dist",
6-
"types": "dist/types",
76
"files": [
87
"artifacts",
98
"dist",
@@ -15,13 +14,15 @@
1514
"tsconfig.json"
1615
],
1716
"scripts": {
18-
"build": "yarn clean && yarn compile && yarn build:typechain",
19-
"build:npm": "yarn clean && yarn compile:npm && yarn build:typechain",
20-
"build:typechain": "yarn typechain && yarn transpile-dist",
17+
"build": "yarn clean && yarn compile && yarn build:ts:latest",
18+
"build:npm:latest": "yarn clean && yarn compile:latest && yarn build:ts:latest",
19+
"build:npm:hardhat": "yarn clean && yarn compile && yarn build:ts:hardhat",
20+
"build:ts:latest": "yarn typechain && yarn transpile:dist:latest",
21+
"build:ts:hardhat": "yarn typechain && yarn transpile:dist:hardhat",
2122
"chain": "npx hardhat node",
22-
"clean": "rm -rf coverage.json .coverage_cache .coverage_contracts cache coverage typechain artifacts dist",
23+
"clean": "./scripts/clean.sh",
2324
"compile": "npx hardhat compile",
24-
"compile:npm": "SKIP_ABI_GAS_MODS=true npx hardhat compile",
25+
"compile:latest": "SKIP_ABI_GAS_MODS=true npx hardhat compile",
2526
"coverage": "yarn clean && yarn build && yarn cov:command",
2627
"cov:command": "COVERAGE=true node --max-old-space-size=4096 ./node_modules/.bin/hardhat coverage",
2728
"etherscan:verify": "hardhat --network kovan etherscan-verify --solc-input --license 'None'",
@@ -31,31 +32,44 @@
3132
"lint-ts": "eslint -c .eslintrc.js --ext .ts test utils tasks --fix",
3233
"precommit": "lint-staged",
3334
"prepare": "yarn build",
34-
"prepublishOnly": "yarn clean && yarn build:npm",
35+
"prepublishOnly": "./scripts/prepublish_only.sh",
36+
"publish:local:latest": "npm run publish",
37+
"publish:local:hardhat": "PUBLISH_HARDHAT=true npm run publish",
38+
"publish:ci:latest": "./scripts/release_latest.sh",
39+
"publish:ci:hardhat": "./scripts/release_hardhat.sh",
3540
"test": "npx hardhat test --network localhost",
3641
"test:fork": "FORK=true npx hardhat test",
3742
"test:fork:fast": "NO_COMPILE=true TS_NODE_TRANSPILE_ONLY=1 FORK=true npx hardhat test --no-compile",
3843
"test:clean": "yarn clean && yarn build && yarn test",
3944
"test:fast": "NO_COMPILE=true TS_NODE_TRANSPILE_ONLY=1 npx hardhat test --network localhost --no-compile",
4045
"test:fast:compile": "TS_NODE_TRANSPILE_ONLY=1 npx hardhat test --network localhost",
4146
"transpile": "tsc",
42-
"transpile-dist": "tsc -p tsconfig.dist.json",
43-
"typechain": "npx hardhat typechain"
47+
"transpile:dist:latest": "tsc --project tsconfig.dist.json",
48+
"transpile:dist:hardhat": "tsc --project tsconfig.hardhat.json",
49+
"typechain": "npx hardhat typechain",
50+
"semantic-release": "semantic-release"
4451
},
4552
"repository": {
4653
"type": "git",
47-
"url": "git+https://github.com/SetProtocol/set-protocol-v2-contracts.git"
54+
"url": "https://github.com/SetProtocol/set-protocol-v2.git"
4855
},
4956
"author": "felix2feng",
5057
"license": "MIT",
5158
"homepage": "https://github.com/SetProtocol",
59+
"resolutions": {
60+
"babel-code-frame/chalk": "4.1.0"
61+
},
62+
"engines": {
63+
"yarn": ">=1.10.1"
64+
},
5265
"devDependencies": {
5366
"@0x/utils": "^6.4.3",
5467
"@nomiclabs/hardhat-ethers": "^2.0.2",
5568
"@nomiclabs/hardhat-waffle": "^2.0.1",
5669
"@openzeppelin/contracts": "^3.1.0",
57-
"@typechain/ethers-v5": "^7.0.1",
58-
"@typechain/hardhat": "^2.3.0",
70+
"@semantic-release/git": "^10.0.1",
71+
"@typechain/ethers-v5": "8.0.5",
72+
"@typechain/hardhat": "3.1.0",
5973
"@types/chai": "^4.2.11",
6074
"@types/fs-extra": "^5.0.0",
6175
"@types/lodash": "^4.14.86",
@@ -78,14 +92,15 @@
7892
"istanbul-combine-updated": "^0.3.0",
7993
"lint-staged": "^10.2.11",
8094
"lodash": "^4.17.4",
95+
"semantic-release": "^19.0.2",
8196
"solc": "^0.6.10",
8297
"solhint": "^3.1.0",
8398
"solidity-coverage": "^0.7.17",
8499
"ts-generator": "^0.1.1",
85100
"ts-node": "^8.10.2",
86101
"tslint": "^6.1.3",
87102
"tslint-eslint-rules": "^5.3.1",
88-
"typechain": "5.1.2",
103+
"typechain": "6.1.0",
89104
"typescript": "^4.4.3",
90105
"web3": "^1.2.9"
91106
},

scripts/clean.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit
4+
5+
rm -f coverage.json
6+
rm -rf .coverage_cache
7+
rm -rf .coverage_contracts
8+
rm -rf cache
9+
rm -rf coverage
10+
rm -rf typechain
11+
rm -rf artifacts
12+
rm -rf dist

scripts/prepublish_only.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
3+
# Run in the `prepublishOnly npm hook`. We publish a specially built version
4+
# of the repo to the @hardhat tag which includes hardcoded gas values in the hardhat
5+
# artifact abis and deposits type definitions alongside the js files in the dist.
6+
# This build is necessary for Perp fixtures to run correctly at set-v2-strategies
7+
set -o errexit
8+
9+
echo "Running prepublishOnly npm hook"
10+
echo "PUBLISH_HARDHAT = $PUBLISH_HARDHAT"
11+
12+
# This hook is skipped when publishing in CI because semantic-release is discarding TS products of
13+
# npm lifecycle hooks. In CI we re-write the tsconfig on the fly to generate the correct outputs.
14+
if [[ -v CI ]]; then
15+
exit 0
16+
elif [[ -v PUBLISH_HARDHAT ]]; then
17+
yarn build:npm:hardhat
18+
else
19+
yarn build:npm:latest
20+
fi

scripts/release_hardhat.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit
4+
5+
# Auto-publishes a specially built release to the @hardhat tag using version schema `x.x.x-hardhat.1`
6+
# which can be installed with `yarn add @setprotocol/set-protocol-v2@hardhat`
7+
# See scripts/prepublish_only.sh for details about this build.
8+
#
9+
# The `release_default...` branch config here is necessary to trick the semantic-release tool into publishing
10+
# latest and hardhat builds from `master`. These are handled by separate jobs in CI (see circleci/config.yml)
11+
echo '{
12+
"branches": [
13+
{ "name": "release_default_do_not_delete" },
14+
{ "name": "master", "channel": "hardhat", "prerelease": "hhat"}
15+
]
16+
}' > .releaserc.json
17+
18+
# `semantic-release` discards any dist changes generated by npm lifecycle hooks so we need to copy the custom
19+
# config into the default config to produce correct build. This change *IS NOT* committed to the repo and
20+
# this script does not update the package.json version.
21+
cp tsconfig.hardhat.json tsconfig.json
22+
23+
npx semantic-release --debug

scripts/release_latest.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
3+
set -o errexit
4+
5+
# Auto-publishes `latest` from master, updates package version on github
6+
# Version format is `x.x.x` and installs with `yarn add @setprotocol/set-protocol-v2`
7+
# `package.json` version field is updated and pushed to Github
8+
echo '{
9+
"branches": [
10+
{ "name": "master" }
11+
],
12+
"plugins": [
13+
"@semantic-release/commit-analyzer",
14+
"@semantic-release/release-notes-generator",
15+
"@semantic-release/npm",
16+
"@semantic-release/git"
17+
]
18+
}' > .releaserc.json
19+
20+
# Regenerate artifacts to strip out hardcoded gas values in artifact ABIs
21+
yarn build:npm:latest
22+
23+
npx semantic-release --debug

semantic.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
titleOnly: true
2+

0 commit comments

Comments
 (0)