実現したいこと
AWS CodeBuildで、
aws/codebuild/standard:3.0からaws/codebuild/standard:7.0に変えた後再ビルド実行したら
buildspec.ymlに記載した「aws ecr describe-image-scan-findings」コマンドが失敗してしまうので、成功するように解決したいです。
発生している問題・分からないこと
エラーメッセージのうち下記エラーがきっかけで失敗しているように見えます。
[Container] 2024/02/08 09:09:20.363424 Running command scan_status=$(aws ecr describe-image-scan-findings --repository-name $target_project_name_l --image-id imageTag=latest | jq -r '.imageScanStatus.status' )
An error occurred (ImageNotFoundException) when calling the DescribeImageScanFindings operation: The image with imageId {imageDigest:'null', imageTag:'latest'} does not exist within the repository with name 'dcmprovisioningservice' in the registry with id '3544XXXXXXXX'
エラーメッセージ
error
1[Container] 2024/02/08 09:09:20.363424 Running command scan_status=$(aws ecr describe-image-scan-findings --repository-name $target_project_name_l --image-id imageTag=latest | jq -r '.imageScanStatus.status' ) 2 3An error occurred (ImageNotFoundException) when calling the DescribeImageScanFindings operation: The image with imageId {imageDigest:'null', imageTag:'latest'} does not exist within the repository with name 'dcmprovisioningservice' in the registry with id '3544XXXXXXXX' 4 5[Container] 2024/02/08 09:09:20.991607 Running command timeout_count=0 6 7[Container] 2024/02/08 09:09:20.997543 Running command while : 8do 9 echo "imageScanStatus: $scan_status"; 10 if [ $scan_status = "COMPLETE" ]; then 11 break; 12 fi 13 if [ $timeout_count -gt 20 ]; then 14 echo "Timeout!!"; 15 exit 1; 16 fi 17 sleep 3; 18 scan_status=$(aws ecr describe-image-scan-findings --repository-name $target_project_name_l --image-id imageTag=latest | jq -r '.imageScanStatus.status' ); 19 timeout_count=$(( timeout_count + 1 )); 20done 21 22imageScanStatus: 23/codebuild/output/tmp/script.sh: 7: [: =: unexpected operator 24 25An error occurred (ImageNotFoundException) when calling the DescribeImageScanFindings operation: The image with imageId {imageDigest:'null', imageTag:'latest'} does not exist within the repository with name 'dcmprovisioningservice' in the registry with id '3544XXXXXXXX' 26imageScanStatus: 27/codebuild/output/tmp/script.sh: 7: [: =: unexpected operator 28
該当のソースコード
yml
1version: 0.2 2 3env: 4 parameter-store: 5 MAVEN_TSCPP_DEVELOPER_USER: "MAVEN_TSCPP_DEVELOPER_USER" 6 MAVEN_TSCPP_DEVELOPER_PASSWORD: "MAVEN_TSCPP_DEVELOPER_PASSWORD" 7phases: 8 install: 9 commands: 10 - nohup /usr/local/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://127.0.0.1:2375 --storage-driver=overlay& 11 - curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - 12 - apt -y update 13 - apt -y install jq 14 - timeout 15 sh -c "until docker info; do echo .; sleep 1; done" 15 - cp -fr $CODEBUILD_SRC_DIR_BuildArtifact/build ./ 16 17 pre_build: 18 commands: 19 - export target_project_name_l=`echo "$TARGET_PROJECT_NAME" | awk '{print tolower($0)}'` 20 - export image_tag=$CODEBUILD_RESOLVED_SOURCE_VERSION 21 - export ecr_uri=354447979371.dkr.ecr.us-west-2.amazonaws.com/$target_project_name_l 22 23 # AWS認証情報取得 24 # stsのendpointに、明示的にデプロイ対象のregionを指定しないと、 25 # sts endpointから取得できる認証tokenが、デフォルトでONになっていないリージョンで操作権限のないものとなるため注意 26 - credentials=$(aws sts assume-role --duration-seconds 3600 --role-arn arn:aws:iam::354447979371:role/CrossAccountRole_832053171557_ECRPushRole --role-session-name session-hoge --region us-west-2 --endpoint https://sts.us-west-2.amazonaws.com | jq .Credentials) 27 - export AWS_ACCESS_KEY_ID=$(echo $credentials | jq -r .AccessKeyId) 28 - export AWS_SECRET_ACCESS_KEY=$(echo $credentials | jq -r .SecretAccessKey) 29 - export AWS_SESSION_TOKEN=$(echo $credentials | jq -r .SessionToken) 30 - export AWS_DEFAULT_REGION=us-west-2 31 - aws sts get-caller-identity 32 33 build: 34 commands: 35 - | 36 docker login 19maven.tscapi.g-book.com -u $MAVEN_TSCPP_DEVELOPER_USER -p $MAVEN_TSCPP_DEVELOPER_PASSWORD 37 docker pull 19maven.tscapi.g-book.com/openjdk:8-jdk 38 docker tag 19maven.tscapi.g-book.com/openjdk:8-jdk openjdk:8-jdk 39 docker pull 19maven.tscapi.g-book.com/openjdk:8-jdk-slim 40 docker tag 19maven.tscapi.g-book.com/openjdk:8-jdk-slim openjdk:8-jdk-slim 41 docker pull 19maven.tscapi.g-book.com/openjdk:8u282-jdk-slim 42 docker tag 19maven.tscapi.g-book.com/openjdk:8u282-jdk-slim openjdk:8u282-jdk-slim 43 docker pull 19maven.tscapi.g-book.com/alpine 44 docker tag 19maven.tscapi.g-book.com/alpine alpine 45 docker pull 19maven.tscapi.g-book.com/nginx@sha256:08262e7a01055bd33920b3f59e2249f438eea5d25cc67b8d8c7f5854437786d2 46 docker images 47 docker tag 2a36393edaf1 nginx 48 docker tag 2a36393edaf1 19maven.tscapi.g-book.com/nginx 49 50 # Jarファイルの名前を変更(Dockerfile.deploy.awsに定義されている名前と合わせるため) 51 - mv build/libs/*.jar build/libs/$TARGET_PROJECT_NAME-0.0.1-SNAPSHOT.jar 52 53 # dockerイメージ作成 54 - docker build -t $ecr_uri:latest -f Dockerfile.deploy.aws . 55 - docker tag $ecr_uri:latest $ecr_uri:$image_tag 56 57 # AWS ECRレポジトリ作成 58 - aws ecr create-repository --repository-name $target_project_name_l | awk '{print $1}' 59 60 # AWS ECRログイン 61 - aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin $ecr_uri 62 63 # ECRにイメージがPushされたタイミングで自動スキャンされるように設定する 64 - aws ecr put-image-scanning-configuration --repository-name $target_project_name_l --image-scanning-configuration scanOnPush=true 65 66 # dockerイメージをECRにPush 67 - docker push $ecr_uri:latest 68 - docker push $ecr_uri:$image_tag 69 70 ################### ↓↓↓ ECRスキャン ↓↓↓ ################### 71 # スキャンの進行状況を取得 72 - scan_status=$(aws ecr describe-image-scan-findings --repository-name $target_project_name_l --image-id imageTag=latest | jq -r '.imageScanStatus.status' ) 73 74 # スキャンの進行状況がCOMPLETEになるまで結果を取得 75 - timeout_count=0 76 - | 77 while : 78 do 79 echo "imageScanStatus: $scan_status"; 80 if [ $scan_status = "COMPLETE" ]; then 81 break; 82 fi 83 if [ $timeout_count -gt 20 ]; then 84 echo "Timeout!!"; 85 exit 1; 86 fi 87 sleep 3; 88 scan_status=$(aws ecr describe-image-scan-findings --repository-name $target_project_name_l --image-id imageTag=latest | jq -r '.imageScanStatus.status' ); 89 timeout_count=$(( timeout_count + 1 )); 90 done 91 92 以下略
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
buildspec.ymlの「while :」を「while true」に変えた。
補足
ビルドプロジェクトの詳細
・環境タイプ:Linux EC2
・イメージ:aws/codebuild/standard:7.0(aws/codebuild/standard:5.0でも試した。)

回答1件
あなたの回答
tips
プレビュー