[AWS] Codebuild 로컬에서 빌드 테스트 하기

[AWS] Codebuild 로컬에서 빌드 테스트 하기

아래의 내용을 진행하기 위해선 Docker가 설치되었다고 가정한다

실행하는 커맨드의 위치는 프로젝트 내부가 아닌 별도의 폴더라고 가정한다

MacOS를 기준으로 한다

목적

CodePipeline(이하 파이프라인)의 구성 중 CodeBuild(이하 빌드)를 지속적으로 테스트해야 하는 시점이 있었다.

파이프라인이 Trigger 되는 시작점인 CodeCommit(이하 소스)에 buildspec.yml 파일의 수정된 내용을 지속적으로 Commit/Push 하여, 파이프라인을 구동시켜서 확인하는 사항이 너무나도 번거롭고 불편하였다.

특히나 간단한 오탈자 수정 및 확인용도의 echo command 추가는 더더욱..

이에 따라 실제 AWS에 등록되어있는 파이프라인/빌드/소스를 사용하지 않고 로컬에서 빌드를 돌려보기로 하였다

Steps

아래 과정의 작업 디렉토리는 /dockerspace 라고 가정한다

aws-codebuild-docker-images 소스 다운로드 로컬에서 사용할 도커이미지 빌드 이미지를 통한 CodeBuild 구동 스크립트 전역 설정 로컬 파일(buildspec.yml) 검증

Step1. aws-codebuild-docker-images 소스 다운로드

aws/aws-codebuild-docker-images

AWS에서는 CodeBuild에서 사용하는 환경 이미지를 Docker 이미지로 제공한다.

$ git clone https://github.com/aws/aws-codebuild-docker-images.git

Step2. 로컬에서 사용할 도커이미지 빌드

현재 Amazon Linux 2(al2)와 Ubuntu 18.04의 standard버전은 Dockerfile 빌드가 실패한다.

이에따라 아래의 예제는 ubuntu의 unsupported 폴더의 nodejs 버전을 기준으로 한다.

추후 해결이 된다면 al2, ubuntu의 standard버전으로 변경을 하는 것이 옳을 것 같다.

관련링크: https://github.com/aws/aws-codebuild-docker-images/issues/286

Step1에서 받은 Repository로 진입하여 목록을 살펴보자

$ cd aws-codebuild-docker-images $ ls -al total 72 drwxr-xr-x 15 mac staff 480 12 27 15:17 . drwxr-xr-x 3 mac staff 96 12 27 15:16 .. drwxr-xr-x 13 mac staff 416 12 27 15:17 .git drwxr-xr-x 3 mac staff 96 12 27 15:17 .github -rw-r--r-- 1 mac staff 10 12 27 15:17 .gitignore -rw-r--r-- 1 mac staff 311 12 27 15:17 CODE_OF_CONDUCT.md -rw-r--r-- 1 mac staff 3619 12 27 15:17 CONTRIBUTING.md -rw-r--r-- 1 mac staff 4845 12 27 15:17 LICENSE.txt -rw-r--r-- 1 mac staff 406 12 27 15:17 Makefile -rw-r--r-- 1 mac staff 105 12 27 15:17 NOTICE.txt -rw-r--r-- 1 mac staff 2476 12 27 15:17 README.md drwxr-xr-x 4 mac staff 128 12 27 15:17 al2 -rw-r--r-- 1 mac staff 275 12 27 15:17 buildspec.yml drwxr-xr-x 7 mac staff 224 12 27 15:17 local_builds drwxr-xr-x 4 mac staff 128 12 27 15:17 ubuntu

CodeBuild에서 사용할 수 있는 환경 이미지는 크게 Amazon Linux 2(al2), Ubuntu가 있다.

글에서는 Ubuntu의 unsupported image중 하나인 nodejs버전을 선택하여 진행한다. (ubuntu 14.04를 base로 사용한다)

해당 폴더로 진입하여 제공되는 Dockerfile을 빌드하여 로컬에 이미지를 생성한다.

(이미지 명칭은 변경할 수 있으며, 아래의 예시 이미지명은 "aws/codebuild/nodejs:10.14.1" 이다)

# dockerspace/aws-codebuild-docker-images/ubuntu/unsupported_images/nodejs/10.14.1 $ cd ubuntu/unsupported_images/nodejs/10.14.1 $ ls -al total 32 drwxr-xr-x 5 mac staff 160 12 27 15:17 . drwxr-xr-x 10 mac staff 320 12 27 15:17 .. -rw-r--r-- 1 mac staff 7325 12 27 15:17 Dockerfile -rwxr-xr-x 1 mac staff 439 12 27 15:17 dockerd-entrypoint.sh -rw-r--r-- 1 mac staff 51 12 27 15:17 ssh_config $ docker build -t aws/codebuild/nodejs:10.14.1 . ... 빌드정보출력

빌드를 진행하면 빌드정보가 출력되며, 이 과정은 시간이 꽤 소요된다.

(체감상 30 ~ 40분은 걸린 것 같다. 돌려놓고 커피타임..)

빌드가 완료된 후 도커이미지를 확인하면 아래와 같이 빌드이미지를 확인할 수 있다.

$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE **aws/codebuild/nodejs 10.14.1 94abe3de8015 42 seconds ago 1.35GB** ubuntu 14.04.5 132b7427a3b4 2 years ago 188MB

Step3. 이미지를 통한 CodeBuild 구동 스크립트 전역 설정

Step1 에서 받은 Repository내에, Step2에서 빌드된 도커 이미지를 통하여 로컬에 위치한 buildspec.yml을 CodeBuild에서 빌드해볼 수 있는 스크립트가 포함되어 있다.

추후 여러 프로젝트를 진행할 수 있음에 따라 어느 경로에서든 빌드테스트를 하기 위하여 스크립트를 전역으로 설정해놓는다

# dockerspace/aws-codebuild-docker-images/local_builds $ cd ../../../local_builds/ $ chmod +x codebuild_build.sh $ cp codebuild_build.sh /usr/local/bin/codebuild $ codebuild The image name flag (-i) must be included for a build to run The artifact directory (-a) must be included for a build to run

이제 어느 프로젝트 경로에서도 buildspec.yml 파일이 위치한 곳이라면 codebuild를 통하여 빌드해볼 수 있다

Step4. 로컬 파일(buildspec.yml) 검증

이제 모든 준비가 완료되었다.

테스트할 buildspec.yml 파일을 준비한다. (이미 존재한다면 해당 디렉토리로 이동한다)

# buildspec.yml (예시) version: 0.2 phases: install: runtime-versions: nodejs: 12 commands: - echo install pre_build: commands: - echo pre_build build: commands: - echo build post_build: commands: - echo post_build

Step2에서 빌드된 도커이미지를 통하여 실행한다

$ codebuild -i aws/codebuild/nodejs:10.14.1 -a ./artifact # [options] # -i (필수) 생성한 이미지명을 입력한다. 이미지명 확인은 'docker images' 명령어를 통하여 확인할 수 있다 # -a (필수) 아티팩트 Output 디렉토리의 위치이다 # -e 환경변수 파일도 등록이 가능하다. e.g. build.env # -s 소스파일의 위치이다. buildspec.yml 파일이 위치한 소스파일의 루트경로이다 # -c AWS Configuration 설정을 지정할 수 있다 # -b buildspec.yml 파일을 override 할 수 있다. 이름/경로가 다른 경우 이용한다

빌드결과 예시는 다음과 같다

Build Command: docker run -it -v /var/run/docker.sock:/var/run/docker.sock -e "IMAGE_NAME=aws/codebuild/nodejs:10.14.1" -e "ARTIFACTS=/Users/mac/Documents/dockerspace/aws-codebuild-docker-images/ubuntu/unsupported_images/nodejs/10.14.1/." -e "SOURCE=/Users/mac/Documents/dockerspace/aws-codebuild-docker-images/ubuntu/unsupported_images/nodejs/10.14.1" -e "INITIATOR=mac" amazon/aws-codebuild-local:latest Unable to find image 'amazon/aws-codebuild-local:latest' locally latest: Pulling from amazon/aws-codebuild-local 5b90a54fcf20: Pull complete d39fe415fa78: Pull complete 7d4a35a5df7e: Pull complete 6e3e0414375b: Pull complete e50d07f984d4: Pull complete dbc780fd05a1: Pull complete 82ead0f09fa9: Pull complete c503dce6de10: Pull complete Digest: sha256:2c2b0a6b3595abfb5408cfa263d91ef280a910e2a03e920f65c3ffb9a97d0550 Status: Downloaded newer image for amazon/aws-codebuild-local:latest Removing network agent-resources_default Removing volume agent-resources_source_volume Removing volume agent-resources_user_volume Creating network "agent-resources_default" with the default driver Creating volume "agent-resources_source_volume" with local driver Creating volume "agent-resources_user_volume" with local driver Creating agent-resources_agent_1 ... done Creating agent-resources_build_1 ... done Attaching to agent-resources_agent_1, agent-resources_build_1 agent_1 | 2020/02/25 06:50:02 [Customer Config] Couldn't open specified customer config file: open /root/.aws/config: no such file or directory agent_1 | 2020/02/25 06:50:02 [Customer Config] Error parsing supplied customer config file: invalid argument agent_1 | [Container] 2020/02/25 06:50:03 Waiting for agent ping agent_1 | [Container] 2020/02/25 06:50:04 Waiting for DOWNLOAD_SOURCE agent_1 | [Container] 2020/02/25 06:50:05 Phase is DOWNLOAD_SOURCE agent_1 | [Container] 2020/02/25 06:50:05 CODEBUILD_SRC_DIR=/codebuild/output/src399164809/src agent_1 | [Container] 2020/02/25 06:50:05 YAML location is /codebuild/output/srcDownload/src/buildspec.yml agent_1 | [Container] 2020/02/25 06:50:05 Processing environment variables agent_1 | [Container] 2020/02/25 06:50:05 Moving to directory /codebuild/output/src399164809/src agent_1 | [Container] 2020/02/25 06:50:05 [WARN] Skipping install of runtimes. Runtime version selection is not supported by this build image. agent_1 | [Container] 2020/02/25 06:50:05 Registering with agent agent_1 | [Container] 2020/02/25 06:50:05 Phases found in YAML: 4 agent_1 | [Container] 2020/02/25 06:50:05 POST_BUILD: 1 commands agent_1 | [Container] 2020/02/25 06:50:05 INSTALL: 1 commands agent_1 | [Container] 2020/02/25 06:50:05 PRE_BUILD: 1 commands agent_1 | [Container] 2020/02/25 06:50:05 BUILD: 1 commands agent_1 | [Container] 2020/02/25 06:50:05 Phase complete: DOWNLOAD_SOURCE State: SUCCEEDED agent_1 | [Container] 2020/02/25 06:50:05 Phase context status code: Message: agent_1 | [Container] 2020/02/25 06:50:05 Entering phase INSTALL agent_1 | [Container] 2020/02/25 06:50:05 Running command echo install agent_1 | install agent_1 | agent_1 | [Container] 2020/02/25 06:50:05 Phase complete: INSTALL State: SUCCEEDED agent_1 | [Container] 2020/02/25 06:50:05 Phase context status code: Message: agent_1 | [Container] 2020/02/25 06:50:05 Entering phase PRE_BUILD agent_1 | [Container] 2020/02/25 06:50:05 Running command echo pre_build agent_1 | pre_build agent_1 | agent_1 | [Container] 2020/02/25 06:50:05 Phase complete: PRE_BUILD State: SUCCEEDED agent_1 | [Container] 2020/02/25 06:50:05 Phase context status code: Message: agent_1 | [Container] 2020/02/25 06:50:05 Entering phase BUILD agent_1 | [Container] 2020/02/25 06:50:05 Running command echo build agent_1 | build agent_1 | agent_1 | [Container] 2020/02/25 06:50:05 Phase complete: BUILD State: SUCCEEDED agent_1 | [Container] 2020/02/25 06:50:05 Phase context status code: Message: agent_1 | [Container] 2020/02/25 06:50:05 Entering phase POST_BUILD agent_1 | [Container] 2020/02/25 06:50:05 Running command echo post_build agent_1 | post_build agent_1 | agent_1 | [Container] 2020/02/25 06:50:05 Phase complete: POST_BUILD State: SUCCEEDED agent_1 | [Container] 2020/02/25 06:50:05 Phase context status code: Message: agent-resources_agent_1 exited with code 0 Stopping agent-resources_build_1 ... done Aborting on container exit...

AWS-CodeBuild-LocalBuild#Clone aws-codebuild-docker-images

필요한 aws-codebuild-local 이미지를 받아오고, 초기설정 및 소스를 다운로드 받아 buildspec.yml 파일의 내용의 실행결과를 볼 수 있다.

결론

AWS에서 제공해주는 CodeBuild 도커이미지를 통하여 로컬에도 같은 환경구성을 한 후 buildspec.yml 파일의 검증을 해보니 확실히 일일이 CodeCommit에 푸시하여 CodePipeline을 트리거링 하는것보다 생산속도가 좋아졌다.

하지만 현재 standard 버전들(Amazon Linux 2:2.0, Ubuntu18.04:3.0)이 로컬에서 빌드가 실패함에 따라서, 현재 개발은 Nodejs 12.13.0 으로 진행을 하는데 10.14.1 버전으로 검증을 하는 것이 꽤 꺼림칙하다.

실제 비즈니스로직과는 어느정도 무관하다 생각하여 진행해보았으나, 라이브러리의 종속성때문에라도 standard 로 빌드하여서 교체는 꼭 이뤄져야 할 것 같다

(혹시 standard 버전의 도커 빌드 방법을 아시는분은 알려주세요..)

참고

aws/aws-codebuild-docker-images

Announcing Local Build Support for AWS CodeBuild | Amazon Web Services

from http://yongyong-blog.tistory.com/2 by ccl(A) rewrite - 2021-09-28 09:26:55