Skip to content

Commit

Permalink
ci: add deploy api action
Browse files Browse the repository at this point in the history
  • Loading branch information
k1eu committed Aug 9, 2024
1 parent 86d1960 commit c2b7a43
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.git
**/node_modules
**/build
**/dist
**/terraform
**/tf
70 changes: 70 additions & 0 deletions .github/workflows/deploy-api-staging.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: STAGING - Deploy API

on:
push:
branches:
- main
- "deploy-staging/**"
paths:
- apps/api/**
- .github/workflows/deploy-api-staging.yml

jobs:
build-api:
runs-on: ubuntu-latest
environment:
name: staging

steps:
- uses: actions/checkout@v4

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1-node16
with:
aws-access-key-id: ${{ secrets.STAGING_AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.STAGING_AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.STAGING_AWS_REGION }}

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

- name: Build, tag, and push image to Amazon ECR
env:
ECR_REGISTRY: ${{ secrets.STAGING_AWS_ECR_REGISTRY }}
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -f ./Dockerfile --build-arg VERSION=$IMAGE_TAG -t $ECR_REGISTRY:$IMAGE_TAG .
docker tag $ECR_REGISTRY:$IMAGE_TAG $ECR_REGISTRY:latest
docker push $ECR_REGISTRY:$IMAGE_TAG
docker push $ECR_REGISTRY:latest
- name: Install Selleo CLI
uses: jaxxstorm/[email protected]
with:
repo: selleo/cli
tag: v0.26.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: ECS Deployment
env:
AWS_REGION: ${{ secrets.STAGING_AWS_REGION }}
ECS_CLUSTER: ${{ vars.STAGING_ECS_CLUSTER }}
ECS_SERVICE: ${{ vars.STAGING_ECS_SERVICE }}
DOCKER_IMAGE: ${{ secrets.STAGING_AWS_ECR_REGISTRY }}:${{ github.sha }}
run: selleo aws ecs deploy --region $AWS_REGION --cluster $ECS_CLUSTER --service $ECS_SERVICE --docker-image $DOCKER_IMAGE --one-off migrate

- name: ECS Run migrations
env:
AWS_REGION: ${{ secrets.STAGING_AWS_REGION }}
ECS_CLUSTER: ${{ vars.STAGING_ECS_CLUSTER }}
ECS_SERVICE: ${{ vars.STAGING_ECS_SERVICE }}
run: selleo aws ecs run --region $AWS_REGION --cluster $ECS_CLUSTER --service $ECS_SERVICE --one-off migrate

- name: ECS Run seeds
env:
AWS_REGION: ${{ secrets.STAGING_AWS_REGION }}
ECS_CLUSTER: ${{ vars.STAGING_ECS_CLUSTER }}
ECS_SERVICE: ${{ vars.STAGING_ECS_SERVICE }}
run: selleo aws ecs run --region $AWS_REGION --cluster $ECS_CLUSTER --service $ECS_SERVICE --one-off seed
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM node:20.15.0-alpine AS base
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable

WORKDIR /app

COPY . .

RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install
RUN pnpm build --filter=api
# TODO: Move pnpm deploy to turbo prune workflow
RUN pnpm deploy --filter=api pnpm-deploy-output --prod

FROM node:20.15.0-alpine
WORKDIR /app
COPY --from=base /app/pnpm-deploy-output /app

RUN chmod +x /app/entrypoint.sh

ENTRYPOINT [ "/app/entrypoint.sh" ]
18 changes: 18 additions & 0 deletions apps/api/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh
set -e

COMMAND="${1:-server}"

if [ $COMMAND = "server" ]; then
echo "Starting server..."
npm run start:prod
elif [ $COMMAND = "migrate" ]; then
echo "Running migrations..."
npm run db:migrate
elif [ $COMMAND = "seed" ]; then
echo "Running seeds..."
node /app/dist/bin/seed.js
else
echo "Usage: entrypoint.sh [server|migrate]"
exit 1
fi
2 changes: 1 addition & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"start:dev": "nest start --watch",
"dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"start:prod": "node dist/src/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"lint-tsc": "tsc --noEmit && eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
Expand Down

0 comments on commit c2b7a43

Please sign in to comment.