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

(amplify-alpha): (Java CDK generate build phases in order) #28256

Open
thirteendian opened this issue Dec 5, 2023 · 1 comment
Open

(amplify-alpha): (Java CDK generate build phases in order) #28256

thirteendian opened this issue Dec 5, 2023 · 1 comment
Labels
@aws-cdk/aws-amplify Related to AWS Amplify bug This issue is a bug. effort/medium Medium work item – several days of effort p3

Comments

@thirteendian
Copy link

thirteendian commented Dec 5, 2023

Describe the bug

Java CDK will not generate amplify.yml phases in order of preBuild, build, and postBuild.

Expected Behavior

So I hope if it's possible for CDK Java Package to always generate the amplify.yml phases in order of preBuild, then build, then postBuild, regardless of the order in which they are defined by java.utils.Map.

Current Behavior

I'm currently using CDK Java amplify-alpha service to deploy my next.js frontend. I noticed that after cdk deploy, the build process error occurred as

# Starting phase: build
2023-12-05T09:42:57.747Z [INFO]: # Executing command: npm run build
2023-12-05T09:43:11.049Z [INFO]: > [email protected] build
> next build
endeg. 1. 0 but: apm run build
2023-12-05T09:43:11.055Z [WARNING]: sh: line 1: next: command not found
...

Because the amplify.yml defined by java.util.Map does not maintain the order of phases as preBuild then build, it might generate something as:

frontend:
    phases:
         build:
              commands:
                - npm run build
         preBuild:
              commands:
                 - npm ci
...

From my personal understanding, the container will try to run build first, and next has not been installed yet by preBuild command, thus caused the error. I always need to manually adjust the amplify.yml afterwards.

Reproduction Steps

App amplifyApp = App.Builder.create(this,"id")
                .appName("appName")
                .sourceCodeProvider(GitHubSourceCodeProvider.Builder.create()
                        .owner("owner")
                        .repository("repo")
                        .oauthToken(SecretValue.secretsManager("token"))
                        .build()
                )
                .autoBranchDeletion(true)
                .buildSpec(BuildSpec.fromObjectToYaml(Map.of(
                        "version","1.0",
                        "frontend",Map.of(
                                "phases",Map.of(
                                        "preBuild",Map.of(
                                                "commands", List.of("npm ci")
                                        ),
                                        "build",Map.of(
                                                "commands",List.of("npm run build")
                                        )
                                ),
                                "artifacts",Map.of(
                                        "baseDirectory",".next",
                                        "files", List.of("**/*")
                                ),
                                "cache", Map.of(
                                        "paths",List.of("node_modules/**/*")
                                )
                            )
                        )
                ))
                .build();
                ```
                Then the amplify.yml might be 
                ```
                frontend:
                        build:
                             commands:
                                - npm run build
                        preBuild:
                             commands:
                                - npm install
              ...
                ```

### Possible Solution

_No response_

### Additional Information/Context

_No response_

### CDK CLI Version

2.111.0 (build 2ccb59e)

### Framework Version

_No response_

### Node.js Version

20.10.0

### OS

Macbook M1

### Language

Java

### Language Version

Java (21)

### Other information

_No response_
@thirteendian thirteendian added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Dec 5, 2023
@github-actions github-actions bot added the @aws-cdk/aws-amplify Related to AWS Amplify label Dec 5, 2023
@thirteendian
Copy link
Author

One possible solution for JAVA CDK user is using java.util.LinkedHashMap instead of java.util.Map, for example:

new LinkedHashMap<>() {{
                            put("version", "1.0");
                            put("applications", new LinkedHashMap<>() {{
                                put("appRoot", "frontend");
                                put("frontend", new LinkedHashMap<>() {{
                                    put("buildPath", "frontend");
                                    put("phase", new LinkedHashMap<>() {{
                                        put("preBuild", new LinkedHashMap<>() {{
                                            put("commands", List.of("npm ci")); //clean install
                                        }});
                                        put("build", new LinkedHashMap<>() {{
                                            put("commands", List.of("npm run build")); //set postBuild if needed
                                        }});
                                    }});

This will preserve the order of amplify.yaml

@pahud pahud added p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Dec 5, 2023
@pahud pahud added p3 and removed p2 labels Jun 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-amplify Related to AWS Amplify bug This issue is a bug. effort/medium Medium work item – several days of effort p3
Projects
None yet
Development

No branches or pull requests

2 participants