aws / aws-codebuild-docker-images

Official AWS CodeBuild repository for managed Docker images http://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref.html
Other
1.11k stars 971 forks source link

Unable to run 'apt-get' on aws/codebuild/amazonlinux2-x86_64-standard:5.0 #699

Closed jjuannn closed 6 months ago

jjuannn commented 7 months ago

I'm using the aws/codebuild/amazonlinux2-x86_64-standard:5.0 image to run a CodeBuild build. My build spec is the next one:

version: 0.2

env:
  parameter-store:
    TOKEN: SONAR_TOKEN
    HOST: SONAR_HOST
    PROJECT: SONAR_CORE_API_PROJECT_KEY

phases:
  install:
    runtime-versions:
      nodejs: 18
    commands:
      - npm ci

  pre_build:
    commands:
      - echo "Pre build phase ..."
      - apt-get update
      - apt-get install -y jq
      - wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.7.0.2747-linux.zip
      - echo "Current path is >>>"
      - sh $pwd
      - unzip ./sonar-scanner-cli-4.7.0.2747-linux.zip
      - ls $pwd
      - export PATH=$PATH:./sonar-scanner-4.7.0.2747-linux/bin/

  build:
    commands:
      - npm run lint
      - npm run format
      - npm run build
      - npm run test
      - echo "Build phase ..."
      - sonar-scanner -Dsonar.login=$TOKEN -Dsonar.host.url=$HOST -Dsonar.projectKey=$PROJECT
      - sleep 5
      - curl https://sonarcloud.io/api/qualitygates/project_status?projectKey=$PROJECT >result.json
      - cat result.json
      - if [ $(jq -r '.projectStatus.status' result.json) = ERROR ] ; then $CODEBUILD_BUILD_SUCCEEDING -eq 0 ;fi

// .... rest of file

When the build runs, it fails in the apt-get update line with the following error:

60 |  /codebuild/output/tmp/script.sh: line 4: apt-get: command not found
61 | [Container] 2024/01/30 18:59:26.201525 Command did not exit successfully apt-get update exit status 127
62 | [Container] 2024/01/30 18:59:26.206512 Phase complete: PRE_BUILD State: FAILED
63 | [Container] 2024/01/30 18:59:26.206528 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: apt-get update. Reason: exit status 127

I also tried the workaround proposed here https://github.com/aws/aws-codebuild-docker-images/issues/594 but it's not working for me, besides it's something that has been already fixed.

Any hint of how can I make it work? Thanks

bateleurX commented 6 months ago

You have to use dnf command instead of apt-get, since apt-get is a package management command for Ubuntu not for Amazon Linux.

And jq is already installed in this image, therefore I don't think that you need not to install jq command.

Dylan-AWS commented 6 months ago

As mentioned by @bateleurX, you can use dnf or yum.

jjuannn commented 6 months ago

There was no difference between using Amazon Linux or Ubuntu for my use case, so I switched to an Ubuntu image and it's working now. Thanks guys.