Search

Code Build

아래의 3개파일 만든 후 Github Push

buildspec.yml

version: 0.2 env: variables: AWS_REGION: "AWS_REGION" ECR_REPO_NAME: "ECR_REPO_NAME" phases: pre_build: commands: - echo Logging in to Amazon ECR... - AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query "Account" --output text) - aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com - IMAGE_TAG=$(date -d "+9 hours" +%Y-%m-%d.%H.%M.%S) build: commands: - echo Build started on `date` - echo Building the Docker image... - docker build -t $ECR_REPO_NAME:latest . - docker tag $ECR_REPO_NAME:latest $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$ECR_REPO_NAME:$IMAGE_TAG post_build: commands: - echo Build completed on `date` - echo Pushing the Docker images... - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$ECR_REPO_NAME:$IMAGE_TAG - echo Writing image definitions file... - echo "$(jq .containerDefinitions[].image=\"$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$ECR_REPO_NAME:$IMAGE_TAG\" taskdef.json)" > taskdef.json artifacts: files: - appspec.yaml - taskdef.json
YAML
복사

Dockerfile

FROM golang:alpine WORKDIR /main COPY main.go . RUN apk add --no-cache curl && go mod init noah.io/ark/rest && go build main.go EXPOSE 8080 CMD ["./main"]
Docker
복사

taskdef.json

{ "executionRoleArn": "arn:aws:iam::362708816803:role/skills-ecs-task-role", "containerDefinitions": [ { "name": "skills-container", "image": "<IMAGE>", "essential": "true", "portMappings": [ { "protocol": "tcp", "containerPort": 80 } ], "healthCheck": { "retries": 3, "command": [ "CMD-SHELL", "curl -f http://localhost:80/health || exit 1" ], "timeout": 5, "interval": 30, "startPeriod": null } } ], "requiresCompatibilities": ["EC2"], "cpu": "512", "memory": "512", "family": "skills-td" }
JSON
복사

Name : skills-build

소스 - Github 추가

Amazon Linux 선택 및 IAM Role Name : skills-build-role

도커 권한 추가

환경변수 추가

Buildspec 파일 추가

로그 활성화

CodeBuild Role에 push-ecr 정책 할당

Complete