How to set repository name and tag on DockerImageAsset of CDK?
This is my code: (groovy)
private uploadImage(String name, File directory, File jarFile) {
    def asset = DockerImageAsset.Builder.create(scope, cdkId("$name-image"))
            .directory(directory.toString())
            .buildArgs([
                    "jarFile"    : jarFile.name,
                    "environment": config.environment
            ])
            .build()
    println "ImageURL: $asset.imageUri"
}
This is the image url printed:
ImageURL: 9999999999999.dkr.ecr.us-west-2.${Token[AWS.URLSuffix.1]}/aws-cdk/assets:89ae89e0b3f7652a4ac70a4e18d6b2acec2abff96920e81e6487da58c8b820f3
I guess this is meant to be this way for CI/CD, where it doesn't matter the repository name/tag.
But if you need to use this image outside of CI/CD environment, it turns into a mess as we have many different projects and versions in the same repository (aws-cdk/assets).
I see a method called "repositoryName" but it is deprecated and I couldn't find an alternative or an explanation of why it is deprecated.
This isnt a direct answer to your question, as this solution uses NodeJS rather than Groovy, but it may help others that end up here.
You could check out the cdk-ecr-deployment library in the cdklabs repository, which gives a construct allowing you to deploy docker images to ECR.
This is done in three steps:
const repo = new ecr.Repository(this, 'NginxRepo', {
  repositoryName: 'nginx',
  removalPolicy: RemovalPolicy.DESTROY,
});
const image = new DockerImageAsset(this, 'CDKDockerImage', {
  directory: path.join(__dirname, 'docker'),
});
new ecrDeploy.ECRDeployment(this, 'DeployDockerImage', {
  src: new ecrDeploy.DockerImageName(image.imageUri),
  dest: new ecrDeploy.DockerImageName(`${repo.repositoryUri}:latest`),
});
See the example for more context on how this is used.
From CDK Docs :
DockerImageAsset is designed for seamless build & consumption of image assets by CDK code deployed to multiple environments through the CDK CLI or through CI/CD workflows. To that end, the ECR repository behind this construct is controlled by the AWS CDK. The mechanics of where these images are published and how are intentionally kept as an implementation detail, and the construct does not support customizations such as specifying the ECR repository name or tags.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With