Skip to content

Commit

Permalink
Merge branch 'master' into skip-autofix
Browse files Browse the repository at this point in the history
  • Loading branch information
samchungy authored Jul 25, 2023
2 parents ac6d866 + 884f3f6 commit 649b06f
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-swans-serve.md
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`
5 changes: 5 additions & 0 deletions .changeset/thirty-bananas-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'skuba': minor
---

format, lint: Add `pnpm-lock.yaml` to `.prettierignore`
7 changes: 7 additions & 0 deletions .changeset/wild-plums-join.md
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.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/.gantry/**/*.yml
gantry*.yaml
gantry*.yml
pnpm-lock.yaml
# end managed by skuba

/integration/base/
8 changes: 4 additions & 4 deletions docs/deep-dives/arm64.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ Replace the relevant `--platform` values in your Dockerfile(s),
then ensure that you run your builds on AMD64 hardware:

```diff
- FROM --platform=arm64 gcr.io/distroless/nodejs:18 AS runtime
+ FROM --platform=${BUILDPLATFORM:-amd64} gcr.io/distroless/nodejs:18 AS runtime
- FROM --platform=arm64 gcr.io/distroless/nodejs18-debian11 AS runtime
+ FROM --platform=${BUILDPLATFORM:-amd64} gcr.io/distroless/nodejs18-debian11 AS runtime
- FROM --platform=${BUILDPLATFORM:-arm64} gcr.io/distroless/nodejs:18 AS runtime
+ FROM --platform=${BUILDPLATFORM:-amd64} gcr.io/distroless/nodejs:18 AS runtime
- FROM --platform=${BUILDPLATFORM:-arm64} gcr.io/distroless/nodejs18-debian11 AS runtime
+ FROM --platform=${BUILDPLATFORM:-amd64} gcr.io/distroless/nodejs18-debian11 AS runtime
```

For a [Gantry] service, modify the `cpuArchitecture` property in your `gantry.build.yml` and `gantry.apply.yml` resource files:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ yarn-error.log
/.gantry/**/*.yml
gantry*.yaml
gantry*.yml
pnpm-lock.yaml
# end managed by skuba
",
"operation": "A",
Expand Down
44 changes: 44 additions & 0 deletions src/cli/configure/patchDockerfile.test.ts
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();
});
38 changes: 38 additions & 0 deletions src/cli/configure/patchDockerfile.ts
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));
}
};
2 changes: 2 additions & 0 deletions src/cli/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { createLogger, log } from '../utils/logging';
import { runESLint } from './adapter/eslint';
import { runPrettier } from './adapter/prettier';
import { tryAddEmptyExports } from './configure/addEmptyExports';
import { tryPatchDockerfile } from './configure/patchDockerfile';
import { tryPatchRenovateConfig } from './configure/patchRenovateConfig';
import { tryPatchServerListener } from './configure/patchServerListener';
import { tryRefreshIgnoreFiles } from './configure/refreshIgnoreFiles';
Expand All @@ -14,6 +15,7 @@ export const format = async (args = process.argv.slice(2)): Promise<void> => {
await Promise.all([
tryAddEmptyExports(),
tryPatchRenovateConfig(),
tryPatchDockerfile(),
tryPatchServerListener(),
tryRefreshIgnoreFiles(),
]);
Expand Down
1 change: 1 addition & 0 deletions template/base/_.prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
/.gantry/**/*.yml
gantry*.yaml
gantry*.yml
pnpm-lock.yaml
# end managed by skuba
2 changes: 1 addition & 1 deletion template/express-rest-api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ RUN yarn build

###

FROM --platform=${BUILDPLATFORM:-<%- platformName %>} gcr.io/distroless/nodejs:18 AS runtime
FROM --platform=${BUILDPLATFORM:-<%- platformName %>} gcr.io/distroless/nodejs18-debian11 AS runtime

WORKDIR /workdir

Expand Down
2 changes: 1 addition & 1 deletion template/koa-rest-api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ RUN yarn build

###

FROM --platform=${BUILDPLATFORM:-<%- platformName %>} gcr.io/distroless/nodejs:18 AS runtime
FROM --platform=${BUILDPLATFORM:-<%- platformName %>} gcr.io/distroless/nodejs18-debian11 AS runtime

WORKDIR /workdir

Expand Down

0 comments on commit 649b06f

Please sign in to comment.