aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.67k stars 3.92k forks source link

(assets): localTagName assigned to docker image executable #17969

Closed TheFlexican closed 2 years ago

TheFlexican commented 2 years ago

What is the problem?

Using cdk pipelines to deploy ecs cluster, with a docker from tarball image. During the Assets codebuild stage I received the error: Error parsing reference: "" is not a valid repository/tag: invalid reference format After going over my code a couple of times, I figured it had to be something else, and started looking at the source code of cdk-assets.

Found the following which too me doesn't really make sense. In packages/cdk-assets/lib/private/handlers/container-images.ts

        const localTagName = this.asset.source.executable
            ? await this.buildExternalAsset(this.asset.source.executable)
            : await this.buildDirectoryAsset();
        if (localTagName === undefined || this.host.aborted) {
            return;
        }

        this.host.emitMessage(progress_1.EventType.UPLOAD, `Push ${imageUri}`);
        if (this.host.aborted) {
            return;
        }

        await this.docker.tag(localTagName, imageUri);
        if (cdkDockerCredentialsConfigured) {
            this.docker.resetAuthPlugins();
            await this.docker.login(ecr);
        }
        await this.docker.push(imageUri);

the localTagName is assigned to the actual command to build the docker image in the assets file. Asset file snippet below:

  "dockerImages": {
    "d380dd02f772a84bdb81b656231f28d7720fa6a460c12c41a717223b820d4358": {
      "source": {
        "executable": [
          "sh",
          "-c",
          "docker load -i asset.d380dd02f772a84bdb81b656231f28d7720fa6a460c12c41a717223b820d4358.tar | sed \"s/Loaded image: //g\""
        ]
      },

To my understanding docker is trying to tag the image in ecr with the value of the executable param, which obviously fails.

Reproduction Steps

Setup a CDK pipeline to deploy ECS cluster with a docker image from tarball. Pipeline should create an Assets codebuild stage which will fail with the above mentioned error.

What did you expect to happen?

Successful deployment of ECS cluster

What actually happened?

Failed pipeline

CDK CLI Version

2.1.0 (build f4f18b1)

Framework Version

No response

Node.js Version

v16.3.0

OS

Ubuntu

Language

Typescript

Language Version

No response

Other information

No response

TheFlexican commented 2 years ago

Looking more into it, I think I'm mistaken for what it actually happening here.Have to admit, I'm just a beginner as a coder, so I tried to understand what's going on. My mistake. What i realized is that localTagName variable is assigned to the output of the command that loads the docker image. Somehow the output of the docker load command is not assigned to the localTagName variable.

Ill dig into that deeper and will close this issue.

github-actions[bot] commented 2 years ago

⚠️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.

cacerca1 commented 2 years ago

Did you ever figure out how to fix this? I'm running into the same thing now.

TheFlexican commented 2 years ago

Did you ever figure out how to fix this? I'm running into the same thing now.

Hi, well I have a workaround for it. You need to edit the buildspec for the docker asset in your pipeline and change the path in the assets file. Below is a sample of my buildspec file. Just look at your current command and copy the assembly-something/something.assets.json and place a line above it with sed just like below.

version: '0.2' phases: install: commands:

I have an issue open for this here https://github.com/aws/aws-cdk/issues/18044