Skip to content

Commit 1bf2f07

Browse files
authored
feat: add script to ensure playwright/puppeteer version in package.json is in sync with docker tag (#177)
This will be used in the pw/pup templates as part of their dockerfile, allowing to pin the versions in the templates. The script fails only if it finds pw/pup in the package.json as well as base docker image tag, otherwise exits with 0. We might also need a script to update those pinned versions in the templates.
1 parent 752006c commit 1bf2f07

File tree

12 files changed

+186
-6
lines changed

12 files changed

+186
-6
lines changed

node-playwright-camoufox/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ ENV PLAYWRIGHT_BROWSERS_PATH=/home/myuser/pw-browsers
5353
ENV CRAWLEE_SKIP_BROWSER_INSTALL=1
5454

5555
# Copy source code and xvfb script
56-
COPY --chown=myuser:myuser package.json main.js firefox_test.js start_xvfb_and_run_cmd.sh new_xvfb_run_cmd.sh xvfb-entrypoint.sh /home/myuser/
56+
COPY --chown=myuser:myuser package.json main.js check-playwright-version.mjs firefox_test.js start_xvfb_and_run_cmd.sh new_xvfb_run_cmd.sh xvfb-entrypoint.sh /home/myuser/
5757

5858
# Tell Node.js this is a production environemnt
5959
ENV NODE_ENV=production
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { readFileSync } from 'node:fs';
2+
import { join } from 'node:path';
3+
4+
const packageJsonPath = join(import.meta.dirname, 'package.json');
5+
const dockerfilePath = join(import.meta.dirname, 'Dockerfile');
6+
7+
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
8+
const dependencyVersion = packageJson.dependencies?.playwright;
9+
10+
if (!dependencyVersion) {
11+
// no playwright dependency found in package.json
12+
process.exit(0);
13+
}
14+
15+
if (dependencyVersion.match(/^[\^~]/)) {
16+
console.error(`playwright dependency in package.json is not pinned to a specific version. Please pin it to a specific version and use the same version in your Dockerfile base image tag, e.g. ${dependencyVersion.replace(/^[\^~]/, '')}.`);
17+
process.exit(1);
18+
}
19+
20+
const dockerfileContent = readFileSync(dockerfilePath, 'utf8');
21+
const matches = dockerfileContent.match(/FROM\s+.*playwright.*:\d+-(\d+\.\d+\.\d+)/ig);
22+
23+
for (const match of matches) {
24+
const dockerImageVersion = match.match(/FROM\s+.*playwright.*:\d+-(\d+\.\d+\.\d+)/i)?.[1];
25+
26+
if (dockerImageVersion !== dependencyVersion) {
27+
console.error(`playwright version in Dockerfile (${dockerImageVersion}) does not match version in package.json (${dependencyVersion})`);
28+
process.exit(1);
29+
}
30+
}

node-playwright-chrome/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ WORKDIR /home/myuser
5959
ENV PLAYWRIGHT_BROWSERS_PATH=/home/myuser/pw-browsers
6060

6161
# Copy source code and xvfb script
62-
COPY --chown=myuser:myuser package.json main.js chrome_test.js start_xvfb_and_run_cmd.sh new_xvfb_run_cmd.sh xvfb-entrypoint.sh /home/myuser/
62+
COPY --chown=myuser:myuser package.json main.js check-playwright-version.mjs chrome_test.js start_xvfb_and_run_cmd.sh new_xvfb_run_cmd.sh xvfb-entrypoint.sh /home/myuser/
6363

6464
# Sets path to Chrome executable, this is used by Apify.launchPuppeteer()
6565
ENV APIFY_CHROME_EXECUTABLE_PATH=/usr/bin/google-chrome
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { readFileSync } from 'node:fs';
2+
import { join } from 'node:path';
3+
4+
const packageJsonPath = join(import.meta.dirname, 'package.json');
5+
const dockerfilePath = join(import.meta.dirname, 'Dockerfile');
6+
7+
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
8+
const dependencyVersion = packageJson.dependencies?.playwright;
9+
10+
if (!dependencyVersion) {
11+
// no playwright dependency found in package.json
12+
process.exit(0);
13+
}
14+
15+
if (dependencyVersion.match(/^[\^~]/)) {
16+
console.error(`playwright dependency in package.json is not pinned to a specific version. Please pin it to a specific version and use the same version in your Dockerfile base image tag, e.g. ${dependencyVersion.replace(/^[\^~]/, '')}.`);
17+
process.exit(1);
18+
}
19+
20+
const dockerfileContent = readFileSync(dockerfilePath, 'utf8');
21+
const matches = dockerfileContent.match(/FROM\s+.*playwright.*:\d+-(\d+\.\d+\.\d+)/ig);
22+
23+
for (const match of matches) {
24+
const dockerImageVersion = match.match(/FROM\s+.*playwright.*:\d+-(\d+\.\d+\.\d+)/i)?.[1];
25+
26+
if (dockerImageVersion !== dependencyVersion) {
27+
console.error(`playwright version in Dockerfile (${dockerImageVersion}) does not match version in package.json (${dependencyVersion})`);
28+
process.exit(1);
29+
}
30+
}

node-playwright-firefox/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ ENV PLAYWRIGHT_BROWSERS_PATH=/home/myuser/pw-browsers
5353
ENV CRAWLEE_SKIP_BROWSER_INSTALL=1
5454

5555
# Copy source code and xvfb script
56-
COPY --chown=myuser:myuser package.json main.js firefox_test.js start_xvfb_and_run_cmd.sh new_xvfb_run_cmd.sh xvfb-entrypoint.sh /home/myuser/
56+
COPY --chown=myuser:myuser package.json main.js check-playwright-version.mjs firefox_test.js start_xvfb_and_run_cmd.sh new_xvfb_run_cmd.sh xvfb-entrypoint.sh /home/myuser/
5757

5858
# Tell Node.js this is a production environemnt
5959
ENV NODE_ENV=production
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { readFileSync } from 'node:fs';
2+
import { join } from 'node:path';
3+
4+
const packageJsonPath = join(import.meta.dirname, 'package.json');
5+
const dockerfilePath = join(import.meta.dirname, 'Dockerfile');
6+
7+
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
8+
const dependencyVersion = packageJson.dependencies?.playwright;
9+
10+
if (!dependencyVersion) {
11+
// no playwright dependency found in package.json
12+
process.exit(0);
13+
}
14+
15+
if (dependencyVersion.match(/^[\^~]/)) {
16+
console.error(`playwright dependency in package.json is not pinned to a specific version. Please pin it to a specific version and use the same version in your Dockerfile base image tag, e.g. ${dependencyVersion.replace(/^[\^~]/, '')}.`);
17+
process.exit(1);
18+
}
19+
20+
const dockerfileContent = readFileSync(dockerfilePath, 'utf8');
21+
const matches = dockerfileContent.match(/FROM\s+.*playwright.*:\d+-(\d+\.\d+\.\d+)/ig);
22+
23+
for (const match of matches) {
24+
const dockerImageVersion = match.match(/FROM\s+.*playwright.*:\d+-(\d+\.\d+\.\d+)/i)?.[1];
25+
26+
if (dockerImageVersion !== dependencyVersion) {
27+
console.error(`playwright version in Dockerfile (${dockerImageVersion}) does not match version in package.json (${dependencyVersion})`);
28+
process.exit(1);
29+
}
30+
}

node-playwright-webkit/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ ENV PLAYWRIGHT_BROWSERS_PATH=/home/myuser/pw-browsers
5050
ENV CRAWLEE_SKIP_BROWSER_INSTALL=1
5151

5252
# Copy source code and xvfb script
53-
COPY --chown=myuser:myuser package.json main.js webkit_test.js start_xvfb_and_run_cmd.sh new_xvfb_run_cmd.sh xvfb-entrypoint.sh /home/myuser/
53+
COPY --chown=myuser:myuser package.json main.js check-playwright-version.mjs webkit_test.js start_xvfb_and_run_cmd.sh new_xvfb_run_cmd.sh xvfb-entrypoint.sh /home/myuser/
5454

5555
# Tell Node.js this is a production environemnt
5656
ENV NODE_ENV=production
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { readFileSync } from 'node:fs';
2+
import { join } from 'node:path';
3+
4+
const packageJsonPath = join(import.meta.dirname, 'package.json');
5+
const dockerfilePath = join(import.meta.dirname, 'Dockerfile');
6+
7+
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
8+
const dependencyVersion = packageJson.dependencies?.playwright;
9+
10+
if (!dependencyVersion) {
11+
// no playwright dependency found in package.json
12+
process.exit(0);
13+
}
14+
15+
if (dependencyVersion.match(/^[\^~]/)) {
16+
console.error(`playwright dependency in package.json is not pinned to a specific version. Please pin it to a specific version and use the same version in your Dockerfile base image tag, e.g. ${dependencyVersion.replace(/^[\^~]/, '')}.`);
17+
process.exit(1);
18+
}
19+
20+
const dockerfileContent = readFileSync(dockerfilePath, 'utf8');
21+
const matches = dockerfileContent.match(/FROM\s+.*playwright.*:\d+-(\d+\.\d+\.\d+)/ig);
22+
23+
for (const match of matches) {
24+
const dockerImageVersion = match.match(/FROM\s+.*playwright.*:\d+-(\d+\.\d+\.\d+)/i)?.[1];
25+
26+
if (dockerImageVersion !== dependencyVersion) {
27+
console.error(`playwright version in Dockerfile (${dockerImageVersion}) does not match version in package.json (${dependencyVersion})`);
28+
process.exit(1);
29+
}
30+
}

node-playwright/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
6565
ENV CRAWLEE_SKIP_BROWSER_INSTALL=1
6666

6767
# Copy source code and xvfb script
68-
COPY --chown=myuser:myuser package.json main.js chrome_test.js start_xvfb_and_run_cmd.sh new_xvfb_run_cmd.sh xvfb-entrypoint.sh /home/myuser/
68+
COPY --chown=myuser:myuser package.json main.js check-playwright-version.mjs chrome_test.js start_xvfb_and_run_cmd.sh new_xvfb_run_cmd.sh xvfb-entrypoint.sh /home/myuser/
6969

7070
# Sets path to Chrome executable, this is used by Apify.launchPuppeteer()
7171
ENV APIFY_CHROME_EXECUTABLE_PATH=/usr/bin/google-chrome
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { readFileSync } from 'node:fs';
2+
import { join } from 'node:path';
3+
4+
const packageJsonPath = join(import.meta.dirname, 'package.json');
5+
const dockerfilePath = join(import.meta.dirname, 'Dockerfile');
6+
7+
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
8+
const dependencyVersion = packageJson.dependencies?.playwright;
9+
10+
if (!dependencyVersion) {
11+
// no playwright dependency found in package.json
12+
process.exit(0);
13+
}
14+
15+
if (dependencyVersion.match(/^[\^~]/)) {
16+
console.error(`playwright dependency in package.json is not pinned to a specific version. Please pin it to a specific version and use the same version in your Dockerfile base image tag, e.g. ${dependencyVersion.replace(/^[\^~]/, '')}.`);
17+
process.exit(1);
18+
}
19+
20+
const dockerfileContent = readFileSync(dockerfilePath, 'utf8');
21+
const matches = dockerfileContent.match(/FROM\s+.*playwright.*:\d+-(\d+\.\d+\.\d+)/ig);
22+
23+
for (const match of matches) {
24+
const dockerImageVersion = match.match(/FROM\s+.*playwright.*:\d+-(\d+\.\d+\.\d+)/i)?.[1];
25+
26+
if (dockerImageVersion !== dependencyVersion) {
27+
console.error(`playwright version in Dockerfile (${dockerImageVersion}) does not match version in package.json (${dependencyVersion})`);
28+
process.exit(1);
29+
}
30+
}

0 commit comments

Comments
 (0)