Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add post creation #2

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Deploy LetUsDev
name: One-off Deploy

on:
workflow_dispatch:
Expand All @@ -11,6 +11,37 @@ jobs:
- uses: actions/setup-node@v4
with:
node-version: 20
# - run: npm install -g pnpm
# - run: pnpm ci
# - run: pnpm test

- name: Cache Next.js Build
uses: actions/cache@v4
with:
path: |
.next/
.open-next/
.sst/
key: cache-${{ hashFiles('**/pnpm-lock.yaml') }}-${{ hashFiles("**.[jt]s', '**.[jt]xs', ) }}
restore-keys: |
cache-${{ hashFiles('**/pnpm-lock.yaml') }}

- name: Install pnpm
run: npm install -g pnpm

- name: Install dependencies
run: pnpm install

- name: Install AWS Creds
run: |
mkdir -p ~/.aws
echo "[default]" > ~/.aws/credentials
echo "aws_access_key_id=${{ secrets.AWS_ACCESS_ID }}" >> ~/.aws/credentials
echo "aws_secret_access_key=${{ secrets.AWS_SECRET_ACCESS_KEY }}" >> ~/.aws/credentials

- name: Set SST Config Secret
run: |
npx sst secrets set COMMIT_SHA=${{ github.sha }} --stage production

- name: Deploy with SST
run: pnpm run deploy

- name: Clean up AWS Profile
run: rm -rf ~/.aws
53 changes: 53 additions & 0 deletions .github/workflows/stage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Stage Deploy via SST

on:
workflow_dispatch:

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20

- name: Cache Next.js Build
uses: actions/cache@v4
with:
path: |
.next/
.open-next/
.sst/
key: cache-${{ hashFiles('**/pnpm-lock.yaml') }}-${{ hashFiles("**.[jt]s', '**.[jt]xs', ) }}
restore-keys: |
cache-${{ hashFiles('**/pnpm-lock.yaml') }}

- name: Install pnpm
run: npm install -g pnpm

- name: Install dependencies
run: pnpm install

- name: Install AWS Creds
run: |
mkdir -p ~/.aws
echo "[default]" > ~/.aws/credentials
echo "aws_access_key_id=${{ secrets.AWS_ACCESS_ID }}" >> ~/.aws/credentials
echo "aws_secret_access_key=${{ secrets.AWS_SECRET_ACCESS_KEY }}" >> ~/.aws/credentials

- name: Set SST Config Secret
run: |
npx sst secrets set DATABASE_URL=${{ secrets.DATABASE_URL_STAGING }} --stage staging
npx sst secrets set NEXT_PUBLIC_SITE_URL=${{ secrets.NEXT_PUBLIC_SITE_URL_STAGING }} --stage staging
npx sst secrets set COMMIT_SHA=${{ github.sha }} --stage staging

- name: Run staging DB Migrations
run: |
STAGE=staging npx tsx cli/migrator.ts

- name: Deploy with SST
run: pnpm run deploy:stage

- name: Clean up AWS Profile
run: rm -rf ~/.aws
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,5 @@ sst secret set NEXT_PUBLIC_URL <your_next_public_url> --env <environment>
## Contributing

1. Fork the repository
2. Create a feature branch
2. Create a feature branch or create an issue
3. Submit a pull request
51 changes: 51 additions & 0 deletions cli/migrator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { migrate } from "drizzle-orm/postgres-js/migrator";
import { drizzle } from "drizzle-orm/neon-serverless";
import { Pool, neonConfig } from "@neondatabase/serverless";
import ws from "ws";

import * as schema from "../lib/schema";
import getSecret from "../lib/secrets";

async function performMigration() {
const dbUrlResponse = await getSecret("DATABASE_URL");
const dbUrl = dbUrlResponse?.Parameter?.Value;
if (!dbUrl) {
console.error("DATABASE_URL not found");
return;
}

neonConfig.webSocketConstructor = ws; // <-- this is the key bit

const pool = new Pool({ connectionString: dbUrl });
pool.on("error", (err) => console.error(err)); // deal with e.g. re-connect

const client = await pool.connect();

try {
await client.query("BEGIN");
const db = await drizzle(client, { schema });
await migrate(db, { migrationsFolder: "src/migrations" });
await client.query("COMMIT");
} catch (err) {
await client.query("ROLLBACK");
throw err;
} finally {
client.release();
}

await pool.end();
}

if (require.main === module) {
console.log("Running migrations");

performMigration()
.then((val) => {
console.log("Migration performed");
process.exit(0);
})
.catch((err) => {
console.log("err", err);
process.exit(1);
});
}
4 changes: 2 additions & 2 deletions lib/secrets.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { GetParameterCommand, SSMClient } from "@aws-sdk/client-ssm";

//Grab Secrets from AWS Parameter Store
const STAGE = "production";
const STAGE = process.env.STAGE ? process.env.STAGE : "production";
const PROJECT = "letusdev";
const REGION = "us-east-1";

export async function getSecret(secretName: string) {
export default async function getSecret(secretName: string) {
if (!secretName) {
throw new Error("Secret name is required");
}
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"dev": "next dev",
"start": "next start",
"deploy": "sst deploy --stage production",
"migrate": "tsx cli/migrator.ts",
"deploy:stage": "sst deploy --stage staging",
"takeDown": "npx sst remove --stage production",
"generate": "npx drizzle-kit generate",
"db": "sst shell drizzle-kit"
Expand Down Expand Up @@ -49,6 +51,7 @@
"@types/node": "20.11.5",
"@types/react": "18.2.48",
"@types/react-dom": "18.2.18",
"@types/ws": "^8.5.13",
"aws-cdk-lib": "2.142.1",
"drizzle-kit": "^0.27.1",
"tsx": "^4.19.2"
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.