-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update distroless nodejs image (#1224)
* use nodejs18-debian11 image * Add changeset * patch dockerfile automatically * add changeset * Tweak changesets --------- Co-authored-by: Ryan Ling <[email protected]>
- Loading branch information
Showing
8 changed files
with
102 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'skuba': patch | ||
--- | ||
|
||
template/\*-rest-api: Switch distroless image from `nodejs:18` to `nodejs18-debian11` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'skuba': minor | ||
--- | ||
|
||
format, lint: Switch distroless image from `nodejs` to `nodejs-debian11` | ||
|
||
`skuba format` and `skuba lint` will now automatically switch your `gcr.io/distroless/nodejs:18` image to `gcr.io/distroless/nodejs18-debian11`. This is now the [recommended](https://github.com/GoogleContainerTools/distroless/blob/main/nodejs/README.md) base image for Node.js. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import memfs, { vol } from 'memfs'; | ||
|
||
import { tryPatchDockerfile } from './patchDockerfile'; | ||
|
||
jest.mock('fs-extra', () => memfs); | ||
|
||
const volToJson = () => vol.toJSON(process.cwd(), undefined, true); | ||
|
||
beforeEach(jest.clearAllMocks); | ||
beforeEach(() => vol.reset()); | ||
|
||
const dockerfile = ` | ||
ARG BASE_IMAGE | ||
ARG BASE_TAG | ||
FROM --platform=\${BUILDPLATFORM:-<%- platformName %>} gcr.io/distroless/nodejs:18 AS runtime | ||
WORKDIR /workdir | ||
`; | ||
|
||
it('patches an existing Dockerfile', async () => { | ||
vol.fromJSON({ Dockerfile: dockerfile }); | ||
|
||
await expect(tryPatchDockerfile()).resolves.toBeUndefined(); | ||
|
||
expect(volToJson()).toMatchInlineSnapshot(` | ||
{ | ||
"Dockerfile": " | ||
ARG BASE_IMAGE | ||
ARG BASE_TAG | ||
FROM --platform=\${BUILDPLATFORM:-<%- platformName %>} gcr.io/distroless/nodejs18-debian11 AS runtime | ||
WORKDIR /workdir | ||
", | ||
} | ||
`); | ||
}); | ||
|
||
it('ignores when a Dockerfile is missing', async () => { | ||
vol.fromJSON({}); | ||
|
||
await expect(tryPatchDockerfile()).resolves.toBeUndefined(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { inspect } from 'util'; | ||
|
||
import fs from 'fs-extra'; | ||
|
||
import { log } from '../../utils/logging'; | ||
|
||
import { createDestinationFileReader } from './analysis/project'; | ||
|
||
const DOCKERFILE_FILENAME = 'Dockerfile'; | ||
|
||
const VERSION_REGEX = /gcr.io\/distroless\/nodejs:(16|18|20)/g; | ||
const VERSION_DEBIAN_REPLACE = 'gcr.io/distroless/nodejs$1-debian11'; | ||
|
||
const patchDockerfile = async (dir: string) => { | ||
const readFile = createDestinationFileReader(dir); | ||
|
||
const maybeDockerfile = await readFile(DOCKERFILE_FILENAME); | ||
|
||
if (!maybeDockerfile) { | ||
return; | ||
} | ||
|
||
const patched = maybeDockerfile.replaceAll( | ||
VERSION_REGEX, | ||
VERSION_DEBIAN_REPLACE, | ||
); | ||
|
||
await fs.promises.writeFile(DOCKERFILE_FILENAME, patched); | ||
}; | ||
|
||
export const tryPatchDockerfile = async (dir = process.cwd()) => { | ||
try { | ||
await patchDockerfile(dir); | ||
} catch (err) { | ||
log.warn('Failed to patch Dockerfile.'); | ||
log.subtle(inspect(err)); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters