forked from cypress-io/cypress-docker-images
-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate-included-image.js
156 lines (126 loc) · 4.74 KB
/
generate-included-image.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// creates new folder included/<Cypress version> with Dockerfile and README file
const path = require('path')
const fs = require('fs')
const shelljs = require('shelljs')
const {isStrictSemver} = require('./utils')
const versionTag = process.argv[2]
const baseImageTag = process.argv[3]
if (!versionTag || !isStrictSemver(versionTag)) {
console.error('expected Cypress version argument like "3.8.3"')
process.exit(1)
}
if (!baseImageTag) {
console.error('expected base Docker image tag like "cypress/browsers:node12.6.0-chrome77"')
process.exit(1)
}
if (!baseImageTag.startsWith('cypress/browsers:')) {
console.error('expected the base Docker image tag to be one of "cypress/browsers:*"')
console.error('but it was "%s"', baseImageTag)
process.exit(1)
}
const outputFolder = path.join('included', versionTag)
if (shelljs.test('-d', outputFolder)) {
console.log('removing existing folder "%s"', outputFolder)
shelljs.rm('-rf', outputFolder)
}
console.log('creating "%s"', outputFolder)
shelljs.mkdir(outputFolder)
const Dockerfile = `
# WARNING: this file was autogenerated by ${path.basename(__filename)}
# using
# npm run add:included -- ${versionTag} ${baseImageTag}
#
# build this image with command
# docker build -t cypress/included:${versionTag} .
#
FROM ${baseImageTag}
# Update the dependencies to get the latest and greatest (and safest!) packages.
RUN apt update && apt upgrade -y
# avoid too many progress messages
# https://github.com/cypress-io/cypress/issues/1243
ENV CI=1
# disable shared memory X11 affecting Cypress v4 and Chrome
# https://github.com/cypress-io/cypress-docker-images/issues/270
ENV QT_X11_NO_MITSHM=1
ENV _X11_NO_MITSHM=1
ENV _MITSHM=0
# should be root user
RUN echo "whoami: $(whoami)"
RUN npm config -g set user $(whoami)
# command "id" should print:
# uid=0(root) gid=0(root) groups=0(root)
# which means the current user is root
RUN id
# point Cypress at the /root/cache no matter what user account is used
# see https://on.cypress.io/caching
ENV CYPRESS_CACHE_FOLDER=/root/.cache/Cypress
RUN npm install -g "cypress@${versionTag}"
RUN cypress verify
# Cypress cache and installed version
# should be in the root user's home folder
RUN cypress cache path
RUN cypress cache list
RUN cypress info
RUN cypress version
# give every user read access to the "/root" folder where the binary is cached
# we really only need to worry about the top folder, fortunately
RUN ls -la /root
RUN chmod 755 /root
# always grab the latest Yarn
# otherwise the base image might have old versions
# NPM does not need to be installed as it is already included with Node.
RUN npm i -g yarn@latest
# Show where Node loads required modules from
RUN node -p 'module.paths'
# should print Cypress version
# plus Electron and bundled Node versions
RUN cypress version
RUN echo " node version: $(node -v) \\n" \\
"npm version: $(npm -v) \\n" \\
"yarn version: $(yarn -v) \\n" \\
"debian version: $(cat /etc/debian_version) \\n" \\
"user: $(whoami) \\n" \\
"chrome: $(google-chrome --version || true) \\n" \\
"firefox: $(firefox --version || true) \\n"
ENTRYPOINT ["cypress", "run"]
`
const dockerFilename = path.join(outputFolder, 'Dockerfile')
fs.writeFileSync(dockerFilename, Dockerfile.trim() + '\n', 'utf8')
console.log('Saved %s', dockerFilename)
const README = `
<!--
WARNING: this file was autogenerated by ${path.basename(__filename)} using
npm run add:included -- ${versionTag} ${baseImageTag}
-->
# cypress/included:${versionTag}
Read [Run Cypress with a single Docker command][blog post url]
## Run tests
\`\`\`shell
$ docker run -it -v $PWD:/e2e -w /e2e cypress/included:${versionTag}
# runs Cypress tests from the current folder
\`\`\`
[blog post url]: https://www.cypress.io/blog/2019/05/02/run-cypress-with-a-single-docker-command/
`
const readmeFilename = path.join(outputFolder, 'README.md')
fs.writeFileSync(readmeFilename, README.trim() + '\n', 'utf8')
console.log('Saved %s', readmeFilename)
// to make building images simpler and to follow the same pattern as previous builds
const buildScript = `
# WARNING: this file was autogenerated by ${path.basename(__filename)}
# using
# npm run add:included -- ${versionTag} ${baseImageTag}
set e+x
LOCAL_NAME=cypress/included:${versionTag}
echo "Building $LOCAL_NAME"
docker build -t $LOCAL_NAME .
`
const buildFilename = path.join(outputFolder, 'build.sh')
fs.writeFileSync(buildFilename, buildScript.trim() + '\n', 'utf8')
shelljs.chmod('a+x', buildFilename)
console.log('Saved %s', buildFilename)
console.log(`
Please add the newly generated folder ${outputFolder} to Git and update CircleCI file with
npm run build
Build the Docker container locally to make sure it is correct and update "included/README.md" list
of images with the new image information.
`)