pulumi / pulumi-docker-build

A Pulumi native provider for Docker
Apache License 2.0
6 stars 2 forks source link

Pulumi.DockerBuild.Image fail to preview when trying to create ECR repo and image same time #251

Open guitarrapc opened 2 months ago

guitarrapc commented 2 months ago

What happened?

Pulumi.DockerBuild.Image shows an error on pulumi preview when the ECR repository has not yet been created, but it does create the repository at the same time the docker build is performed. However, pulumi preview succeeds if the ECR repository already exists. Since Pulumi.Docker.Image can preview even when the ECR and Docker are created at the same time, is this by design, or am I missing something?

Error meesage.

  docker-build:index:Image (dummy-image):
    error: docker-build:index:Image resource 'dummy-image': property exports[0] value {<nil>} has a problem: at least one tag or export name is needed when pushing to a registry

Example

pulumi preview failure sample.

var ecr = new Pulumi.Aws.Ecr.Repository($"dummy-repo", new()
{
    ForceDelete = true,
});
var authToken = Pulumi.Aws.Ecr.GetAuthorizationToken.Invoke(new()
{
    RegistryId = ecr.RegistryId,
});
var image = new Pulumi.DockerBuild.Image($"dummy-image", new()
{
    Dockerfile = new Pulumi.DockerBuild.Inputs.DockerfileArgs
    {
        Inline = """
        FROM envoy
        """,
    },
    Platforms = [Pulumi.DockerBuild.Platform.Linux_amd64, Pulumi.DockerBuild.Platform.Linux_arm64],
    Push = true,
    Registries = [
        new Pulumi.DockerBuild.Inputs.RegistryArgs
        {
            Address = ecr.RepositoryUrl,
            Username = authToken.Apply(x => x.UserName),
            Password = authToken.Apply(x => x.Password)
        }
    ],
    Tags = [ ecr.RepositoryUrl.Apply(x => $"{x}:latest"), ], // `pulumi preview` fail
});

pulumi preview success sample, just changed Tags not use Pulumi.Ouput.Apply and change to literal.

var image = new Pulumi.DockerBuild.Image($"dummy-image", new()
{
    Dockerfile = new Pulumi.DockerBuild.Inputs.DockerfileArgs
    {
        Inline = """
        FROM envoy
        """,
    },
    Platforms = [Pulumi.DockerBuild.Platform.Linux_amd64, Pulumi.DockerBuild.Platform.Linux_arm64],
    Push = true,
    Registries = [
        new Pulumi.DockerBuild.Inputs.RegistryArgs
        {
            Address = ecr.RepositoryUrl,
            Username = authToken.Apply(x => x.UserName),
            Password = authToken.Apply(x => x.Password)
        }
    ],
    Tags = ["0123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/dummy-repo-abcdef:latest"], // change to literal
});

Output of pulumi about

CLI
Version      3.131.0
Go Version   go1.23.0
Go Compiler  gc

Plugins
KIND      NAME          VERSION
resource  aws           6.51.0
resource  aws-native    0.119.0
resource  azure-native  2.57.0
resource  docker-build  0.0.6
language  dotnet        unknown

Host
OS       Microsoft Windows 11 Pro
Version  10.0.22631 Build 22631
Arch     x86_64

This project is written in dotnet: executable='C:\Program Files\dotnet\dotnet.exe' version='8.0.400'

Dependencies:
NAME                VERSION
Pulumi              3.66.2
Pulumi.Aws          6.51.0
Pulumi.AwsNative    0.119.0
Pulumi.DockerBuild  0.0.6

Additional context

Example code is logically same as official api-docs https://www.pulumi.com/registry/packages/docker-build/api-docs/image/ do.

Contributing

Vote on this issue by adding a 👍 reaction. To contribute a fix for this issue, leave a comment (and link to your pull request, if you've opened one already).

guitarrapc commented 4 weeks ago

Workaround

set Push = false for pulumi preview, and Push = true for pulumi up. This can be archived by Deployment.Instance.IsDryRun.

// before
var image = new Pulumi.DockerBuild.Image($"dummy-image", new()
{
    Push = true, // cause error when ECR is not yet created on preview.
    // any other parameters
};

// after
var image = new Pulumi.DockerBuild.Image($"dummy-image", new()
{
    Push = !Deployment.Instance.IsDryRun, // Deployment.Instance.IsDryRun will be true on `pulumi preview` = true, false on `pulumi up`.
    // any other parameters
};
guitarrapc commented 4 weeks ago

Seems relate to https://github.com/pulumi/pulumi-docker-build/issues/252. If this is by design, please close this issue.