Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
TAK848 committed Feb 26, 2024
2 parents ea04cf7 + 42d24f4 commit d5a2dc8
Show file tree
Hide file tree
Showing 12 changed files with 358 additions and 62 deletions.
111 changes: 111 additions & 0 deletions .github/workflows/deploy-dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
name: Build and Push Docker Image

on:
push:
branches:
- develop
env:
ENV: dev
AWS_REGION: ap-northeast-1
AWS_ACCOUNT_ID: 905418376731

jobs:
build-and-push:
outputs:
ecr_image_name: ${{ steps.set_outputs.outputs.ecr_image_name }}
runs-on: ubuntu-latest
permissions:
packages: write
id-token: write
contents: read
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: configure aws credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::905418376731:role/magische-${{ env.ENV }}-api-deploy
aws-region: ${{ env.AWS_REGION }}

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

- id: set_env
name: set env
run: |
echo "image_tag=build-$(date +%Y%m%d)-${{ github.sha }}" >> $GITHUB_OUTPUT
echo "ecr_repository_name=${{ steps.login-ecr.outputs.registry }}/magische-${{ env.ENV }}-api" >> $GITHUB_OUTPUT
- id: set_outputs
name: set outputs
run: |
echo "ecr_image_name=${{ steps.set_env.outputs.ecr_repository_name }}:${{ steps.set_env.outputs.image_tag }}" >> $GITHUB_OUTPUT
- uses: docker/metadata-action@v5
id: meta
with:
images: |
ghcr.io/${{ github.repository }}/server
${{ steps.set_env.outputs.ecr_repository_name }}
tags: |
type=raw,value=${{ steps.set_env.outputs.image_tag }}
type=raw,value=latest
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
cache-from: type=gha
cache-to: type=gha,mode=max
file: ./Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
target: deploy
platforms: linux/amd64,linux/arm64

deploy:
permissions:
id-token: write
contents: read
needs: [build-and-push]
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: configure aws credentials
uses: aws-actions/configure-aws-credentials@v1
with:
role-to-assume: arn:aws:iam::905418376731:role/magische-${{ env.ENV }}-api-deploy
aws-region: ${{ env.AWS_REGION }}

- uses: kayac/ecspresso@v2
with:
version-file: ./infra/ecs/.ecspresso-version

- working-directory: ./infra/ecs
run: |
ecspresso deploy --config ecspresso.yml
env:
AWS_REGION: ${{ env.AWS_REGION }}
AWS_ACCOUNT_ID: ${{ env.AWS_ACCOUNT_ID }}
ENV: ${{ env.ENV }}
IMAGE_NAME: ${{ needs.build-and-push.outputs.ecr_image_name }}
CPU: 256
MEMORY: 512
CPU_ARCHITECTURE: ARM64
111 changes: 111 additions & 0 deletions .github/workflows/deploy-prd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
name: Build and Push Docker Image

on:
push:
branches:
- main
env:
ENV: prd
AWS_REGION: ap-northeast-1
AWS_ACCOUNT_ID: 905418376731

jobs:
build-and-push:
outputs:
ecr_image_name: ${{ steps.set_outputs.outputs.ecr_image_name }}
runs-on: ubuntu-latest
permissions:
packages: write
id-token: write
contents: read
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: configure aws credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::905418376731:role/magische-${{ env.ENV }}-api-deploy
aws-region: ${{ env.AWS_REGION }}

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

- id: set_env
name: set env
run: |
echo "image_tag=build-$(date +%Y%m%d)-${{ github.sha }}" >> $GITHUB_OUTPUT
echo "ecr_repository_name=${{ steps.login-ecr.outputs.registry }}/magische-${{ env.ENV }}-api" >> $GITHUB_OUTPUT
- id: set_outputs
name: set outputs
run: |
echo "ecr_image_name=${{ steps.set_env.outputs.ecr_repository_name }}:${{ steps.set_env.outputs.image_tag }}" >> $GITHUB_OUTPUT
- uses: docker/metadata-action@v5
id: meta
with:
images: |
ghcr.io/${{ github.repository }}/server
${{ steps.set_env.outputs.ecr_repository_name }}
tags: |
type=raw,value=${{ steps.set_env.outputs.image_tag }}
type=raw,value=latest
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
cache-from: type=gha
cache-to: type=gha,mode=max
file: ./Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
target: deploy
platforms: linux/amd64,linux/arm64

deploy:
permissions:
id-token: write
contents: read
needs: [build-and-push]
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: configure aws credentials
uses: aws-actions/configure-aws-credentials@v1
with:
role-to-assume: arn:aws:iam::905418376731:role/magische-${{ env.ENV }}-api-deploy
aws-region: ${{ env.AWS_REGION }}

- uses: kayac/ecspresso@v2
with:
version-file: ./infra/ecs/.ecspresso-version

- working-directory: ./infra/ecs
run: |
ecspresso deploy --config ecspresso.yml
env:
AWS_REGION: ${{ env.AWS_REGION }}
AWS_ACCOUNT_ID: ${{ env.AWS_ACCOUNT_ID }}
ENV: ${{ env.ENV }}
IMAGE_NAME: ${{ needs.build-and-push.outputs.ecr_image_name }}
CPU: 256
MEMORY: 512
CPU_ARCHITECTURE: ARM64
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# build for deploy
FROM golang:1.21-bullseye as deploy-builder
FROM golang:1.22-bullseye as deploy-builder

WORKDIR /app

Expand Down Expand Up @@ -33,7 +33,7 @@ CMD ["/app/migrate"]


# for local development with air
FROM golang:1.21-bullseye as dev
FROM golang:1.22-bullseye as dev
WORKDIR /app
RUN go install github.com/cosmtrek/air@latest && \
go install github.com/volatiletech/sqlboiler/v4@latest && \
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

type Config struct {
Port int `env:"PORT" envDefault:"80"`
Port int `env:"PORT" envDefault:"8080"`
Env string `env:"ENV" envDefault:"dev"`
BaseURL string `env:"BASE_URL" envDefault:"http://localhost:8080"`
SecretKey string `env:"SECRET_KEY" envDefault:"secret"`
Expand Down
14 changes: 6 additions & 8 deletions db/db.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package db

import (
"context"
"database/sql"
"fmt"
"time"

"github.com/geekcamp-vol11-team30/backend/config"
"go.uber.org/zap"
Expand Down Expand Up @@ -42,11 +40,11 @@ func NewDB(cfg *config.Config, logger *zap.Logger) (*sql.DB, error) {
return nil, err
}

ctx, canncel := context.WithTimeout(context.Background(), 10*time.Second)
defer canncel()
if err := db.PingContext(ctx); err != nil {
logger.Error("failed to ping db", zap.Error(err))
return nil, err
}
// ctx, canncel := context.WithTimeout(context.Background(), 10*time.Second)
// defer canncel()
// if err := db.PingContext(ctx); err != nil {
// logger.Error("failed to ping db", zap.Error(err))
// return nil, err
// }
return db, nil
}
1 change: 1 addition & 0 deletions infra/ecs/.ecspresso-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.3.2
55 changes: 55 additions & 0 deletions infra/ecs/ecs-task-def.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
containerDefinitions: [
{
cpu: 0,
essential: true,
image: '{{ must_env `IMAGE_NAME` }}',
logConfiguration: {
logDriver: 'awslogs',
options: {
'awslogs-group': '/ecs/magische-{{ must_env `ENV` }}-api-server',
'awslogs-region': '{{ must_env `AWS_REGION` }}',
'awslogs-stream-prefix': 'magische-{{ must_env `ENV` }}-api-server',
},
},
name: 'magische-{{ must_env `ENV` }}-api',
portMappings: [
{
appProtocol: '',
containerPort: 8080,
hostPort: 8080,
protocol: 'tcp',
},
],
},
],
cpu: '{{ must_env `CPU` }}',
executionRoleArn: 'arn:aws:iam::905418376731:role/magische-{{ must_env `ENV` }}-api-server-task-exec',
family: 'magische-{{ must_env `ENV` }}-api',
ipcMode: '',
memory: '{{ must_env `MEMORY` }}',
networkMode: 'awsvpc',
pidMode: '',
requiresCompatibilities: [
'FARGATE',
],
runtimePlatform: {
cpuArchitecture: '{{ must_env `CPU_ARCHITECTURE` }}',
operatingSystemFamily: 'LINUX',
},
tags: [
{
key: 'Env',
value: '{{ must_env `ENV` }}',
},
{
key: 'Service',
value: 'api',
},
{
key: 'Name',
value: 'magische-{{ must_env `ENV` }}-api-server',
},
],
taskRoleArn: 'arn:aws:iam::905418376731:role/magische-{{ must_env `ENV` }}-api-server-task',
}
16 changes: 16 additions & 0 deletions infra/ecs/ecspresso.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
region: 'ap-northeast-1',
cluster: 'magische-{{ must_env `ENV` }}',
service: 'magische-{{ must_env `ENV` }}-api',
service_definition: '',
task_definition: 'ecs-task-def.jsonnet',
timeout: '10m0s',
// plugins: [
// {
// name: 'tfstate',
// config: {
// url: 'remote://app.terraform.io/magische/magische_infra_dev',
// },
// },
// ],
}
6 changes: 3 additions & 3 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ func SetRequestLoggerToEcho(e *echo.Echo, logger *zap.Logger) {
}
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if c.Path() == "/health" {
return next(c)
}
// if c.Path() == "/health" {
// return next(c)
// }
return middleware.RequestLoggerWithConfig(cfg)(next)(c)
}
})
Expand Down
Loading

0 comments on commit d5a2dc8

Please sign in to comment.