Skip to content

Commit

Permalink
feat(ecr-assets): Support .dockerignore (faster Docker builds) (#4104)
Browse files Browse the repository at this point in the history
* Support .dockerignore (faster Docker builds)

* ts fix

* test update and lint fix

* exclude dockerignore so it does not influence hashing

* tweaks

* remove verify from other test

* revert

* more revert
  • Loading branch information
parisholley authored and mergify[bot] committed Sep 22, 2019
1 parent 91cda9d commit 8389eeb
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/@aws-cdk/aws-ecr-assets/lib/image-asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,17 @@ export class DockerImageAsset extends cdk.Construct implements assets.IAsset {
throw new Error(`No 'Dockerfile' found in ${dir}`);
}

let exclude: string[] = ['.dockerignore'];

const ignore = path.join(dir, '.dockerignore');

if (fs.existsSync(ignore)) {
exclude = [...exclude, ...fs.readFileSync(ignore).toString().split('\n').filter(e => !!e)];
}

const staging = new assets.Staging(this, 'Staging', {
...props,
exclude,
sourcePath: dir
});

Expand Down Expand Up @@ -126,7 +135,7 @@ export class DockerImageAsset extends cdk.Construct implements assets.IAsset {
}

function validateBuildArgs(buildArgs?: { [key: string]: string }) {
for (const [ key, value ] of Object.entries(buildArgs || {})) {
for (const [key, value] of Object.entries(buildArgs || {})) {
if (Token.isUnresolved(key) || Token.isUnresolved(value)) {
throw new Error(`Cannot use tokens in keys or values of "buildArgs" since they are needed before deployment`);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foobar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM python:3.6
EXPOSE 8000
WORKDIR /src
ADD . /src
CMD python3 index.py
Empty file.
33 changes: 33 additions & 0 deletions packages/@aws-cdk/aws-ecr-assets/test/dockerignore-image/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/python
import sys
import textwrap
import http.server
import socketserver

PORT = 8000


class Handler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
self.wfile.write(textwrap.dedent('''\
<!doctype html>
<html><head><title>It works</title></head>
<body>
<h1>Hello from the integ test container</h1>
<p>This container got built and started as part of the integ test.</p>
<img src="https://media.giphy.com/media/nFjDu1LjEADh6/giphy.gif">
</body>
''').encode('utf-8'))


def main():
httpd = http.server.HTTPServer(("", PORT), Handler)
print("serving at port", PORT)
httpd.serve_forever()


if __name__ == '__main__':
main()
17 changes: 17 additions & 0 deletions packages/@aws-cdk/aws-ecr-assets/test/test.image-asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,23 @@ export = {
test.done();
},

'docker directory is staged without files specified in .dockerignore'(test: Test) {
const app = new App();
const stack = new Stack(app, 'stack');

new DockerImageAsset(stack, 'MyAsset', {
directory: path.join(__dirname, 'dockerignore-image')
});

const session = app.synth();

test.ok(fs.existsSync(path.join(session.directory, `asset.1a17a141505ac69144931fe263d130f4612251caa4bbbdaf68a44ed0f405439c/Dockerfile`)));
test.ok(fs.existsSync(path.join(session.directory, 'asset.1a17a141505ac69144931fe263d130f4612251caa4bbbdaf68a44ed0f405439c/index.py')));
test.ok(!fs.existsSync(path.join(session.directory, 'asset.1a17a141505ac69144931fe263d130f4612251caa4bbbdaf68a44ed0f405439c/foobar.txt')));

test.done();
},

'fails if using tokens in build args keys or values'(test: Test) {
// GIVEN
const stack = new Stack();
Expand Down

0 comments on commit 8389eeb

Please sign in to comment.