awslabs / aws-codebuild-jenkins-plugin

AWS CodeBuild integration as a Jenkins build step.
https://aws.amazon.com/codebuild
Apache License 2.0
149 stars 122 forks source link

Showing Test Results / Code Coverage in Jenkins #103

Open ErmanB opened 3 years ago

ErmanB commented 3 years ago

Hi,

I am trying to run tests in CodeBuild plugin and showing results on Jenkins. I was able to put test results on CodeBuild reports, but I am struggling to display these results on Jenkins UI. I searched online but unable to find any sample. I put the buildspec file & my jenkins stage below.

jenkins stage Testing


    stage('Testing') {
        steps {

            script {
                    echo ("caller identity with JenkinsMaster Role: ")
                    sh "aws sts get-caller-identity"

                    withAWS(region: "${region}", role: "${buildRole}", roleAccount: "${buildAccount}") {
                        echo ("caller identity within withAWS: ")
                        sh "aws sts get-caller-identity"
                        try {
                        // Initially anything inside "awsCodeBuild...." runs as jenkins-build-buildrole1-BuildCodeBuildRole1
                        // Use cbResult to get Build results - https://issues.jenkins-ci.org/browse/JENKINS-61259?focusedCommentId=386419&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel
                        cbResult = awsCodeBuild projectName: "${repoName}",
                            sourceControlType: 'project',
                            credentialsType: 'keys',
                            region: "${region}",
                            envVariables: "[ { assume_role_arn, arn:aws:iam::${devAccount}:role/jenkins-build-deployrole1-DeployCodeBuildRole1 }, { AWS_DEFAULT_REGION, ${region} } ]",
                            sourceTypeOverride: 'S3',
                            sourceLocationOverride: "${sourceLocation}/${artifactName}",
                            buildSpecFile: "buildspec_test.yml"
                        } catch (Exception cbEx) {
                            cbResult = cbEx.getCodeBuildResult()
                        }
                    }

                    echo ("********Results Start: ********")
                    echo cbResult.getArtifactsLocation()
                    echo cbResult.getBuildId()
                    echo cbResult.getArn()
                    echo currentBuild.result
                    echo ("********Results End: ********")
                    // A workaround to catch failing stage (until fixed in the code) - https://github.com/awslabs/aws-codebuild-jenkins-plugin/issues/63
                    // Only needed if using cbResult = .... (https://issues.jenkins-ci.org/browse/JENKINS-61259?focusedCommentId=386419&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel)
                    if(currentBuild.result == "FAILURE") {
                        throw new Exception()
                    }
            } //script
        } // steps
    } // stage - testing 

buildspec file

version: 0.2
env:
  parameter-store:
    TOKEN: /codebuild/bitbucket/token
phases:
  install:
    runtime-versions:
      nodejs: 12
    commands:
    # Get Chrome Information
    - curl -s S -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
    - echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list
    - apt-get -y update
    # Install chrome
    - apt-get -y install google-chrome-stable
    # Installation of all required packages.
    - apt-get install -yq gconf-service xvfb libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
  pre_build:
    commands:
      - echo "awscli ...."
      - aws --version
      - ls -l
      - echo "caller identity within withAWS BEFORE assuming the jenkins-build-deployrole1-DeployCodeBuildRole1 ':' "
      - aws sts get-caller-identity
      - TEMP_ROLE=''
      - TEMP_ROLE=`aws sts assume-role --role-arn ${assume_role_arn} --role-session-name test --region ${AWS_DEFAULT_REGION} --endpoint-url https://sts.${AWS_DEFAULT_REGION}.amazonaws.com`
      - export AWS_ACCESS_KEY_ID=$(echo "${TEMP_ROLE}" | jq -r '.Credentials.AccessKeyId')
      - export AWS_SECRET_ACCESS_KEY=$(echo "${TEMP_ROLE}" | jq -r '.Credentials.SecretAccessKey')
      - export AWS_SESSION_TOKEN=$(echo "${TEMP_ROLE}" | jq -r '.Credentials.SessionToken')
      - echo "caller identity within withAWS AFTER assuming the jenkins-build-deployrole1-DeployCodeBuildRole1 ':' "
      - aws sts get-caller-identity
      - echo "Installing NVM"
  build:
    commands:
      - echo "Token value from parameter store is':' ${TOKEN}"
      - echo "Runnings OMO FRONTEND UNIT TESTS ..."
      - echo "${CODEBUILD_SRC_DIR}"
      - ls -l "${CODEBUILD_SRC_DIR}"
      - echo "List files...."
      - ls -l
      - pip3 --version
      - npm --version
      - npm install
      - npm run utf-ci
      - ls -l
      - ls -l "test-results/"
  post_build:
    commands:
      - echo "OMO FRONTEND UNIT TESTS':' Completed..."
      - unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN
reports:
  FrontEndUnitTests: # CodeBuild will create a report group called "FrontEndUnitTests".
    files: #Store all of the files
      - '**/*'
    base-directory: '/test-results' # Location of the report
leoherran-aws commented 3 years ago

The plugin currently doesn't support displaying test reports in the Jenkins UI, but we have recorded your request for that functionality and will update this issue when we implement it.

ErmanB commented 3 years ago

The plugin currently doesn't support displaying test reports in the Jenkins UI, but we have recorded your request for that functionality and will update this issue when we implement it.

👍 Thank you for the quick response, Leo. Look forward to using new functionality once it's implemented.