이세개발
article thumbnail

저번에 했었던 Action 사용해서 AWS S3 에 정적 웹 페이지를 구현을 했었던거에 이어서

이번에는 backend 에서 활용하기 좋은 CICD 방법을 사용해보려고 한다.

현재 golang의 웹 프레임워크중 하나인 echo framework 로 프로젝트를 진행 중인데 그것을 최종적으로 AWS ECS 에 올릴것이기에 AWS ECR 에 올려놓는게 좋겠다 생각이 들었다.

프로젝트 도커라이징

config 파일 정리 (viper 패키지)
데이터베이스와 관련된 정보가 있는 config.json 을 그대로 빌드를 하면 같이 들어가기 때문에 내용은 삭제해준다.

{
"server": {
"port": "8080"
},
"database": {
"host": "localhost",
"user": "root",
"password": "root",
"name": "userdb"
}}

대신 아래 "os" 임포트 후 이런식으로 지정을 해 주어서 docker 환경변수로 사용할 수 있도록 하였다.

package main

import (
"os"

func main() {
host := os.Getenv("DB_HOST")
user := os.Getenv("DB_USER")
password := os.Getenv("DB_PASSWORD")
dbName := os.Getenv("DB_NAME")
port := os.Getenv("SERVER_PORT")

도커파일을 작성해 준다

# Stage 1: Build the Go app
FROM golang:1.18 AS builder

# Set the Current Working Directory inside the container
WORKDIR /app

# Copy everything from the current directory to the Working Directory inside the container
COPY . .

# Build the Go app statically
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .

# Stage 2: Copy the build app to a slim image
FROM alpine:latest
WORKDIR /app

# Copy the binary from builder
COPY --from=builder /app/main .

# Ensure the binary is executable
RUN chmod +x ./main

# Expose port 8080
EXPOSE 8080

# Command to run the application
ENTRYPOINT ["./main"]

대부분 go 언어 1.17 로 구성을 하는데 오류가 나서 1.18로 구성했다.

Action Workflows 작성

원래 go와 nginx를 compose해서 배포를 하려고 했는데 일단 간단하게 먼저 해보려고 한다.
ECR repo, iam 설정 등은 생략하도록 하겠다.

.github/workflows/docker-publish.yaml

name: Build and Push to ECR

on:
  push:
    paths:
      - 'back/myauthapp/**'
    branches:
      - main

jobs:
  build-and-push:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout repo
      uses: actions/checkout@v3

    - name: Configure AWS credentials
      uses: aws-actions/configure-aws-credentials@v4
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: ${{ secrets.AWS_REGION }}

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

    - name: Build, tag, and push image to Amazon ECR
      env:
        ECR_REGISTRY: ${{ secrets.ECR_REGISTRY }}
        ECR_REPOSITORY: gopod
        IMAGE_TAG: latest
      run: |
        docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG back/myauthapp/
        docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG

secrets 설정 등 나머지는 이전 정적배포때와 같으니 그것을 참고하면 된다.

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
이부분이 많이 헷갈렸는데, 공식문서에 잘 설명이 되어있다.

arn:aws:iam::12345123312:user/32123-hk4-my-react-20231029

*참고

https://github.com/aws-actions/amazon-ecr-login

 

GitHub - aws-actions/amazon-ecr-login: Logs into Amazon ECR with the local Docker client.

Logs into Amazon ECR with the local Docker client. - GitHub - aws-actions/amazon-ecr-login: Logs into Amazon ECR with the local Docker client.

github.com

https://github.com/aws-actions/configure-aws-credentials

 

GitHub - aws-actions/configure-aws-credentials: Configure AWS credential environment variables for use in other GitHub Actions.

Configure AWS credential environment variables for use in other GitHub Actions. - GitHub - aws-actions/configure-aws-credentials: Configure AWS credential environment variables for use in other Git...

github.com

 

몇번의 뻘짓 끝, workflows성공

ECR 에도 push 정상적으로 들어와 있는것을 확인할 수 있다.

ECR 관련 명령어
AWS CLI에서 ECR (Elastic Container Registry)와 관련된 주요 명령어

  1. describe-repositories:
    • 특정 레포지토리 또는 레지스트리의 모든 레포지토리에 대한 정보
      aws ecr describe-repositories
  2. list-repositories:
    • 레포지토리의 목록을 제공하지만, 레포지토리에 대한 상세 정보는 제공하지 않는다.
      aws ecr list-repositories
  3. get-authorization-token:
    • ECR 레포지토리에 대한 인증 토큰을 반환하여 Docker 클라이언트를 인증.
      aws ecr get-authorization-token
  4. get-lifecycle-policy:
    • 레포지토리의 생명주기 정책을 반환
      aws ecr get-lifecycle-policy --repository-name <repository-name>
  5. create-repository:
    • 새로운 ECR 레포지토리를 생성
      aws ecr create-repository --repository-name <repository-name>
  6. delete-repository:
    • ECR 레포지토리를 삭제
      aws ecr delete-repository --repository-name <repository-name>
  7. list-images:
    • 레포지토리의 이미지 목록을 제공
      aws ecr list-images --repository-name <repository-name>
  8. get-login-password:
    • Amazon ECR 레지스트리에 대한 인증에 사용할 수 있는 패스워드를 반환
      aws ecr get-login-password

AWS CLI ECR 명령어 레퍼런스

profile

이세개발

@print(name)

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!