Skip to content

Commit

Permalink
Added new SDK stack (#300)
Browse files Browse the repository at this point in the history
Added new SDK stack deployment:
- new stack definition that is run with "sst:deploy:sdk:staging" or
"sst:deploy:sdk:prod"
- new github actions that is run only manually from the selected branch
- sdk will not deploy automatically from now on, only manually
  • Loading branch information
piotrwitek authored and robercano committed May 27, 2024
1 parent 954b884 commit 591a9a5
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 13 deletions.
75 changes: 75 additions & 0 deletions .github/workflows/deploy-sdk-production.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: 'Deploy SDK Production'

on:
workflow_dispatch:

permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout
jobs:
deploy:
name: Build & Deploy SDK to production using SST
environment: production
runs-on: ubuntu-latest
env:
ONE_INCH_API_KEY: ${{ secrets.ONE_INCH_API_KEY }}
ONE_INCH_API_VERSION: ${{ secrets.ONE_INCH_API_VERSION }}
ONE_INCH_API_URL: ${{ secrets.ONE_INCH_API_URL }}
ONE_INCH_ALLOWED_SWAP_PROTOCOLS: ${{ secrets.ONE_INCH_ALLOWED_SWAP_PROTOCOLS }}
ONE_INCH_SWAP_CHAIN_IDS: ${{ secrets.ONE_INCH_SWAP_CHAIN_IDS }}
ONE_INCH_API_SPOT_URL: ${{ secrets.ONE_INCH_API_SPOT_URL }}
ONE_INCH_API_SPOT_VERSION: ${{ secrets.ONE_INCH_API_SPOT_VERSION }}
ONE_INCH_API_SPOT_KEY: ${{ secrets.ONE_INCH_API_SPOT_KEY }}
MAINNET_RPC_URL: ${{ secrets.MAINNET_RPC_URL }}
SUBGRAPH_BASE: ${{ secrets.SUBGRAPH_BASE }}
RPC_GATEWAY: ${{ secrets.RPC_GATEWAY }}
DEBANK_API_KEY: ${{ secrets.DEBANK_API_KEY }}
DEBANK_API_URL: ${{ secrets.PROD_DEBANK_API_URL }}
POWERTOOLS_LOG_LEVEL: ${{ vars.POWERTOOLS_LOG_LEVEL }}
REDIS_CACHE_URL: ${{ secrets.REDIS_CACHE_URL }}
REDIS_CACHE_USER: ${{ secrets.REDIS_CACHE_USER }}
REDIS_CACHE_PASSWORD: ${{ secrets.REDIS_CACHE_PASSWORD }}
VPC_ID: ${{ secrets.VPC_ID }}
SECURITY_GROUP_ID: ${{ secrets.SECURITY_GROUP_ID }}
RAYS_DB_WRITE_CONNECTION_STRING: ${{ secrets.RAYS_DB_WRITE_CONNECTION_STRING }}
RAYS_DB_READ_CONNECTION_STRING: ${{ secrets.RAYS_DB_READ_CONNECTION_STRING }}
steps:
- name: Git clone the repository
uses: actions/checkout@v3

- name: Cache turbo build setup
uses: actions/cache@v3
with:
path: .turbo
key: ${{ runner.os }}-turbo-${{ github.sha }}
restore-keys: |
${{ runner.os }}-turbo-
- uses: pnpm/action-setup@v2.0.1
with:
version: 8.14.1

- name: Setup Node.js environment
uses: actions/setup-node@v3
with:
node-version: 20
cache: 'pnpm'

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE }}
aws-region: ${{ secrets.AWS_REGION }}

- name: Install dependencies
run: pnpm install

- name: Prebuild
run: pnpm prebuild

- name: Build
run: pnpm build

- name: Deploy app
run: |
pnpm run sst:deploy:sdk:prod
76 changes: 76 additions & 0 deletions .github/workflows/deploy-sdk-staging.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: 'Deploy SDK Staging'

on:
workflow_dispatch:

permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout
jobs:
deploy:
name: Build & Deploy SDK to staging using SST
environment: staging
runs-on: ubuntu-latest
env:
ONE_INCH_API_KEY: ${{ secrets.ONE_INCH_API_KEY }}
ONE_INCH_API_VERSION: ${{ secrets.ONE_INCH_API_VERSION }}
ONE_INCH_API_URL: ${{ secrets.ONE_INCH_API_URL }}
ONE_INCH_ALLOWED_SWAP_PROTOCOLS: ${{ secrets.ONE_INCH_ALLOWED_SWAP_PROTOCOLS }}
ONE_INCH_SWAP_CHAIN_IDS: ${{ secrets.ONE_INCH_SWAP_CHAIN_IDS }}
ONE_INCH_API_SPOT_URL: ${{ secrets.ONE_INCH_API_SPOT_URL }}
ONE_INCH_API_SPOT_VERSION: ${{ secrets.ONE_INCH_API_SPOT_VERSION }}
ONE_INCH_API_SPOT_KEY: ${{ secrets.ONE_INCH_API_SPOT_KEY }}
MAINNET_RPC_URL: ${{ secrets.MAINNET_RPC_URL }}
SUBGRAPH_BASE: ${{ secrets.SUBGRAPH_BASE }}
RPC_GATEWAY: ${{ secrets.RPC_GATEWAY }}
DEBANK_API_KEY: ${{ secrets.DEBANK_API_KEY }}
DEBANK_API_URL: ${{ secrets.STAGING_DEBANK_API_URL }}
POWERTOOLS_LOG_LEVEL: ${{ vars.POWERTOOLS_LOG_LEVEL }}
REDIS_CACHE_URL: ${{ secrets.REDIS_CACHE_URL }}
REDIS_CACHE_USER: ${{ secrets.REDIS_CACHE_USER }}
REDIS_CACHE_PASSWORD: ${{ secrets.REDIS_CACHE_PASSWORD }}
VPC_ID: ${{ secrets.VPC_ID }}
SECURITY_GROUP_ID: ${{ secrets.SECURITY_GROUP_ID }}
RAYS_DB_WRITE_CONNECTION_STRING: ${{ secrets.RAYS_DB_WRITE_CONNECTION_STRING }}
RAYS_DB_READ_CONNECTION_STRING: ${{ secrets.RAYS_DB_READ_CONNECTION_STRING }}
steps:
- name: Git clone the repository
uses: actions/checkout@v3

- name: Cache turbo build setup
uses: actions/cache@v3
with:
path: .turbo
key: ${{ runner.os }}-turbo-${{ github.sha }}
restore-keys: |
${{ runner.os }}-turbo-
- uses: pnpm/action-setup@v2.0.1
with:
version: 8.14.1

- name: Setup Node.js environment
uses: actions/setup-node@v3
with:
node-version: 20
cache: 'pnpm'

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.AWS_ROLE }}
# role-duration-seconds: 14390 #adjust as needed for your build time
aws-region: ${{ secrets.AWS_REGION }}

- name: Install dependencies
run: pnpm install

- name: Prebuild
run: pnpm prebuild

- name: Build
run: pnpm build

- name: Deploy app
run: |
pnpm run sst:deploy:sdk:staging
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
"sst:dev": "sst dev",
"sst:build": "sst build",
"sst:deploy:dev": "sst deploy",
"sst:deploy:staging": "sst deploy --stage staging",
"sst:deploy:prod": "sst deploy --stage production",
"sst:deploy:staging": "sst deploy --stage staging --app summerfi-stack",
"sst:deploy:prod": "sst deploy --stage production --app summerfi-stack",
"sst:dev:sdk": "sst dev --app sdk",
"sst:deploy:sdk:staging": "sst deploy --stage staging --app sdk",
"sst:deploy:sdk:prod": "sst deploy --stage production --app sdk",
"remove": "sst remove",
"console": "sst console",
"graph": "pnpm dlx nx graph",
Expand Down
31 changes: 23 additions & 8 deletions sst.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { SSTConfig } from 'sst'
import { API } from './stacks/summer-stack'
import { ExternalAPI } from './stacks/partners-stack'
import { SdkAPI } from './stacks/sdk-stack'
import { $, chalk, echo } from 'zx'

const availableStage = ['dev', 'feature', 'staging', 'production']

enum App {
SummerfiStack = 'summerfi-stack',
Sdk = 'sdk',
}
const availableApps: string[] = [App.SummerfiStack, App.Sdk]

const getCurrentBranch = async () => {
const { stdout: currentBranch } = await $`git branch --show-current`
return currentBranch.trim()
Expand Down Expand Up @@ -81,6 +88,9 @@ const runDockerCompose = async () => {

export const sstConfig: SSTConfig = {
async config(_input) {
// read app from cli input
const { app } = _input as { app?: string }

const currentBranch = await getCurrentBranch()
const commitsToFetch = await getCommitsToFetch(currentBranch)
if (commitsToFetch === null) {
Expand Down Expand Up @@ -133,11 +143,7 @@ export const sstConfig: SSTConfig = {
throw new Error('Please specify stage or set SST_USER env variable')
}

if (
_input.stage &&
!availableStage.includes(_input.stage) &&
!_input.stage.startsWith('dev-')
) {
if (_input.stage && !availableStage.includes(_input.stage)) {
throw new Error('Invalid stage, use one of: ' + availableStage.join(', '))
}

Expand All @@ -160,8 +166,12 @@ export const sstConfig: SSTConfig = {
}
}

if (!app || availableApps.includes(app) === false) {
throw new Error('Invalid --app argument, use one of: ' + availableApps.join(', '))
}

return {
name: `summerfi-stack`,
name: app,
region: `${process.env.AWS_REGION}`,
profile: `${process.env.AWS_PROFILE}`,
stage: stage,
Expand All @@ -174,8 +184,13 @@ export const sstConfig: SSTConfig = {
app.setDefaultRemovalPolicy('retain')
}
echo`\n`
app.stack(API)
app.stack(ExternalAPI)
if (app.name === App.SummerfiStack) {
app.stack(API)
app.stack(ExternalAPI)
}
if (app.name === App.Sdk) {
app.stack(SdkAPI)
}
},
}

Expand Down
18 changes: 18 additions & 0 deletions stacks/sdk-stack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Api, StackContext } from 'sst/constructs'
import { addSdkConfig } from './sdk'

export function SdkAPI(stackContext: StackContext) {
const { stack } = stackContext
const api = new Api(stack, 'sdk', {
defaults: {
function: {},
},
routes: {},
})

addSdkConfig(stackContext, api)

stack.addOutputs({
RouterEndpoint: api.url,
})
}
2 changes: 1 addition & 1 deletion stacks/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const {
RPC_GATEWAY,
} = process.env

export function addSdkConfig({ stack, api }: StackContext & { api: Api }) {
export function addSdkConfig({ stack }: StackContext, api: Api) {
if (
!ONE_INCH_API_KEY ||
!ONE_INCH_API_VERSION ||
Expand Down
2 changes: 0 additions & 2 deletions stacks/summer-stack.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Api, StackContext } from 'sst/constructs'
import { addTriggersConfig } from './triggers'
import { addSdkConfig } from './sdk'
import { addMigrationsConfig } from './migrations'
import { addPortfolioConfig } from './portfolio'
import { addMorpho } from './morpho'
Expand Down Expand Up @@ -43,7 +42,6 @@ export function API(stackContext: StackContext) {

const db = addRaysDb(summerContext)
addTriggersConfig(summerContext)
addSdkConfig(summerContext)
addMigrationsConfig(summerContext)
addPortfolioConfig(summerContext)
addMorpho(summerContext)
Expand Down

0 comments on commit 591a9a5

Please sign in to comment.