How can one check if the stack or resource has alr...
# getting-started
m
How can one check if the stack or resource has already been deployed? I'm trying to work around this bug: https://github.com/pulumi/pulumi-docker/issues/676 and the behavior I'm attempting to achieve is that if the stack has been deployed / ECR repo already exists, I want to run:
Copy code
const pmbMainDockerImage = new docker.Image("pmb-main", {
        build: stackExists ? {
            args: {
                BUILDKIT_INLINE_CACHE: "1"
            },
            platform: "linux/amd64",
            builderVersion: "BuilderBuildKit",
            cacheFrom: {
                images: [pulumi.interpolate`${pmbMainEcr.url}:latest`],
            },
            context: "../../",
            dockerfile: "../../.cicd/imageFiles/Dockerfile",
        } : {
            platform: "linux/amd64",
            builderVersion: "BuilderBuildKit",
            context: "../../",
            dockerfile: "../../.cicd/imageFiles/Dockerfile",
        },
to enable build cache/acceleration if it will not crash.
The following does work:
Copy code
const stackRef = new pulumi.StackReference(`pmb/${getProject()}/${getStack()}`);
const stackOutput = stackRef.getOutput('pmbMainDockerImageFull');
let pmbMainDockerImage: docker.Image = {} as docker.Image;
stackOutput.apply(stackOutput => {
    pmbMainDockerImage = new docker.Image("pmb-main", {
        build: stackOutput ? {
but results in:
Copy code
warning: Undefined value (pmbMainDockerImageURI) will not show as a stack output.
    warning: Undefined value (pmbMainDockerImageFull) will not show as a stack output.
Found a workaround! It's rather painful, but it works:
Copy code
const stackRef = new pulumi.StackReference(`pmb/${getProject()}/${getStack()}`);
const stackOutput = stackRef.getOutput('pmbMainDockerImageFull');

const pmbMainDockerImage = new docker.Image("pmb-main", {
    build: stackOutput.apply(x => {
        console.log(`stackExists = ${!!x}`);
        return (!!x) ? {
            args: {
                BUILDKIT_INLINE_CACHE: "1"
            },
            platform: "linux/amd64",
            builderVersion: "BuilderBuildKit",
            cacheFrom: {
                images: [pulumi.interpolate`${pmbMainEcr.url}:latest`],
            },
            context: "../../",
            dockerfile: "../../.cicd/imageFiles/Dockerfile",
        }  : {
            platform: "linux/amd64",
            builderVersion: "BuilderBuildKit",
            context: "../../",
            dockerfile: "../../.cicd/imageFiles/Dockerfile",
        } 
    }) as pulumi.Input<docker.types.input.DockerBuild>,
    imageName: pulumi.interpolate`${pmbMainEcr.url}:latest`,
    registry: {
        password: pulumi.secret(aws.ecr.getAuthorizationTokenOutput({
            registryId: pmbMainEcr.repository.registryId,
        }).apply(authToken => authToken.password)),
        server: pmbMainEcr.url,
    },
    skipPush: skipDockerImagePush
});