|
1 | | -import {ListTagsResponse, NodeVersion, PhpVersion, WebServerType} from './model'; |
2 | | -import compareVersions from 'compare-versions'; |
3 | | -import Docker from 'dockerode'; |
4 | | -import axios from 'axios'; |
5 | | -import {spawn} from 'child_process'; |
6 | | -import {getInput} from "@actions/core"; |
7 | | -import {context, getOctokit} from "@actions/github"; |
8 | | -// @ts-ignore |
9 | | -import {createClientV2} from 'docker-registry-client'; |
10 | | - |
11 | | -(async () => { |
12 | | - try { |
13 | | - const phpRegex = new RegExp(process.argv[2] || '^([89]\\.\\d)(\\.\\d+)?-(apache|fpm)$'); |
14 | | - const client = createClientV2({'name': 'php'}); |
15 | | - const phpVersions: PhpVersion[] = (await getTags(client)).tags.map(it => { |
16 | | - const match = phpRegex.exec(it); |
17 | | - return { |
18 | | - tag: it, |
19 | | - version: match && match[1], |
20 | | - exactVersion: match && (match[1] + (match[2] || '')), |
21 | | - webServer: it.includes('-fpm') ? WebServerType.NGINX : WebServerType.APACHE, |
22 | | - } as PhpVersion; |
23 | | - }).filter(it => it.version); |
24 | | - const highestPhpVersion = phpVersions.reduce((a, b) => { |
25 | | - return compareVersions(a.exactVersion, b.exactVersion) === -1 ? b : a; |
26 | | - }); |
27 | | - |
28 | | - const nodeVersions = await getNodeLtsVersions(); |
29 | | - |
30 | | - const docker = new Docker(); |
31 | | - |
32 | | - const imageTags = await buildImages(docker, highestPhpVersion, nodeVersions) |
33 | | - await createOrUpdateRelease(highestPhpVersion, imageTags) |
34 | | - } catch (e) { |
35 | | - console.error('Build failed: ', e); |
36 | | - process.exit(typeof e == 'number' ? e : 1); |
37 | | - } |
38 | | -})(); |
39 | | - |
40 | | -async function createOrUpdateRelease(phpVersion: PhpVersion, imageTags: string[]) { |
41 | | - const githubToken = getInput("token", {required: false}); |
42 | | - if (githubToken) { |
43 | | - const octokit = getOctokit(githubToken); |
44 | | - const { owner, repo } = context.repo; |
45 | | - |
46 | | - const target_commitish = getInput('commitish', { required: false }) || context.sha; |
47 | | - const tag = `php${phpVersion.version}` |
48 | | - const existingRelease = await octokit.rest.repos.getReleaseByTag({owner, repo, tag}).then(response => response.data).catch(err => { |
49 | | - if (err?.status === 404) { |
50 | | - return null |
51 | | - } |
52 | | - throw err; |
53 | | - }) |
54 | | - |
55 | | - const body = `Available tags for \`${tag}\`:\n\n` + imageTags.join('\n') |
56 | | - |
57 | | - if (existingRelease === null) { |
58 | | - octokit.rest.repos.createRelease({ |
59 | | - owner, |
60 | | - repo, |
61 | | - tag_name: tag, |
62 | | - target_commitish, |
63 | | - name: tag, |
64 | | - body |
65 | | - }) |
66 | | - } else { |
67 | | - octokit.rest.repos.updateRelease({ |
68 | | - owner, |
69 | | - repo, |
70 | | - tag_name: tag, |
71 | | - target_commitish, |
72 | | - name: tag, |
73 | | - body, |
74 | | - release_id: existingRelease.id |
75 | | - }) |
76 | | - } |
77 | | - } else { |
78 | | - console.warn('No INPUT_TOKEN specified, skipping creating release') |
79 | | - } |
80 | | -} |
81 | | - |
82 | | -async function buildImages(docker: Docker, phpVersion: PhpVersion, nodeVersions: NodeVersion[]) { |
83 | | - console.log('Building images for ' + phpVersion.tag); |
84 | | - const images = []; |
85 | | - // Build non-debug images |
86 | | - for (let nodeVersion of nodeVersions) { |
87 | | - images.push( |
88 | | - await buildAndPushImage(docker, phpVersion, nodeVersion, false, false), |
89 | | - await buildAndPushImage(docker, phpVersion, nodeVersion, true, false), |
90 | | - await buildAndPushImage(docker, phpVersion, nodeVersion, false, true), |
91 | | - await buildAndPushImage(docker, phpVersion, nodeVersion, true, true) |
92 | | - ); |
93 | | - } |
94 | | - console.log('Done building images for ' + phpVersion.tag); |
95 | | - return images; |
96 | | -} |
97 | | - |
98 | | -async function buildAndPushImage(docker: Docker, phpVersion: PhpVersion, nodeVersion: NodeVersion, imageSupport: boolean, debug: boolean) { |
99 | | - const imageName = 'recognizebv/symfony-docker'; |
100 | | - const tagSuffix = `${phpVersion.webServer === WebServerType.NGINX ? '-nginx' : ''}-node${nodeVersion.major}` + (imageSupport ? '-image' : '') + (debug ? '-dev' : ''); |
101 | | - const exactTagName = `php${phpVersion.exactVersion}${tagSuffix}` |
102 | | - const tagName = `php${phpVersion.version}${tagSuffix}` |
103 | | - const tag = imageName + ':' + tagName; |
104 | | - const architecture = process.argv[3] ?? 'linux/amd64'; |
105 | | - |
106 | | - console.log(`Building image ${tag} with base image version ${phpVersion.exactVersion}`); |
107 | | - const childProcess = spawn('docker', [ |
108 | | - 'buildx', |
109 | | - 'build', |
110 | | - '--platform', architecture, |
111 | | - '--push', |
112 | | - '-f', phpVersion.webServer === WebServerType.NGINX ? 'nginx/Dockerfile' : 'apache/Dockerfile', |
113 | | - '--cache-from', `${imageName}:${tagName}`, |
114 | | - '--tag', `${imageName}:${exactTagName}`, |
115 | | - '--tag', `${imageName}:${tagName}`, |
116 | | - '--build-arg', `BASE_IMAGE=php:${phpVersion.tag}`, |
117 | | - '--build-arg', `NODE_VERSION=${nodeVersion.version}`, |
118 | | - '--build-arg', `ENABLE_IMAGE_SUPPORT=${imageSupport ? 'true' : 'false'}`, |
119 | | - '--build-arg', `ENABLE_DEBUG=${debug ? 'true' : 'false'}`, |
120 | | - '.' |
121 | | - ], {stdio: 'inherit'}); |
122 | | - |
123 | | - await new Promise(((resolve, reject) => childProcess.on('close', code => code === 0 ? resolve(code) : reject(code)))); |
124 | | - |
125 | | - return tag |
126 | | -} |
127 | | - |
128 | | -function getTags(client: { |
129 | | - listTags: (cb: (err: any, response: ListTagsResponse) => void) => void |
130 | | -}): Promise<ListTagsResponse> { |
131 | | - return new Promise((resolve, reject) => { |
132 | | - client.listTags((err, response) => { |
133 | | - if (err) { |
134 | | - reject(err); |
135 | | - } else { |
136 | | - resolve(response); |
137 | | - } |
138 | | - }) |
139 | | - }); |
140 | | -} |
141 | | - |
142 | | -async function getNodeLtsVersions() { |
143 | | - const response = await axios.get<{ version: string, lts: string|boolean }[]>('https://nodejs.org/dist/index.json'); |
144 | | - const groupedVersion = groupBy(response.data.filter(it => it.lts), 'lts'); |
145 | | - const versions: NodeVersion[] = []; |
146 | | - |
147 | | - for (let key in groupedVersion) { |
148 | | - const highestVersion = groupedVersion[key].reduce((a, b) => { |
149 | | - return compareVersions(a.version, b.version) === -1 ? b : a; |
150 | | - }, {version: '0.0.0', lts: ''}); |
151 | | - |
152 | | - const majorMatch = highestVersion.version.match(/^v(\d+).+/) |
153 | | - if (majorMatch !== null) { |
154 | | - versions.push({ |
155 | | - version: highestVersion.version, |
156 | | - major: majorMatch[1], |
157 | | - lts: highestVersion.lts || '' |
158 | | - } as NodeVersion) |
159 | | - } |
160 | | - } |
161 | | - return versions.filter(it => Number(it.major) >= 18 && Number(it.major) <= 20) |
162 | | -} |
163 | | - |
164 | | -function groupBy<T, K extends keyof T>(items: T[], key: K): { [key: string]: T[] } { |
165 | | - return items.reduce((objectsByKeyValue, obj) => { |
166 | | - const value = obj[key]; |
167 | | - objectsByKeyValue[value] = (objectsByKeyValue[value] || []).concat(obj); |
168 | | - return objectsByKeyValue; |
169 | | - }, {} as any); |
170 | | -} |
| 1 | +import {ListTagsResponse, NodeVersion, PhpVersion, WebServerType} from './model'; |
| 2 | +import compareVersions from 'compare-versions'; |
| 3 | +import Docker from 'dockerode'; |
| 4 | +import axios from 'axios'; |
| 5 | +import {spawn} from 'child_process'; |
| 6 | +import {getInput} from "@actions/core"; |
| 7 | +import {context, getOctokit} from "@actions/github"; |
| 8 | +// @ts-ignore |
| 9 | +import {createClientV2} from 'docker-registry-client'; |
| 10 | + |
| 11 | +(async () => { |
| 12 | + try { |
| 13 | + const phpRegex = new RegExp(process.argv[2] || '^([89]\\.\\d)(\\.\\d+)?-(apache|fpm)$'); |
| 14 | + const client = createClientV2({'name': 'php'}); |
| 15 | + const phpVersions: PhpVersion[] = (await getTags(client)).tags.map(it => { |
| 16 | + const match = phpRegex.exec(it); |
| 17 | + return { |
| 18 | + tag: it, |
| 19 | + version: match && match[1], |
| 20 | + exactVersion: match && (match[1] + (match[2] || '')), |
| 21 | + webServer: it.includes('-fpm') ? WebServerType.NGINX : WebServerType.APACHE, |
| 22 | + } as PhpVersion; |
| 23 | + }).filter(it => it.version); |
| 24 | + const highestPhpVersion = phpVersions.reduce((a, b) => { |
| 25 | + return compareVersions(a.exactVersion, b.exactVersion) === -1 ? b : a; |
| 26 | + }); |
| 27 | + |
| 28 | + const nodeVersions = await getNodeLtsVersions(); |
| 29 | + |
| 30 | + const docker = new Docker(); |
| 31 | + |
| 32 | + const imageTags = await buildImages(docker, highestPhpVersion, nodeVersions) |
| 33 | + await createOrUpdateRelease(highestPhpVersion, imageTags) |
| 34 | + } catch (e) { |
| 35 | + console.error('Build failed: ', e); |
| 36 | + process.exit(typeof e == 'number' ? e : 1); |
| 37 | + } |
| 38 | +})(); |
| 39 | + |
| 40 | +async function createOrUpdateRelease(phpVersion: PhpVersion, imageTags: string[]) { |
| 41 | + const githubToken = getInput("token", {required: false}); |
| 42 | + if (githubToken) { |
| 43 | + const octokit = getOctokit(githubToken); |
| 44 | + const { owner, repo } = context.repo; |
| 45 | + |
| 46 | + const target_commitish = getInput('commitish', { required: false }) || context.sha; |
| 47 | + const tag = `php${phpVersion.version}` |
| 48 | + const existingRelease = await octokit.rest.repos.getReleaseByTag({owner, repo, tag}).then(response => response.data).catch(err => { |
| 49 | + if (err?.status === 404) { |
| 50 | + return null |
| 51 | + } |
| 52 | + throw err; |
| 53 | + }) |
| 54 | + |
| 55 | + const body = `Available tags for \`${tag}\`:\n\n` + imageTags.join('\n') |
| 56 | + |
| 57 | + if (existingRelease === null) { |
| 58 | + octokit.rest.repos.createRelease({ |
| 59 | + owner, |
| 60 | + repo, |
| 61 | + tag_name: tag, |
| 62 | + target_commitish, |
| 63 | + name: tag, |
| 64 | + body |
| 65 | + }) |
| 66 | + } else { |
| 67 | + octokit.rest.repos.updateRelease({ |
| 68 | + owner, |
| 69 | + repo, |
| 70 | + tag_name: tag, |
| 71 | + target_commitish, |
| 72 | + name: tag, |
| 73 | + body, |
| 74 | + release_id: existingRelease.id |
| 75 | + }) |
| 76 | + } |
| 77 | + } else { |
| 78 | + console.warn('No INPUT_TOKEN specified, skipping creating release') |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +async function buildImages(docker: Docker, phpVersion: PhpVersion, nodeVersions: NodeVersion[]) { |
| 83 | + console.log('Building images for ' + phpVersion.tag); |
| 84 | + const images = []; |
| 85 | + // Build non-debug images |
| 86 | + for (let nodeVersion of nodeVersions) { |
| 87 | + images.push( |
| 88 | + await buildAndPushImage(docker, phpVersion, nodeVersion, false, false), |
| 89 | + await buildAndPushImage(docker, phpVersion, nodeVersion, true, false), |
| 90 | + await buildAndPushImage(docker, phpVersion, nodeVersion, false, true), |
| 91 | + await buildAndPushImage(docker, phpVersion, nodeVersion, true, true) |
| 92 | + ); |
| 93 | + } |
| 94 | + console.log('Done building images for ' + phpVersion.tag); |
| 95 | + return images; |
| 96 | +} |
| 97 | + |
| 98 | +async function buildAndPushImage(docker: Docker, phpVersion: PhpVersion, nodeVersion: NodeVersion, imageSupport: boolean, debug: boolean) { |
| 99 | + const imageName = 'recognizebv/symfony-docker'; |
| 100 | + const tagSuffix = `${phpVersion.webServer === WebServerType.NGINX ? '-nginx' : ''}-node${nodeVersion.major}` + (imageSupport ? '-image' : '') + (debug ? '-dev' : ''); |
| 101 | + const exactTagName = `php${phpVersion.exactVersion}${tagSuffix}` |
| 102 | + const tagName = `php${phpVersion.version}${tagSuffix}` |
| 103 | + const tag = imageName + ':' + tagName; |
| 104 | + const architecture = process.argv[3] ?? 'linux/amd64'; |
| 105 | + |
| 106 | + console.log(`Building image ${tag} with base image version ${phpVersion.exactVersion}`); |
| 107 | + const childProcess = spawn('docker', [ |
| 108 | + 'buildx', |
| 109 | + 'build', |
| 110 | + '--platform', architecture, |
| 111 | + '--push', |
| 112 | + '-f', phpVersion.webServer === WebServerType.NGINX ? 'nginx/Dockerfile' : 'apache/Dockerfile', |
| 113 | + '--cache-from', `${imageName}:${tagName}`, |
| 114 | + '--tag', `${imageName}:${exactTagName}`, |
| 115 | + '--tag', `${imageName}:${tagName}`, |
| 116 | + '--build-arg', `BASE_IMAGE=php:${phpVersion.tag}`, |
| 117 | + '--build-arg', `NODE_VERSION=${nodeVersion.version}`, |
| 118 | + '--build-arg', `ENABLE_IMAGE_SUPPORT=${imageSupport ? 'true' : 'false'}`, |
| 119 | + '--build-arg', `ENABLE_DEBUG=${debug ? 'true' : 'false'}`, |
| 120 | + '.' |
| 121 | + ], {stdio: 'inherit'}); |
| 122 | + |
| 123 | + await new Promise(((resolve, reject) => childProcess.on('close', code => code === 0 ? resolve(code) : reject(code)))); |
| 124 | + |
| 125 | + return tag |
| 126 | +} |
| 127 | + |
| 128 | +function getTags(client: { |
| 129 | + listTags: (cb: (err: any, response: ListTagsResponse) => void) => void |
| 130 | +}): Promise<ListTagsResponse> { |
| 131 | + return new Promise((resolve, reject) => { |
| 132 | + client.listTags((err, response) => { |
| 133 | + if (err) { |
| 134 | + reject(err); |
| 135 | + } else { |
| 136 | + resolve(response); |
| 137 | + } |
| 138 | + }) |
| 139 | + }); |
| 140 | +} |
| 141 | + |
| 142 | +async function getNodeLtsVersions() { |
| 143 | + const response = await axios.get<{ version: string, lts: string|boolean }[]>('https://nodejs.org/dist/index.json'); |
| 144 | + const groupedVersion = groupBy(response.data.filter(it => it.lts), 'lts'); |
| 145 | + const versions: NodeVersion[] = []; |
| 146 | + |
| 147 | + for (let key in groupedVersion) { |
| 148 | + const highestVersion = groupedVersion[key].reduce((a, b) => { |
| 149 | + return compareVersions(a.version, b.version) === -1 ? b : a; |
| 150 | + }, {version: '0.0.0', lts: ''}); |
| 151 | + |
| 152 | + const majorMatch = highestVersion.version.match(/^v(\d+).+/) |
| 153 | + if (majorMatch !== null) { |
| 154 | + versions.push({ |
| 155 | + version: highestVersion.version, |
| 156 | + major: majorMatch[1], |
| 157 | + lts: highestVersion.lts || '' |
| 158 | + } as NodeVersion) |
| 159 | + } |
| 160 | + } |
| 161 | + return versions.filter(it => Number(it.major) >= 18 && Number(it.major) <= 22) |
| 162 | +} |
| 163 | + |
| 164 | +function groupBy<T, K extends keyof T>(items: T[], key: K): { [key: string]: T[] } { |
| 165 | + return items.reduce((objectsByKeyValue, obj) => { |
| 166 | + const value = obj[key]; |
| 167 | + objectsByKeyValue[value] = (objectsByKeyValue[value] || []).concat(obj); |
| 168 | + return objectsByKeyValue; |
| 169 | + }, {} as any); |
| 170 | +} |
0 commit comments