Codebuild - AWS CI/CD 구축하기
Codebuild 삽질의 기록
내용
codepipeline을 구축하여 repository에 code를 형상관리, 빌드 시 아티팩트를 S3에 저장한다.
1. CodeCommit
- 소스코드를 이용, codecommit 에 repository를 생성한다.
- 소스코드를 형상관리하던 branch 에 push 가 발생하면 자동으로 codecommit repository 로 push 되도록 연결한다.
2. CodeBuild
- codebuild.json
- 빌드 프로젝트의 속성을 설정하는 파일이다. json 파일을 이용하여 CLI로 업데이트 가능하다.
example code
{
"name": "<project-name>",
"description": "<description>",
"source": {
"type": "CODECOMMIT",
"location": "<source-location>",
"gitCloneDepth": "0",
"buildspec": "buildspec.yml",
"InsecureSsl": true,
"reportBuildStatus": true,
"gitSubmodulesConfig": {
"fetchSubmodules": "true"
},
},
"artifacts": {
"type": "S3",
"location": "s3-bucket-name",
"path": "<artifacts-path>",
"namespaceType": "BUILD_ID",
"name": "<artifacts-name>",
"overrideArtifactName": true,
"packaging": "NONE"
},
"cache": {
"type": "<cache-type>",
"location": "<cache-location>",
"mode": [
"<cache-mode>"
]
},
"environment": {
"type": "LINUX_CONTAINER",
"image": "aws/codebuild/amazonlinux2-x86_64-standard:2.0",
"computeType": "BUILD_GENERAL1_SMALL",
"imagePullCredentialsType": "SERVICE_ROLE",
"privilegedMode": true
},
"serviceRole": "<service-role>",
"timeoutInMinutes": 20,
"queuedTimeoutInMinutes": 10,
"vpcConfig": {
"securityGroupIds": [
"<security-group-id>"
],
"subnets": [
"<subnet-id>"
],
"vpcId": "<vpc-id>"
},
"badgeEnabled": "<badge-enabled>",
"logsConfig": {
"cloudWatchLogs": {
"status": "<cloudwatch-logs-status>",
"groupName": "<group-name>",
"streamName": "<stream-name>"
},
"s3Logs": {
"status": "<s3-logs-status>",
"location": "<s3-logs-location>",
"encryptionDisabled": "<s3-logs-encryption-disabled>"
}
},
"concurrentBuildLimit": 0
}
key | value |
---|---|
artifacts/location | 아티팩트를 저장할 S3 bucket name |
artifacts/namespaceType | (N)BUILD_ID가 아티팩트 이름 앞에 지정된다 |
- buildspec.yml
- codecommit repository의 최상단에 해당 파일을 저장하여 push 한다
example code
version: 0.2
phases:
build:
commands:
- echo Building ...
- echo $CODEBUILD_SRC_DIR
- echo $CODEBUILD_BUILD_NUMBER
artifacts:
files:
- location
name: artifact-name
discard-paths: no | yes
base-directory: location
exclude-paths: excluded paths
enable-symlinks: no | yes
s3-prefix: prefix
key | value |
---|---|
version | AWS 0.2를 권장한다 |
phases | install, pre_build, build, post_build 로 나뉘고 사용될 명령어를 기재한다 |
- 필요한 환경변수들도 가져다 쓸 수 있다
2-1. Troubleshooting
Error code:
Phase context status code: CLIENT_ERROR Message: no matching artifact paths found
Solution:
- codebuild.json 의 빌드 프로젝트 - artifact 설정에 path 는 S3 아티팩트 결과물을 저장하는 위치를 지칭한다.
- buildspec.yml 의 artifact - file: / location 은 빌드가 실행되는 temp 소스의 위치를 지정한다. ‘*/’ 설정은 소스 폴더의 집약적인 지칭이다.
- buildspec.yml 의 artifact - name 에 지정한 이름으로 타겟 결과물 파일/폴더를 생성하고 싶을 경우, 콘솔에서 Enable semantic versioning 을 체크하거나 codebuild.json 에서 artifacts/name 에 overrideArtifactName Flag 를 설정해야 한다.