Skip to content

Commit

Permalink
Add test for ECR getCredentials function
Browse files Browse the repository at this point in the history
  • Loading branch information
flostadler committed Oct 29, 2024
1 parent 82da9b2 commit 25505b9
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/ecr-image/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: ecr-image
runtime: nodejs
description: Publish docker images to ECR
41 changes: 41 additions & 0 deletions examples/ecr-image/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# examples/ecr-image

This example demonstrates how to use Pulumi to publish a Docker image to Amazon Elastic Container Registry (ECR).

## Steps

1. **Install Dependencies**

Ensure you have the necessary dependencies installed:

```sh
npm install
```

2. **Run Pulumi Up**

Run Pulumi to create the ECR repository and push the Docker image:

```sh
pulumi up
```

Confirm the changes and wait for the process to complete.

## Files

- `index.ts`: Contains the Pulumi program to create the ECR repository and push the Docker image.
- `Dockerfile`: Dockerfile for building the Docker image.

## Clean Up

To clean up the resources created by Pulumi:

```sh
pulumi destroy
```

## Additional Resources

- [Pulumi AWS Documentation](https://www.pulumi.com/docs/intro/cloud-providers/aws/)
- [Amazon ECR Documentation](https://docs.aws.amazon.com/AmazonECR/latest/userguide/what-is-ecr.html)
3 changes: 3 additions & 0 deletions examples/ecr-image/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM public.ecr.aws/nginx/nginx
RUN echo "<h1>Hello Pulumi!</h1>" > \
/usr/share/nginx/html/index.html
36 changes: 36 additions & 0 deletions examples/ecr-image/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as docker from "@pulumi/docker-build";

const config = new pulumi.Config("aws");
const providerOpts = { provider: new aws.Provider("prov", { region: <aws.Region>config.require("envRegion") }) };

const repository = new aws.ecr.Repository("myrepository", {
forceDelete: true,
}, providerOpts);

// Get registry info (credentials and endpoint) so we can publish to it.
const credentials = aws.ecr.getCredentialsOutput({ registryId: repository.registryId }, providerOpts);
const decodedCredentials = credentials.authorizationToken.apply(tok => Buffer.from(tok, "base64").toString());
const registryInfo = decodedCredentials.apply(creds => {
const [username, password] = creds.split(":");
if (!password || !username) {
throw new Error("Invalid credentials");
}
return {
address: credentials.proxyEndpoint,
username: username,
password: password,
};
});

const image = new docker.Image("myimage", {
push: true,
tags: [pulumi.interpolate`${repository.repositoryUrl}:latest`],
context: {
location: "./app",
},
registries: [registryInfo],
});

export const digest = image.digest;
16 changes: 16 additions & 0 deletions examples/ecr-image/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "ecr-image",
"version": "0.0.1",
"license": "Apache-2.0",
"scripts": {
"build": "tsc"
},
"dependencies": {
"@pulumi/pulumi": "^3.0.0",
"@pulumi/aws": "^6.0.0",
"@pulumi/docker-build": "^0.0.7"
},
"devDependencies": {
"@types/node": "^8.0.0"
}
}
18 changes: 18 additions & 0 deletions examples/ecr-image/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"strict": true,
"outDir": "bin",
"target": "es2016",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.ts"
]
}
14 changes: 14 additions & 0 deletions examples/examples_nodejs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,3 +716,17 @@ func TestServerlessAppRepositoryApplication(t *testing.T) {

integration.ProgramTest(t, &test)
}

func TestAccEcrImage(t *testing.T) {
test := getJSBaseOptions(t).
With(integration.ProgramTestOptions{
Dir: filepath.Join(getCwd(t), "ecr-image"),
ExtraRuntimeValidation: func(t *testing.T, stack integration.RuntimeValidationStackInfo) {
repoDigest, ok := stack.Outputs["digest"].(string)
assert.True(t, ok, "expected digest output to be set")
assert.NotEmpty(t, repoDigest)
},
})

integration.ProgramTest(t, &test)
}

0 comments on commit 25505b9

Please sign in to comment.