# BuildSpec

BuildSpec for CodeBuild projects.

Obtainable from: BuildSpec.fromAsset, BuildSpec.fromObject, BuildSpec.fromObjectToYaml, BuildSpec.fromSourceFilename

- Source: [`aws-codebuild/lib/build-spec.ts`](https://github.com/aws/aws-cdk/blob/v2.277.0/packages/aws-cdk-lib/aws-codebuild/lib/build-spec.ts#L12)
- Type last changed: v2.67.0 · 2 Mar 2023

## Example

TypeScript:

```ts
import * as codebuild from 'aws-cdk-lib/aws-codebuild';

const codebuildProject = new codebuild.Project(this, 'Project', {
  projectName: 'MyTestProject',
  buildSpec: codebuild.BuildSpec.fromObject({
    version: '0.2',
    phases: {
      build: {
        commands: [
          'echo "Hello, CodeBuild!"',
        ],
      },
    },
  }),
});

const task = new tasks.CodeBuildStartBuild(this, 'Task', {
  project: codebuildProject,
  integrationPattern: sfn.IntegrationPattern.RUN_JOB,
  environmentVariablesOverride: {
    ZONE: {
      type: codebuild.BuildEnvironmentVariableType.PLAINTEXT,
      value: sfn.JsonPath.stringAt('$.envVariables.zone'),
    },
  },
});
```

Python:

```python
import aws_cdk.aws_codebuild as codebuild

codebuild_project = codebuild.Project(self, "Project",
    project_name="MyTestProject",
    build_spec=codebuild.BuildSpec.from_object({
        "version": "0.2",
        "phases": {
            "build": {
                "commands": ["echo \"Hello, CodeBuild!\""
                ]
            }
        }
    })
)

task = tasks.CodeBuildStartBuild(self, "Task",
    project=codebuild_project,
    integration_pattern=sfn.IntegrationPattern.RUN_JOB,
    environment_variables_override={
        "ZONE": codebuild.BuildEnvironmentVariable(
            type=codebuild.BuildEnvironmentVariableType.PLAINTEXT,
            value=sfn.JsonPath.string_at("$.envVariables.zone")
        )
    }
)
```

Java:

```java
import software.amazon.awscdk.services.codebuild.*;

Project codebuildProject = Project.Builder.create(this, "Project")
        .projectName("MyTestProject")
        .buildSpec(BuildSpec.fromObject(Map.of(
                "version", "0.2",
                "phases", Map.of(
                        "build", Map.of(
                                "commands", List.of("echo \"Hello, CodeBuild!\""))))))
        .build();

CodeBuildStartBuild task = CodeBuildStartBuild.Builder.create(this, "Task")
        .project(codebuildProject)
        .integrationPattern(IntegrationPattern.RUN_JOB)
        .environmentVariablesOverride(Map.of(
                "ZONE", BuildEnvironmentVariable.builder()
                        .type(BuildEnvironmentVariableType.PLAINTEXT)
                        .value(JsonPath.stringAt("$.envVariables.zone"))
                        .build()))
        .build();
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
using Amazon.CDK.AWS.CodeBuild;

var codebuildProject = new Project(this, "Project", new ProjectProps {
    ProjectName = "MyTestProject",
    BuildSpec = BuildSpec.FromObject(new Dictionary<string, object> {
        { "version", "0.2" },
        { "phases", new Dictionary<string, IDictionary<string, string[]>> {
            { "build", new Struct {
                Commands = new [] { "echo \"Hello, CodeBuild!\"" }
            } }
        } }
    })
});

var task = new CodeBuildStartBuild(this, "Task", new CodeBuildStartBuildProps {
    Project = codebuildProject,
    IntegrationPattern = IntegrationPattern.RUN_JOB,
    EnvironmentVariablesOverride = new Dictionary<string, BuildEnvironmentVariable> {
        { "ZONE", new BuildEnvironmentVariable {
            Type = BuildEnvironmentVariableType.PLAINTEXT,
            Value = JsonPath.StringAt("$.envVariables.zone")
        } }
    }
});
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import "github.com/aws/aws-cdk-go/awscdk"

codebuildProject := codebuild.NewProject(this, jsii.String("Project"), &ProjectProps{
	ProjectName: jsii.String("MyTestProject"),
	BuildSpec: codebuild.BuildSpec_FromObject(map[string]interface{}{
		"version": jsii.String("0.2"),
		"phases": map[string]map[string][]*string{
			"build": map[string][]*string{
				"commands": []*string{
					jsii.String("echo \"Hello, CodeBuild!\""),
				},
			},
		},
	}),
})

task := tasks.NewCodeBuildStartBuild(this, jsii.String("Task"), &CodeBuildStartBuildProps{
	Project: codebuildProject,
	IntegrationPattern: sfn.IntegrationPattern_RUN_JOB,
	EnvironmentVariablesOverride: map[string]BuildEnvironmentVariable{
		"ZONE": &BuildEnvironmentVariable{
			"type": codebuild.BuildEnvironmentVariableType_PLAINTEXT,
			"value": sfn.JsonPath_stringAt(jsii.String("$.envVariables.zone")),
		},
	},
})
```

## Initializer

TypeScript:

```ts
new BuildSpec()
```

Python:

```python
BuildSpec()
```

Java:

```java
new BuildSpec()
```

C# / .NET:

```csharp
new BuildSpec()
```

Golang:

```go
NewBuildSpec()
```

## Properties

- `isImmediate`: boolean — Whether the buildspec is directly available or deferred until build-time.

## Methods

- `fromAsset(path: string): BuildSpec // chainable` (static) — Use the contents of a local file as the build spec string.

  Use this if you have a local .yml or .json file that you want to use as the buildspec

- `fromObject(value: { [key: string]: any }): BuildSpec // chainable` (static)

- `fromObjectToYaml(value: { [key: string]: any }): BuildSpec // chainable` (static) — Create a buildspec from an object that will be rendered as YAML in the resulting CloudFormation template.

  **Parameters**

  - `value` — the object containing the buildspec that will be rendered as YAML.

- `fromSourceFilename(filename: string): BuildSpec // chainable` (static) — Use a file from the source as buildspec.

  Use this if you want to use a file different from 'buildspec.yml'`

- `toBuildSpec(scope?: Construct): string` — Render the represented BuildSpec.
