ããã«ã¡ã¯ãAWS CLIã好ããªç¦å³¶ã§ãã
- ã¯ããã«
- æ¦è¦å³
- ãã¡ã¤ã«ä¸è¦§
- ãã©ã«ãæ§æ
- åãã¡ã¤ã«ã®è©³ç´°
- çµããã«
ã¯ããã«
ä»åã¯ã以ä¸ã®ãã³ãºãªã³ãå®æ½ããããããã³ãºãªã³ã®å 容ãåºã«CI/CD for Amazon ECSã§å©ç¨ãããã¡ã¤ã«ãã¾ã¨ãã¾ããã
âAWS CI/CD for Amazon ECS ãã³ãºãªã³
https://pages.awscloud.com/rs/112-TZM-766/images/AWS_CICD_ECS_Handson.pdf
æ¦è¦å³
æ¦è¦å³ã¯ä»¥ä¸ã®éãã§ãã
ãã¡ã¤ã«ä¸è¦§
- â buildspec.yml : ãã«ãå¦çã®å®ç¾©æ¸
- â¡Dockerfile : dockerãã«ãæã«å©ç¨ããå®ç¾©æ¸
- â¢appspec.yml : ãããã¤å¦çã®å®ç¾©æ¸
- â£taskdef.json : ã¿ã¹ã¯å®ç¾©ãä½æããå®ç¾©æ¸
- â¤imageDetail.json : Amazon ECS Blue/Green ãããã¤æã«å¿ è¦ãªå®ç¾©æ¸
ãã©ã«ãæ§æ
php-sample â src : ã½ã¼ã¹ã³ã¼ããæ ¼ç´ãããã©ã«ã â index.php : ã³ã³ãã³ã â appspec.yml : ãããã¤å¦çã®å®ç¾©æ¸ â buildspec.yml : ãã«ãå¦çã®å®ç¾©æ¸ â Dockerfile : docker buildã®å®ç¾©æ¸ â taskdef.json : ECSã®ã¿ã¹ã¯å®ç¾©ã®å®ç¾©æ¸
â»1 srcé
ä¸ã«æ ¼ç´ããã³ã³ãã³ã(index.php)ã®èª¬æã¯å²æãã¾ãã
â»2 æ¦è¦å³ã«è¨è¼ã®éããimageDetail.jsonã¯CodeBuildå®è¡æã«ä½æãããããä¸è¨ã«ã¯è¨è¼ãã¦ããã¾ããã
åãã¡ã¤ã«ã®è©³ç´°
CodeBuildã§å©ç¨
1. buildspec.yml: ãã«ãå¦çã®å®ç¾©æ¸
ä¸é¨å
容ãå¤ãã¦ããã¾ããããã³ãºãªã³ã§å©ç¨ããbuildspec.ymlã¯ä»¥ä¸ã®éãã§ãã
ãã¡ã¤ã«åã¯ããã©ã«ãbuildspec.ymlã¨ãªããã½ã¼ã¹ãã£ã¬ã¯ããªã®ã«ã¼ãã«ä¿åããå¿
è¦ãããã¾ããããã¡ã¤ã«åããã¹ãå¤æ´ãããã¨ãå¯è½ã§ãã
詳細ã¯ä»¥ä¸ã確èªãã ããã
CodeBuild ã®ãã«ãä»æ§ã«é¢ãããªãã¡ã¬ã³ã¹ - AWS CodeBuild
buildspec.ymlã®ä¾
version: 0.2 phases: pre_build: commands: - AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text) - aws ecr get-login-password | docker login --username AWS --password-stdin https://$AWS_ACCOUNT_ID.dkr.ecr.ap-northeast-1.amazonaws.com - REPOSITORY_URI=$AWS_ACCOUNT_ID.dkr.ecr.ap-northeast-1.amazonaws.com/php-sample - IMAGE_TAG=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7) build: commands: - docker build -t $REPOSITORY_URI:latest . - docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG post_build: commands: - docker push $REPOSITORY_URI:latest - docker push $REPOSITORY_URI:$IMAGE_TAG - printf '{"Version":"1.0","ImageURI":"%s"}' $REPOSITORY_URI:$IMAGE_TAG > imageDetail.json artifacts: files: imageDetail.json
以ä¸ã«åã»ã¯ã·ã§ã³ã®æ¦è¦ãè¨è¼ãã¾ãã
version
buildspec.ymlã®ãã¼ã¸ã§ã³ãå®ç¾©ãã¾ããç¾å¨(2022/07æç¹)ã¯ã0.2ãææ°ã¨ãªãã¾ãã
pre_build(ãã«ãã®åå¦ç)
ä»åã¯ã主ã«ECRã¸ã®ãã°ã¤ã³å¦çãè¡ã£ã¦ãã¾ãã
ã¾ããAWSã¢ã«ã¦ã³ãIDãä½æããã¤ã¡ã¼ã¸ãPushãããªãã¸ããªã®URIãã¤ã¡ã¼ã¸ã«ä»ä¸ããã¿ã°(ã³ãããID)ãç°å¢å¤æ°ã«å®ç¾©ãã¦ããã¾ãã
CODEBUILD_RESOLVED_SOURCE_VERSIONã¯ãCodeBuildç°å¢ã§å©ç¨ã§ããç°å¢å¤æ°ã¨ãªãããã«ãã®ã½ã¼ã¹ã³ã¼ãã®ãã¼ã¸ã§ã³ã®èå¥åãå®ç¾©ããã¦ããã¾ãã ãã®ä»ãå©ç¨ã§ããç°å¢å¤æ°ã¯ä»¥ä¸ã®ããã¥ã¡ã³ããåç §ãã ããã
ãã«ãç°å¢ã®ç°å¢å¤æ° - AWS CodeBuild
build(ãã«ã)
ä»åã¯ãdocker buildããã³ä½æããã¤ã¡ã¼ã¸ã«ã¿ã°ãä»ä¸ãã¦ããã¾ãã
post_build(ãã«ãã®å¾å¦ç)
ä»åã¯ãä½æããã¤ã¡ã¼ã¸ãlatestããã³ã¤ã¡ã¼ã¸ã¿ã°ãä»ä¸ããECRã«pushãã¦ãã¾ãã
ã¾ãä»åã¯ãECS Blue/Green ãããã¤ãè¡ããããå¿
è¦ãªimageDetail.jsonãçæãã¦ããã¾ãã
imageDetail.jsonã®ä¾:
{ "ImageURI": "ACCOUNTID.dkr.ecr.us-west-2.amazonaws.com/dk-image-repo@sha256:example3" }
- è£è¶³
ECS Blue/Green ãããã¤ãè¡ãéã¯ãimageDetail.jsonãå¿ è¦ã«ãªãã¾ããã ECSã®é常ã®ãããã¤ã®å ´åã¯ãimagedefinitions.jsonãå¿ è¦ã«ãªãã¾ãã
ã¤ã¡ã¼ã¸å®ç¾©ãã¡ã¤ã«ã®ãªãã¡ã¬ã³ã¹ - AWS CodePipeline
artifacts
post_buildã§çæããimageDetail.jsonãã¢ã¼ãã£ãã¡ã¯ãã¨ãã¦å®ç¾©ãã¾ãã ã¢ã¼ãã£ãã¡ã¯ãã¨ãã¦å®ç¾©ãããã¨ã§ãå¾ç¶ã®å¦ç(CodeDeploy)ã§å©ç¨ã§ãã¾ãã
2. Dockerfile: dockerãã«ãæã«å©ç¨ããå®ç¾©æ¸
Codeå å¼ã¨ã¯é¢ä¿ãªããdocker buildã³ãã³ããå©ç¨ããéã«å¿ è¦ã¨ãªããã¡ã¤ã«ã§ãã
Dockerfileã®ä¾
FROM php:7.4.0-apache COPY src/ /var/www/html
FROM
ãããªãã¯ãªãã¸ããªããã³ã³ããã¤ã¡ã¼ã¸ãåå¾ãã¾ãã
COPY
srcãã©ã«ãé ä¸ã®ãã¡ã¤ã«ã/var/www/html(Apacheã®ããã¥ã¡ã³ãã«ã¼ã)ã«ã³ãã¼ãã¾ãã
Dockerfileã®è©³ç´°
Dockerfile ãªãã¡ã¬ã³ã¹ â Docker-docs-ja 20.10 ããã¥ã¡ã³ã
CodeDeployã§å©ç¨
3. appspec.yml: ãããã¤å¦çã®å®ç¾©æ¸
ãã³ãºãªã³ã§å©ç¨ããappspec.ymlã¯ä»¥ä¸ã®éãã§ãã
version: 0.0 Resources: - TargetService: Type: AWS::ECS::Service Properties: TaskDefinition: "<TASK_DEFINITION>" LoadBalancerInfo: ContainerName: "php-sample-fargate" ContainerPort: "80"
version
appspec.ymlã®ãã¼ã¸ã§ã³ãå®ç¾©ãã¾ããç¾å¨(2022/07æç¹)ã¯ã0.0ã®ã¿å®ç¾©å¯è½ã§ãã
TaskDefinition
ãããã¤ãã Amazon ECS ãµã¼ãã¹ã®ã¿ã¹ã¯å®ç¾©ãå®ç¾©ãã¾ãã
<TASK_DEFINITION>ã¨å®ç¾©ãã¦ãããã¨ã§CodePipelineå®è¡æã«èªåã§æ´æ°ããã¾ãã
ContainerName
Amazon ECS ã¢ããªã±ã¼ã·ã§ã³ãå«ã Amazon ECS ã³ã³ããã®ååã§ããAmazon ECS ã¿ã¹ã¯å®ç¾©ã§æå®ãããã³ã³ããã«ããå¿ è¦ãããã¾ãã
ContainerPort
ãã©ãã£ãã¯ãã«ã¼ãã£ã³ã°ãããã³ã³ããä¸ã®ãã¼ããå®ç¾©ãã¾ãã
詳細
4. taskdef.json: ã¿ã¹ã¯å®ç¾©ãä½æããå®ç¾©æ¸
taskdef.jsonã¯ãECSã®ã¿ã¹ã¯å®ç¾©ã®JSONãã³ãã¼ããimageã®ç®æã ãã以ä¸ã®éãå¤æ´ãã¾ãã
"image": "<IMAGE1_NAME>",
å¼ç¨: https://pages.awscloud.com/rs/112-TZM-766/images/AWS_CICD_ECS_Handson.pdf#page=87
5. imageDetail.json: Amazon ECS Blue/Green ãããã¤æã«å¿ è¦ãªå®ç¾©æ¸
Codebuildã®post_buildæã«ä½æãããã¡ã¤ã«ã§ãã
imageDetail.jsonã®ä¾:
{ "ImageURI": "ACCOUNTID.dkr.ecr.us-west-2.amazonaws.com/dk-image-repo@sha256:example3" }
- è£è¶³
Codebuildã«å«ãããã½ã¼ã¹ã«ãã®ãã¡ã¤ã«ãå«ãããã¨ãå¯è½ã§ãã
ã¾ããã½ã¼ã¹ãECRã®å ´åããã®ãã¡ã¤ã«ã¯èªåã§çæããã¾ãã
çµããã«
Codeç³»ã«è§¦ããæ©ä¼ã¯ã»ã¼ãªãã£ãã®ã§ããããã³ãºãªã³ã¨æ¬ããã°ãã¾ã¨ãããã¨ã§å°ãCodeç³»ãç解ã§ãã¾ããã
ã©ãªããã®ãå½¹ã«ç«ã¦ãã°å¹¸ãã§ãã