Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws-cdk: Batch build configuration #22087

Closed
s1mrankaur opened this issue Sep 17, 2022 · 4 comments
Closed

aws-cdk: Batch build configuration #22087

s1mrankaur opened this issue Sep 17, 2022 · 4 comments
Labels
@aws-cdk/aws-batch Related to AWS Batch

Comments

@s1mrankaur
Copy link

s1mrankaur commented Sep 17, 2022

Describe the bug

I am using the pipeline from https://github.com/awslabs/aws-simple-cicd/. I need to enable batch build in test execution stage to run cypress tests in parallel.

I modified one of the project files to


import * as cdk from '@aws-cdk/core';
import * as codebuild from '@aws-cdk/aws-codebuild';
import * as iam from '@aws-cdk/aws-iam';

import { defaultEnvironment, NODE_JS_VERSION } from './environments/base-environment';
import { projectEnvironmentVars } from './environments/e2e-tests-project-environment';
// import { createReportGroupJsonObject } from '../../utils/utils';
// import {  Duration } from '@aws-cdk/core'

interface RunTestsProjectProps {
  testsBucketName: string,
  testsBucketArn: string,
  targetEnv: string,
  repoType: string,
  role: iam.Role,
  codeCovTokenArn: string,
}

export class RunTestsProject extends codebuild.PipelineProject {
  constructor(scope: cdk.Construct, id: string, props: RunTestsProjectProps) {
    const { testsBucketName, testsBucketArn, targetEnv, repoType, codeCovTokenArn } = props
    super(scope, id, {
      projectName: id,
      role: props.role,
      environment: defaultEnvironment,
      environmentVariables: projectEnvironmentVars({ testsBucketName, testsBucketArn, targetEnv, repoType, codeCovTokenArn }),
      timeout: cdk.Duration.hours(3),
      buildSpec: codebuild.BuildSpec.fromObject({
        version: '0.2',
        phases: {
          install: {
            'runtime-versions': {
              nodejs: NODE_JS_VERSION
            }
          },
          build: {
            commands: [
              'if [ ! -f "${CODEBUILD_SRC_DIR}/scripts/assume-cross-account-role.env" ]; then echo "assume-cross-account-this.role.env not found in repo" && aws s3 cp s3://${ARTIFACTS_BUCKET_NAME}/admin/cross-account/assume-cross-account-role.env ${CODEBUILD_SRC_DIR}/scripts/; else echo "Overriding assume-cross-account-role.env from repo"; fi',
              '. ${CODEBUILD_SRC_DIR}/scripts/assume-cross-account-role.env',
              'bash ${CODEBUILD_SRC_DIR}/scripts/final-tests.sh'
            ],
            batch: {
              'fail-fast': false,
              'build-list': [
                {
                identifier: 'build1'
                },
                {
                identifier: 'build2'
                }
              ]
            }
          },
        },
        artifacts: {
          files: '**/*'
        },
      })
    });
  }
}

When I go to the console, the buildspec.yml uploaded looks like this:


{
  "version": "0.2",
  "phases": {
    "install": {
      "runtime-versions": {
        "nodejs": "14"
      }
    },
    "build": {
      "commands": [
        "if [ ! -f \"${CODEBUILD_SRC_DIR}/scripts/assume-cross-account-role.env\" ]; then echo \"assume-cross-account-this.role.env not found in repo\" && aws s3 cp s3://${ARTIFACTS_BUCKET_NAME}/admin/cross-account/assume-cross-account-role.env ${CODEBUILD_SRC_DIR}/scripts/; else echo \"Overriding assume-cross-account-role.env from repo\"; fi",
        ". ${CODEBUILD_SRC_DIR}/scripts/assume-cross-account-role.env",
        "bash ${CODEBUILD_SRC_DIR}/scripts/final-tests.sh"
      ],
      "batch": {
        "fail-fast": "false",
        "build-list": [
          {
            "identifier": "build1"
          },
          {
            "identifier": "build2"
          }
        ]
      }
    }
  },
  "artifacts": {
    "files": "**/*"
  }
}


But the action on trigger throws:


[Container] 2022/09/16 23:33:56 Waiting for agent ping
2[Container] 2022/09/16 23:33:57 Waiting for DOWNLOAD_SOURCE
3[Container] 2022/09/16 23:36:03 Phase is DOWNLOAD_SOURCE
4[Container] 2022/09/16 23:36:04 CODEBUILD_SRC_DIR=/codebuild/output/src036205290/src
5[Container] 2022/09/16 23:36:04 YAML location is /codebuild/readonly/buildspec.yml
6[Container] 2022/09/16 23:36:06 Phase complete: DOWNLOAD_SOURCE State: FAILED
7[Container] 2022/09/16 23:36:06 Phase context status code: YAML_FILE_ERROR Message: yaml: unmarshal errors:
8  line 2: cannot unmarshal !!str `0.2` into float64

Expected Behavior

Expected the batch build to be triggered

Current Behavior

Throws the following error:

But the action on trigger throws:

[Container] 2022/09/16 23:33:56 Waiting for agent ping
2[Container] 2022/09/16 23:33:57 Waiting for DOWNLOAD_SOURCE
3[Container] 2022/09/16 23:36:03 Phase is DOWNLOAD_SOURCE
4[Container] 2022/09/16 23:36:04 CODEBUILD_SRC_DIR=/codebuild/output/src036205290/src
5[Container] 2022/09/16 23:36:04 YAML location is /codebuild/readonly/buildspec.yml
6[Container] 2022/09/16 23:36:06 Phase complete: DOWNLOAD_SOURCE State: FAILED
7[Container] 2022/09/16 23:36:06 Phase context status code: YAML_FILE_ERROR Message: yaml: unmarshal errors:
8  line 2: cannot unmarshal !!str `0.2` into float64

Reproduction Steps

  1. Deploy https://github.com/awslabs/aws-simple-cicd/
  2. Modifiy https://github.com/awslabs/aws-simple-cicd/blob/main/lib/projects/test-project.ts to include
"batch": {
        "fail-fast": "false",
        "build-list": [
          {
            "identifier": "build1"
          },
          {
            "identifier": "build2"
          }
        ]
      }
  1. Deploy and trigger the pipeline

Possible Solution

No response

Additional Information/Context

Would be good to at-least have documentation on this. I did see YML file sample the batch build but nothing for when it is to be added in CDK files directly

CDK CLI Version

2.35.0

Framework Version

No response

Node.js Version

14.18.3

OS

Linux

Language

Typescript

Language Version

No response

Other information

No response

@s1mrankaur s1mrankaur added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Sep 17, 2022
@github-actions github-actions bot added the @aws-cdk/aws-batch Related to AWS Batch label Sep 17, 2022
@s1mrankaur
Copy link
Author

Commenting to bump

@peterwoodworth
Copy link
Contributor

I think the buildspec template you're submitting isn't quite right. It looks to me like you should have your batch configurations separated from your phase configurations

@peterwoodworth peterwoodworth removed bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Dec 6, 2022
@github-actions
Copy link

github-actions bot commented Dec 6, 2022

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@HansFalkenberg-Visma
Copy link

HansFalkenberg-Visma commented Mar 23, 2024

Regardless of the bug in OP's BuildSpec, there does indeed appear to be an issue with CDK generated build specifications.

According to the syntax at https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html
the build spec must be in YAML format and the version property's value must be a number.

CDK fails both these requirements, but CodeBuild is apparently forgiving and accepts JSON, at least when the specification is inlined.

CodeBuild also accepts that the version property is a string, but only when doing a simple build run.

When doing a batch run, it will fail if the version is a string

cannot unmarshal !!str `0.2` into float64

This is simple enough to fix manually, but not so easy to correct during CDK synth.

I encountered other issues while trying to enable batch builds from the L3 construct pipelines.CodePipeline and I'll create a new GitHub issue with a workaround implementation for everything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-batch Related to AWS Batch
Projects
None yet
Development

No branches or pull requests

4 participants