Skip to content

Commit

Permalink
Deploy prod via GitHub Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
daohoangson committed Nov 12, 2023
1 parent 22a332c commit 7a6e124
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/sst.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: SST
on:
push:
branches:
- main
- sst

concurrency:
group: sst-prod

permissions:
contents: read
id-token: write

jobs:
prod:
runs-on: ubuntu-latest
environment:
name: production
url: https://t.me/bubby_bot
steps:
- uses: actions/checkout@v4
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::611714419758:role/GitHub
aws-region: us-east-1
- run: pnpm deploy --stage prod
5 changes: 3 additions & 2 deletions sst.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SSTConfig } from "sst";
import { API } from "./stacks/MyStack";
import { GitHub } from "./stacks/ProdStack";

export default {
config(_input) {
Expand All @@ -9,6 +10,6 @@ export default {
};
},
stacks(app) {
app.stack(API);
}
app.stack(API).stack(GitHub);
},
} satisfies SSTConfig;
34 changes: 34 additions & 0 deletions stacks/ProdStack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
ManagedPolicy,
OpenIdConnectPrincipal,
OpenIdConnectProvider,
Role,
} from "aws-cdk-lib/aws-iam";
import { StackContext } from "sst/constructs";

export function GitHub({ app, stack }: StackContext) {
if (app.stage !== "prod") {
return;
}

// https://docs.sst.dev/going-to-production#stacks-setup
const provider = new OpenIdConnectProvider(stack, "GitHub", {
url: "https://token.actions.githubusercontent.com",
clientIds: ["sts.amazonaws.com"],
});

new Role(stack, "GitHubActionsRole", {
assumedBy: new OpenIdConnectPrincipal(provider).withConditions({
StringLike: {
"token.actions.githubusercontent.com:sub":
// https://github.com/daohoangson/bubby
`repo:daohoangson/bubby:*`,
},
}),
description: "Role assumed for deploying from GitHub CI using AWS CDK",
managedPolicies: [
ManagedPolicy.fromAwsManagedPolicyName("AdministratorAccess"),
],
roleName: "GitHub",
});
}

0 comments on commit 7a6e124

Please sign in to comment.