@docker/github-builder-runtime 0.90.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (211) hide show
  1. package/LICENSE +191 -0
  2. package/README.md +5 -0
  3. package/lib/buildkit/buildkit.d.ts +29 -0
  4. package/lib/buildkit/buildkit.js +95 -0
  5. package/lib/buildkit/buildkit.js.map +1 -0
  6. package/lib/buildkit/config.d.ts +20 -0
  7. package/lib/buildkit/config.js +37 -0
  8. package/lib/buildkit/config.js.map +1 -0
  9. package/lib/buildkit/git.d.ts +24 -0
  10. package/lib/buildkit/git.js +103 -0
  11. package/lib/buildkit/git.js.map +1 -0
  12. package/lib/buildx/bake.d.ts +64 -0
  13. package/lib/buildx/bake.js +387 -0
  14. package/lib/buildx/bake.js.map +1 -0
  15. package/lib/buildx/build.d.ts +63 -0
  16. package/lib/buildx/build.js +356 -0
  17. package/lib/buildx/build.js.map +1 -0
  18. package/lib/buildx/builder.d.ts +27 -0
  19. package/lib/buildx/builder.js +290 -0
  20. package/lib/buildx/builder.js.map +1 -0
  21. package/lib/buildx/buildx.d.ts +47 -0
  22. package/lib/buildx/buildx.js +374 -0
  23. package/lib/buildx/buildx.js.map +1 -0
  24. package/lib/buildx/history.d.ts +39 -0
  25. package/lib/buildx/history.js +144 -0
  26. package/lib/buildx/history.js.map +1 -0
  27. package/lib/buildx/imagetools.d.ts +47 -0
  28. package/lib/buildx/imagetools.js +205 -0
  29. package/lib/buildx/imagetools.js.map +1 -0
  30. package/lib/buildx/install.d.ts +47 -0
  31. package/lib/buildx/install.js +314 -0
  32. package/lib/buildx/install.js.map +1 -0
  33. package/lib/cache.d.ts +40 -0
  34. package/lib/cache.js +129 -0
  35. package/lib/cache.js.map +1 -0
  36. package/lib/compose/compose.d.ts +33 -0
  37. package/lib/compose/compose.js +91 -0
  38. package/lib/compose/compose.js.map +1 -0
  39. package/lib/compose/install.d.ts +34 -0
  40. package/lib/compose/install.js +190 -0
  41. package/lib/compose/install.js.map +1 -0
  42. package/lib/context.d.ts +22 -0
  43. package/lib/context.js +33 -0
  44. package/lib/context.js.map +1 -0
  45. package/lib/cosign/cosign.d.ts +40 -0
  46. package/lib/cosign/cosign.js +127 -0
  47. package/lib/cosign/cosign.js.map +1 -0
  48. package/lib/cosign/dockerfile.d.ts +16 -0
  49. package/lib/cosign/dockerfile.js +61 -0
  50. package/lib/cosign/dockerfile.js.map +1 -0
  51. package/lib/cosign/install.d.ts +45 -0
  52. package/lib/cosign/install.js +229 -0
  53. package/lib/cosign/install.js.map +1 -0
  54. package/lib/docker/assets.d.ts +21 -0
  55. package/lib/docker/assets.js +346 -0
  56. package/lib/docker/assets.js.map +1 -0
  57. package/lib/docker/docker.d.ts +35 -0
  58. package/lib/docker/docker.js +199 -0
  59. package/lib/docker/docker.js.map +1 -0
  60. package/lib/docker/install.d.ts +85 -0
  61. package/lib/docker/install.js +726 -0
  62. package/lib/docker/install.js.map +1 -0
  63. package/lib/dockerhub.d.ts +33 -0
  64. package/lib/dockerhub.js +111 -0
  65. package/lib/dockerhub.js.map +1 -0
  66. package/lib/exec.d.ts +20 -0
  67. package/lib/exec.js +28 -0
  68. package/lib/exec.js.map +1 -0
  69. package/lib/git.d.ts +33 -0
  70. package/lib/git.js +185 -0
  71. package/lib/git.js.map +1 -0
  72. package/lib/github/artifact.d.ts +19 -0
  73. package/lib/github/artifact.js +49 -0
  74. package/lib/github/artifact.js.map +1 -0
  75. package/lib/github/github.d.ts +39 -0
  76. package/lib/github/github.js +135 -0
  77. package/lib/github/github.js.map +1 -0
  78. package/lib/github/summary.d.ts +20 -0
  79. package/lib/github/summary.js +166 -0
  80. package/lib/github/summary.js.map +1 -0
  81. package/lib/index.d.ts +24 -0
  82. package/lib/index.js +48 -0
  83. package/lib/index.js.map +1 -0
  84. package/lib/oci/oci.d.ts +11 -0
  85. package/lib/oci/oci.js +199 -0
  86. package/lib/oci/oci.js.map +1 -0
  87. package/lib/regclient/install.d.ts +30 -0
  88. package/lib/regclient/install.js +136 -0
  89. package/lib/regclient/install.js.map +1 -0
  90. package/lib/regclient/regctl.d.ts +39 -0
  91. package/lib/regclient/regctl.js +102 -0
  92. package/lib/regclient/regctl.js.map +1 -0
  93. package/lib/sigstore/sigstore.d.ts +41 -0
  94. package/lib/sigstore/sigstore.js +461 -0
  95. package/lib/sigstore/sigstore.js.map +1 -0
  96. package/lib/toolkit.d.ts +57 -0
  97. package/lib/toolkit.js +68 -0
  98. package/lib/toolkit.js.map +1 -0
  99. package/lib/types/buildkit/buildkit.d.ts +16 -0
  100. package/lib/types/buildkit/buildkit.js +18 -0
  101. package/lib/types/buildkit/buildkit.js.map +1 -0
  102. package/lib/types/buildkit/client.d.ts +61 -0
  103. package/lib/types/buildkit/client.js +17 -0
  104. package/lib/types/buildkit/client.js.map +1 -0
  105. package/lib/types/buildkit/control.d.ts +88 -0
  106. package/lib/types/buildkit/control.js +17 -0
  107. package/lib/types/buildkit/control.js.map +1 -0
  108. package/lib/types/buildkit/git.d.ts +40 -0
  109. package/lib/types/buildkit/git.js +17 -0
  110. package/lib/types/buildkit/git.js.map +1 -0
  111. package/lib/types/buildkit/ops.d.ts +60 -0
  112. package/lib/types/buildkit/ops.js +17 -0
  113. package/lib/types/buildkit/ops.js.map +1 -0
  114. package/lib/types/buildkit/rpc.d.ts +24 -0
  115. package/lib/types/buildkit/rpc.js +17 -0
  116. package/lib/types/buildkit/rpc.js.map +1 -0
  117. package/lib/types/buildx/bake.d.ts +70 -0
  118. package/lib/types/buildx/bake.js +17 -0
  119. package/lib/types/buildx/bake.js.map +1 -0
  120. package/lib/types/buildx/build.d.ts +19 -0
  121. package/lib/types/buildx/build.js +17 -0
  122. package/lib/types/buildx/build.js.map +1 -0
  123. package/lib/types/buildx/builder.d.ts +52 -0
  124. package/lib/types/buildx/builder.js +17 -0
  125. package/lib/types/buildx/builder.js.map +1 -0
  126. package/lib/types/buildx/buildx.d.ts +42 -0
  127. package/lib/types/buildx/buildx.js +17 -0
  128. package/lib/types/buildx/buildx.js.map +1 -0
  129. package/lib/types/buildx/history.d.ts +111 -0
  130. package/lib/types/buildx/history.js +17 -0
  131. package/lib/types/buildx/history.js.map +1 -0
  132. package/lib/types/buildx/imagetools.d.ts +52 -0
  133. package/lib/types/buildx/imagetools.js +17 -0
  134. package/lib/types/buildx/imagetools.js.map +1 -0
  135. package/lib/types/compose/compose.d.ts +22 -0
  136. package/lib/types/compose/compose.js +17 -0
  137. package/lib/types/compose/compose.js.map +1 -0
  138. package/lib/types/cosign/cosign.d.ts +21 -0
  139. package/lib/types/cosign/cosign.js +17 -0
  140. package/lib/types/cosign/cosign.js.map +1 -0
  141. package/lib/types/docker/docker.d.ts +82 -0
  142. package/lib/types/docker/docker.js +17 -0
  143. package/lib/types/docker/docker.js.map +1 -0
  144. package/lib/types/docker/mediatype.d.ts +18 -0
  145. package/lib/types/docker/mediatype.js +19 -0
  146. package/lib/types/docker/mediatype.js.map +1 -0
  147. package/lib/types/dockerhub.d.ts +104 -0
  148. package/lib/types/dockerhub.js +17 -0
  149. package/lib/types/dockerhub.js.map +1 -0
  150. package/lib/types/github/artifact.d.ts +26 -0
  151. package/lib/types/github/artifact.js +17 -0
  152. package/lib/types/github/artifact.js.map +1 -0
  153. package/lib/types/github/github.d.ts +43 -0
  154. package/lib/types/github/github.js +17 -0
  155. package/lib/types/github/github.js.map +1 -0
  156. package/lib/types/github/summary.d.ts +29 -0
  157. package/lib/types/github/summary.js +17 -0
  158. package/lib/types/github/summary.js.map +1 -0
  159. package/lib/types/intoto/intoto.d.ts +21 -0
  160. package/lib/types/intoto/intoto.js +19 -0
  161. package/lib/types/intoto/intoto.js.map +1 -0
  162. package/lib/types/intoto/slsa_provenance/v0.2/provenance.d.ts +55 -0
  163. package/lib/types/intoto/slsa_provenance/v0.2/provenance.js +18 -0
  164. package/lib/types/intoto/slsa_provenance/v0.2/provenance.js.map +1 -0
  165. package/lib/types/oci/config.d.ts +47 -0
  166. package/lib/types/oci/config.js +17 -0
  167. package/lib/types/oci/config.js.map +1 -0
  168. package/lib/types/oci/descriptor.d.ts +34 -0
  169. package/lib/types/oci/descriptor.js +23 -0
  170. package/lib/types/oci/descriptor.js.map +1 -0
  171. package/lib/types/oci/digest.d.ts +16 -0
  172. package/lib/types/oci/digest.js +17 -0
  173. package/lib/types/oci/digest.js.map +1 -0
  174. package/lib/types/oci/index.d.ts +24 -0
  175. package/lib/types/oci/index.js +17 -0
  176. package/lib/types/oci/index.js.map +1 -0
  177. package/lib/types/oci/layout.d.ts +22 -0
  178. package/lib/types/oci/layout.js +20 -0
  179. package/lib/types/oci/layout.js.map +1 -0
  180. package/lib/types/oci/manifest.d.ts +25 -0
  181. package/lib/types/oci/manifest.js +17 -0
  182. package/lib/types/oci/manifest.js.map +1 -0
  183. package/lib/types/oci/mediatype.d.ts +21 -0
  184. package/lib/types/oci/mediatype.js +22 -0
  185. package/lib/types/oci/mediatype.js.map +1 -0
  186. package/lib/types/oci/oci.d.ts +32 -0
  187. package/lib/types/oci/oci.js +17 -0
  188. package/lib/types/oci/oci.js.map +1 -0
  189. package/lib/types/oci/versioned.d.ts +18 -0
  190. package/lib/types/oci/versioned.js +17 -0
  191. package/lib/types/oci/versioned.js.map +1 -0
  192. package/lib/types/regclient/regclient.d.ts +21 -0
  193. package/lib/types/regclient/regclient.js +17 -0
  194. package/lib/types/regclient/regclient.js.map +1 -0
  195. package/lib/types/sigstore/sigstore.d.ts +80 -0
  196. package/lib/types/sigstore/sigstore.js +20 -0
  197. package/lib/types/sigstore/sigstore.js.map +1 -0
  198. package/lib/types/undock/undock.d.ts +21 -0
  199. package/lib/types/undock/undock.js +17 -0
  200. package/lib/types/undock/undock.js.map +1 -0
  201. package/lib/undock/install.d.ts +30 -0
  202. package/lib/undock/install.js +146 -0
  203. package/lib/undock/install.js.map +1 -0
  204. package/lib/undock/undock.d.ts +42 -0
  205. package/lib/undock/undock.js +121 -0
  206. package/lib/undock/undock.js.map +1 -0
  207. package/lib/util.d.ts +49 -0
  208. package/lib/util.js +218 -0
  209. package/lib/util.js.map +1 -0
  210. package/npm-shrinkwrap.json +2858 -0
  211. package/package.json +57 -0
@@ -0,0 +1,726 @@
1
+ /**
2
+ * Copyright 2023 actions-toolkit authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import * as child_process from 'child_process';
17
+ import fs from 'fs';
18
+ import fsp from 'fs/promises';
19
+ import os from 'os';
20
+ import path from 'path';
21
+ import retry from 'async-retry';
22
+ import handlebars from 'handlebars';
23
+ import * as core from '@actions/core';
24
+ import * as io from '@actions/io';
25
+ import * as tc from '@actions/tool-cache';
26
+ import { Context } from '../context.js';
27
+ import { Docker } from './docker.js';
28
+ import { Exec } from '../exec.js';
29
+ import { GitHub } from '../github/github.js';
30
+ import { Regctl } from '../regclient/regctl.js';
31
+ import { Undock } from '../undock/undock.js';
32
+ import { Util } from '../util.js';
33
+ import { limaYamlData, dockerServiceLogsPs1, setupDockerWinPs1 } from './assets.js';
34
+ export class Install {
35
+ runDir;
36
+ source;
37
+ contextName;
38
+ daemonConfig;
39
+ rootless;
40
+ localTCPPort;
41
+ regctl;
42
+ undock;
43
+ githubToken;
44
+ _version;
45
+ _toolDir;
46
+ gitCommit;
47
+ limaInstanceName = 'docker-actions-toolkit';
48
+ constructor(opts) {
49
+ this.runDir = opts.runDir;
50
+ this.source = opts.source || {
51
+ type: 'archive',
52
+ version: 'latest',
53
+ channel: 'stable'
54
+ };
55
+ this.contextName = opts.contextName || 'setup-docker-action';
56
+ this.daemonConfig = opts.daemonConfig;
57
+ this.rootless = opts.rootless || false;
58
+ this.localTCPPort = opts.localTCPPort;
59
+ this.regctl = opts.regctl || new Regctl();
60
+ this.undock = opts.undock || new Undock();
61
+ this.githubToken = opts.githubToken || process.env.GITHUB_TOKEN;
62
+ }
63
+ get toolDir() {
64
+ return this._toolDir || Context.tmpDir();
65
+ }
66
+ async download() {
67
+ let extractFolder;
68
+ let cacheKey;
69
+ const platform = os.platform();
70
+ switch (this.source.type) {
71
+ case 'image': {
72
+ this._version = this.source.tag;
73
+ cacheKey = `docker-image`;
74
+ extractFolder = await this.downloadSourceImage(platform);
75
+ break;
76
+ }
77
+ case 'archive': {
78
+ const version = this.source.version;
79
+ const channel = this.source.channel;
80
+ cacheKey = `docker-archive-${channel}`;
81
+ this._version = version;
82
+ core.info(`Downloading Docker ${version} from ${this.source.channel} at download.docker.com`);
83
+ extractFolder = await this.downloadSourceArchive('docker', this.source);
84
+ if (this.rootless) {
85
+ core.info(`Downloading Docker rootless extras ${version} from ${this.source.channel} at download.docker.com`);
86
+ const extrasFolder = await this.downloadSourceArchive('docker-rootless-extras', this.source);
87
+ fs.readdirSync(extrasFolder).forEach(file => {
88
+ const src = path.join(extrasFolder, file);
89
+ const dest = path.join(extractFolder, file);
90
+ fs.copyFileSync(src, dest);
91
+ });
92
+ }
93
+ break;
94
+ }
95
+ }
96
+ core.info('Fixing perms');
97
+ fs.readdir(path.join(extractFolder), function (err, files) {
98
+ if (err) {
99
+ throw err;
100
+ }
101
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
102
+ files.forEach(function (file, index) {
103
+ if (!Util.isDirectory(path.join(extractFolder, file))) {
104
+ fs.chmodSync(path.join(extractFolder, file), '0755');
105
+ }
106
+ });
107
+ });
108
+ const tooldir = await tc.cacheDir(extractFolder, cacheKey, this._version.replace(/(0+)([1-9]+)/, '$2'));
109
+ core.addPath(tooldir);
110
+ core.info('Added Docker to PATH');
111
+ this._toolDir = tooldir;
112
+ return tooldir;
113
+ }
114
+ async downloadSourceImage(platform) {
115
+ const dest = path.join(Context.tmpDir(), 'docker-install-image');
116
+ const cliImage = `dockereng/cli-bin:${this._version}`;
117
+ const engineImage = `moby/moby-bin:${this._version}`;
118
+ core.info(`Downloading Docker CLI from ${cliImage}`);
119
+ await this.undock.run({
120
+ source: cliImage,
121
+ dist: dest
122
+ });
123
+ if (['win32', 'linux'].includes(platform)) {
124
+ core.info(`Downloading Docker engine from ${engineImage}`);
125
+ await this.undock.run({
126
+ source: engineImage,
127
+ dist: dest
128
+ });
129
+ }
130
+ else if (platform == 'darwin') {
131
+ // On macOS, the docker daemon binary will be downloaded inside the lima VM.
132
+ // However, we will get the exact git revision from the image config
133
+ // to get the matching systemd unit files. There's no macOS image for
134
+ // moby/moby-bin - a linux daemon is run inside lima.
135
+ try {
136
+ const engineImageConfig = await this.imageConfig(engineImage, 'linux/arm64');
137
+ core.debug(`docker.Install.downloadSourceImage engineImageConfig: ${JSON.stringify(engineImageConfig)}`);
138
+ this.gitCommit = engineImageConfig.config?.Labels?.['org.opencontainers.image.revision'];
139
+ if (!this.gitCommit) {
140
+ throw new Error(`No git revision can be determined from the image`);
141
+ }
142
+ }
143
+ catch (e) {
144
+ core.warning(e);
145
+ this.gitCommit = 'master';
146
+ }
147
+ core.debug(`docker.Install.downloadSourceImage gitCommit: ${this.gitCommit}`);
148
+ }
149
+ else {
150
+ core.warning(`Docker engine not supported on ${platform}, only the Docker cli will be available`);
151
+ }
152
+ return dest;
153
+ }
154
+ async downloadSourceArchive(component, src) {
155
+ const release = await Install.getRelease(src.version, this.githubToken);
156
+ this._version = release.tag_name.replace(/^(docker-)?v+/, '');
157
+ core.debug(`docker.Install.downloadSourceArchive version: ${this._version}`);
158
+ const downloadURL = this.downloadURL(component, this._version, src.channel);
159
+ core.info(`Downloading ${downloadURL}`);
160
+ const downloadPath = await tc.downloadTool(downloadURL);
161
+ core.debug(`docker.Install.downloadSourceArchive downloadPath: ${downloadPath}`);
162
+ let extractFolder;
163
+ if (os.platform() == 'win32') {
164
+ extractFolder = await tc.extractZip(downloadPath, extractFolder);
165
+ }
166
+ else {
167
+ extractFolder = await tc.extractTar(downloadPath, extractFolder);
168
+ }
169
+ if (Util.isDirectory(path.join(extractFolder, component))) {
170
+ extractFolder = path.join(extractFolder, component);
171
+ }
172
+ core.debug(`docker.Install.downloadSourceArchive extractFolder: ${extractFolder}`);
173
+ return extractFolder;
174
+ }
175
+ async install() {
176
+ if (!this.toolDir) {
177
+ throw new Error('toolDir must be set. Run download first.');
178
+ }
179
+ if (!this.runDir) {
180
+ throw new Error('runDir must be set');
181
+ }
182
+ const platform = os.platform();
183
+ if (this.rootless && platform != 'linux') {
184
+ // TODO: Support on macOS (via lima)
185
+ throw new Error(`rootless is only supported on linux`);
186
+ }
187
+ switch (platform) {
188
+ case 'darwin': {
189
+ return await this.installDarwin();
190
+ }
191
+ case 'linux': {
192
+ return await this.installLinux();
193
+ }
194
+ case 'win32': {
195
+ return await this.installWindows();
196
+ }
197
+ default: {
198
+ throw new Error(`Unsupported platform: ${os.platform()}`);
199
+ }
200
+ }
201
+ }
202
+ async installDarwin() {
203
+ if (this.source.type == 'image' && !this.gitCommit) {
204
+ throw new Error('gitCommit must be set. Run download first.');
205
+ }
206
+ const src = this.source;
207
+ const limaDir = path.join(os.homedir(), '.lima', this.limaInstanceName);
208
+ await io.mkdirP(limaDir);
209
+ const dockerHost = `unix://${limaDir}/docker.sock`;
210
+ if (!(await Install.limaInstalled())) {
211
+ await this.brewInstall('lima');
212
+ }
213
+ await core.group('Lima version', async () => {
214
+ await Exec.exec('lima', ['--version']);
215
+ });
216
+ await core.group('Creating lima config', async () => {
217
+ let limaDaemonConfig = {};
218
+ if (this.daemonConfig) {
219
+ limaDaemonConfig = JSON.parse(this.daemonConfig);
220
+ }
221
+ handlebars.registerHelper('stringify', function (obj) {
222
+ return new handlebars.SafeString(JSON.stringify(obj));
223
+ });
224
+ const srcArchive = src;
225
+ const limaCfg = handlebars.compile(limaYamlData)({
226
+ customImages: Install.limaCustomImages(),
227
+ daemonConfig: limaDaemonConfig,
228
+ dockerSock: `${limaDir}/docker.sock`,
229
+ localTCPPort: this.localTCPPort,
230
+ gitCommit: this.gitCommit,
231
+ srcType: src.type,
232
+ srcArchiveVersion: this._version, // Use the resolved version (e.g. latest -> 27.4.0)
233
+ srcArchiveChannel: srcArchive.channel,
234
+ srcImageTag: src.tag
235
+ });
236
+ core.info(`Writing lima config to ${path.join(limaDir, 'lima.yaml')}`);
237
+ fs.writeFileSync(path.join(limaDir, 'lima.yaml'), limaCfg);
238
+ core.info(limaCfg);
239
+ });
240
+ if (!(await Install.qemuInstalled())) {
241
+ await this.brewInstall('qemu');
242
+ }
243
+ const qemuBin = await Install.qemuBin();
244
+ await core.group('QEMU version', async () => {
245
+ await Exec.exec(qemuBin, ['--version']);
246
+ });
247
+ // lima might already be started on the runner so env var added in download
248
+ // method is not expanded to the running process.
249
+ const envs = Object.assign({}, process.env, {
250
+ PATH: `${this.toolDir}:${process.env.PATH}`
251
+ });
252
+ await core.group('Starting lima instance', async () => {
253
+ const limaStartArgs = ['start', `--name=${this.limaInstanceName}`, `--timeout=${process.env.LIMA_START_TIMEOUT ?? '15m'}`];
254
+ if (process.env.LIMA_START_ARGS) {
255
+ limaStartArgs.push(process.env.LIMA_START_ARGS);
256
+ }
257
+ try {
258
+ await Exec.exec(`limactl ${limaStartArgs.join(' ')}`, [], { env: envs });
259
+ }
260
+ catch (e) {
261
+ await this.printDarwinDiagnostics();
262
+ fsp
263
+ .readdir(limaDir)
264
+ .then(files => {
265
+ files
266
+ .filter(f => path.extname(f) === '.log')
267
+ .forEach(f => {
268
+ const logfile = path.join(limaDir, f);
269
+ const logcontent = fs.readFileSync(logfile, { encoding: 'utf8' }).trim();
270
+ if (logcontent.length > 0) {
271
+ core.info(`### ${logfile}:\n${logcontent}`);
272
+ }
273
+ });
274
+ })
275
+ .catch(() => {
276
+ // ignore
277
+ });
278
+ throw e;
279
+ }
280
+ });
281
+ await core.group('Create Docker context', async () => {
282
+ await Docker.exec(['context', 'create', this.contextName, '--docker', `host=${dockerHost}`]);
283
+ await Docker.exec(['context', 'use', this.contextName]);
284
+ });
285
+ return dockerHost;
286
+ }
287
+ async installLinux() {
288
+ const dockerHost = `unix://${path.join(this.runDir, 'docker.sock')}`;
289
+ await io.mkdirP(this.runDir);
290
+ const daemonConfigPath = path.join(this.runDir, 'daemon.json');
291
+ await fs.writeFileSync(daemonConfigPath, '{}');
292
+ let daemonConfig = undefined;
293
+ const daemonConfigDefaultPath = '/etc/docker/daemon.json';
294
+ if (fs.existsSync(daemonConfigDefaultPath)) {
295
+ await core.group('Default Docker daemon config found', async () => {
296
+ core.info(JSON.stringify(JSON.parse(fs.readFileSync(daemonConfigDefaultPath, { encoding: 'utf8' })), null, 2));
297
+ });
298
+ daemonConfig = JSON.parse(fs.readFileSync(daemonConfigDefaultPath, { encoding: 'utf8' }));
299
+ }
300
+ if (this.daemonConfig) {
301
+ daemonConfig = Object.assign(daemonConfig || {}, JSON.parse(this.daemonConfig));
302
+ }
303
+ if (daemonConfig) {
304
+ const daemonConfigStr = JSON.stringify(daemonConfig, null, 2);
305
+ await core.group('Writing Docker daemon config', async () => {
306
+ fs.writeFileSync(daemonConfigPath, daemonConfigStr);
307
+ core.info(daemonConfigStr);
308
+ });
309
+ }
310
+ const envs = Object.assign({}, process.env, {
311
+ PATH: `${this.toolDir}:${process.env.PATH}`,
312
+ XDG_RUNTIME_DIR: (this.rootless && this.runDir) || undefined
313
+ });
314
+ await core.group('Start Docker daemon', async () => {
315
+ const bashPath = await io.which('bash', true);
316
+ let dockerPath = `${this.toolDir}/dockerd`;
317
+ if (this.rootless) {
318
+ dockerPath = `${this.toolDir}/dockerd-rootless.sh`;
319
+ if (fs.existsSync('/proc/sys/kernel/apparmor_restrict_unprivileged_userns')) {
320
+ await Exec.exec('sudo', ['sh', '-c', 'echo 0 > /proc/sys/kernel/apparmor_restrict_unprivileged_userns']);
321
+ }
322
+ }
323
+ let cmd = `${dockerPath} --host="${dockerHost}" --config-file="${daemonConfigPath}" --exec-root="${this.runDir}/execroot" --data-root="${this.runDir}/data" --pidfile="${this.runDir}/docker.pid"`;
324
+ if (this.localTCPPort) {
325
+ cmd += ` --host="tcp://127.0.0.1:${this.localTCPPort}"`;
326
+ }
327
+ core.info(`[command] ${cmd}`); // https://github.com/actions/toolkit/blob/3d652d3133965f63309e4b2e1c8852cdbdcb3833/packages/exec/src/toolrunner.ts#L47
328
+ let sudo = 'sudo';
329
+ if (this.rootless) {
330
+ sudo += ' -u \\#1001';
331
+ }
332
+ const proc = await child_process.spawn(
333
+ // We can't use Exec.exec here because we need to detach the process to
334
+ // avoid killing it when the action finishes running. Even if detached,
335
+ // we also need to run dockerd in a subshell and unref the process so
336
+ // GitHub Action doesn't wait for it to finish.
337
+ `${sudo} env "PATH=$PATH" "XDG_RUNTIME_DIR=$XDG_RUNTIME_DIR" ${bashPath} << EOF
338
+ ( ${cmd} 2>&1 | tee "${this.runDir}/dockerd.log" ) &
339
+ EOF`, [], {
340
+ env: envs,
341
+ detached: true,
342
+ shell: true,
343
+ stdio: ['ignore', process.stdout, process.stderr]
344
+ });
345
+ proc.unref();
346
+ await Util.sleep(3);
347
+ const retries = 10;
348
+ await retry(async (bail) => {
349
+ try {
350
+ await Exec.getExecOutput(`docker version`, undefined, {
351
+ silent: true,
352
+ env: Object.assign({}, envs, {
353
+ DOCKER_HOST: dockerHost,
354
+ DOCKER_CONTENT_TRUST: 'false'
355
+ })
356
+ });
357
+ }
358
+ catch (e) {
359
+ bail(e);
360
+ }
361
+ }, {
362
+ retries: retries,
363
+ minTimeout: 1000,
364
+ onRetry: (err, i) => {
365
+ core.info(`${err}. Retrying (${i}/${retries})...`);
366
+ }
367
+ });
368
+ core.info(`Docker daemon started started successfully`);
369
+ });
370
+ await core.group('Create Docker context', async () => {
371
+ await Docker.exec(['context', 'create', this.contextName, '--docker', `host=${dockerHost}`]);
372
+ await Docker.exec(['context', 'use', this.contextName]);
373
+ });
374
+ return dockerHost;
375
+ }
376
+ async installWindows() {
377
+ const dockerHostSocket = 'npipe:////./pipe/setup_docker_action';
378
+ let daemonConfig = undefined;
379
+ const daemonConfigPath = path.join(this.runDir, 'daemon.json');
380
+ if (fs.existsSync(daemonConfigPath)) {
381
+ await core.group('Default Docker daemon config found', async () => {
382
+ core.info(JSON.stringify(JSON.parse(fs.readFileSync(daemonConfigPath, { encoding: 'utf8' })), null, 2));
383
+ });
384
+ daemonConfig = JSON.parse(fs.readFileSync(daemonConfigPath, { encoding: 'utf8' }));
385
+ }
386
+ if (this.daemonConfig) {
387
+ daemonConfig = Object.assign(daemonConfig || {}, JSON.parse(this.daemonConfig));
388
+ }
389
+ let daemonConfigStr = '{}';
390
+ if (daemonConfig) {
391
+ daemonConfigStr = JSON.stringify(daemonConfig, null, 2);
392
+ await core.group('Docker daemon config', async () => {
393
+ core.info(daemonConfigStr);
394
+ });
395
+ }
396
+ const params = {
397
+ ToolDir: this.toolDir,
398
+ RunDir: this.runDir,
399
+ DockerHostSocket: dockerHostSocket,
400
+ DaemonConfig: daemonConfigStr
401
+ };
402
+ if (this.localTCPPort) {
403
+ params['DockerHostTCP'] = `tcp://127.0.0.1:${this.localTCPPort}`;
404
+ }
405
+ await core.group('Install Docker daemon service', async () => {
406
+ const setupCmd = await Util.powershellCommand(setupDockerWinPs1(), params);
407
+ await Exec.exec(setupCmd.command, setupCmd.args);
408
+ const logCmd = await Util.powershellCommand(dockerServiceLogsPs1());
409
+ await Exec.exec(logCmd.command, logCmd.args);
410
+ });
411
+ await core.group('Create Docker context', async () => {
412
+ await Docker.exec(['context', 'create', this.contextName, '--docker', `host=${dockerHostSocket}`]);
413
+ await Docker.exec(['context', 'use', this.contextName]);
414
+ });
415
+ return dockerHostSocket;
416
+ }
417
+ async tearDown() {
418
+ if (!this.runDir) {
419
+ throw new Error('runDir must be set');
420
+ }
421
+ switch (os.platform()) {
422
+ case 'darwin': {
423
+ await this.tearDownDarwin();
424
+ break;
425
+ }
426
+ case 'linux': {
427
+ await this.tearDownLinux();
428
+ break;
429
+ }
430
+ case 'win32': {
431
+ await this.tearDownWindows();
432
+ break;
433
+ }
434
+ default: {
435
+ throw new Error(`Unsupported platform: ${os.platform()}`);
436
+ }
437
+ }
438
+ await core.group(`Cleaning up toolDir`, async () => {
439
+ if (!this._toolDir) {
440
+ return;
441
+ }
442
+ fs.rmSync(this._toolDir, { recursive: true, force: true });
443
+ });
444
+ }
445
+ async tearDownDarwin() {
446
+ await this.printDarwinDiagnostics();
447
+ await core.group('Stopping lima instance', async () => {
448
+ await Exec.exec('limactl', ['stop', '--tty=false', this.limaInstanceName, '--force']);
449
+ });
450
+ await core.group('Removing lima instance', async () => {
451
+ await Exec.exec('limactl', ['delete', '--tty=false', this.limaInstanceName, '--force']);
452
+ });
453
+ await core.group('Removing Docker context', async () => {
454
+ await Docker.exec(['context', 'rm', '-f', this.contextName]);
455
+ });
456
+ await core.group(`Cleaning up runDir`, async () => {
457
+ await Exec.exec('sudo', ['rm', '-rf', this.runDir]);
458
+ });
459
+ }
460
+ async printDarwinDiagnostics() {
461
+ await this.printDarwinGuestCommandOutput('Cloud-init output log', ['sudo', 'cat', '/var/log/cloud-init-output.log']);
462
+ await this.printDarwinGuestCommandOutput('Cloud-init status', ['sudo', 'timeout', '300s', 'cloud-init', 'status', '--wait']);
463
+ await this.printDarwinGuestCommandOutput('Docker unit file', ['sudo', 'systemctl', 'cat', 'docker.service']);
464
+ await this.printDarwinGuestCommandOutput('Docker service status', ['sudo', 'systemctl', 'status', 'docker.service', 'docker.socket', '-l', '--no-pager']);
465
+ await this.printDarwinGuestCommandOutput('Archive install script log', ['sudo', 'cat', '/var/log/docker-actions-toolkit-archive-install.log']);
466
+ await this.printDarwinGuestCommandOutput('Docker daemon logs', ['sudo', 'journalctl', '-u', 'docker.service', '-l', '--no-pager']);
467
+ }
468
+ async printDarwinGuestCommandOutput(title, args) {
469
+ await core.group(title, async () => {
470
+ const res = await Exec.getExecOutput('limactl', ['shell', '--tty=false', this.limaInstanceName, ...args], {
471
+ ignoreReturnCode: true,
472
+ silent: true
473
+ }).catch(error => {
474
+ core.info(`Failed to get ${title}: ${error}`);
475
+ return undefined;
476
+ });
477
+ if (!res) {
478
+ return;
479
+ }
480
+ const output = [res.stdout.trim(), res.stderr.trim()].filter(Boolean).join('\n');
481
+ if (output.length > 0) {
482
+ core.info(output);
483
+ }
484
+ else if (res.exitCode !== 0) {
485
+ core.info(`${title} exited with code ${res.exitCode}`);
486
+ }
487
+ });
488
+ }
489
+ async tearDownLinux() {
490
+ await core.group('Docker daemon logs', async () => {
491
+ core.info(fs.readFileSync(path.join(this.runDir, 'dockerd.log'), { encoding: 'utf8' }));
492
+ });
493
+ await core.group('Stopping Docker daemon', async () => {
494
+ await Exec.exec('sudo', ['kill', '-s', 'SIGTERM', fs.readFileSync(path.join(this.runDir, 'docker.pid')).toString().trim()]);
495
+ await Util.sleep(5);
496
+ });
497
+ await core.group('Removing Docker context', async () => {
498
+ await Docker.exec(['context', 'rm', '-f', this.contextName]);
499
+ });
500
+ await core.group(`Cleaning up runDir`, async () => {
501
+ await Exec.exec('sudo', ['rm', '-rf', this.runDir], {
502
+ ignoreReturnCode: true,
503
+ failOnStdErr: false
504
+ });
505
+ });
506
+ }
507
+ async tearDownWindows() {
508
+ await core.group('Docker daemon logs', async () => {
509
+ const logCmd = await Util.powershellCommand(dockerServiceLogsPs1());
510
+ await Exec.exec(logCmd.command, logCmd.args);
511
+ });
512
+ await core.group('Removing Docker context', async () => {
513
+ await Docker.exec(['context', 'rm', '-f', this.contextName]);
514
+ });
515
+ await core.group('Stopping Docker daemon service', async () => {
516
+ await Exec.exec('powershell', ['-Command', `Stop-Service -Name docker -Force`]);
517
+ });
518
+ }
519
+ downloadURL(component, version, channel) {
520
+ const platformOS = Install.platformOS();
521
+ const platformArch = Install.platformArch();
522
+ const ext = platformOS === 'win' ? '.zip' : '.tgz';
523
+ return `https://download.docker.com/${platformOS}/static/${channel}/${platformArch}/${component}-${version}${ext}`;
524
+ }
525
+ static platformOS() {
526
+ switch (os.platform()) {
527
+ case 'darwin': {
528
+ return 'mac';
529
+ }
530
+ case 'linux': {
531
+ return 'linux';
532
+ }
533
+ case 'win32': {
534
+ return 'win';
535
+ }
536
+ default: {
537
+ return os.platform();
538
+ }
539
+ }
540
+ }
541
+ static platformArch() {
542
+ switch (os.arch()) {
543
+ case 'x64': {
544
+ return 'x86_64';
545
+ }
546
+ case 'ppc64': {
547
+ return 'ppc64le';
548
+ }
549
+ case 'arm64': {
550
+ return 'aarch64';
551
+ }
552
+ case 'arm': {
553
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
554
+ const arm_version = process.config.variables.arm_version;
555
+ switch (arm_version) {
556
+ case 6: {
557
+ return 'armel';
558
+ }
559
+ case 7: {
560
+ return 'armhf';
561
+ }
562
+ default: {
563
+ return `v${arm_version}`;
564
+ }
565
+ }
566
+ }
567
+ default: {
568
+ return os.arch();
569
+ }
570
+ }
571
+ }
572
+ static async limaInstalled() {
573
+ return await io
574
+ .which('lima', true)
575
+ .then(res => {
576
+ core.debug(`docker.Install.limaInstalled ok: ${res}`);
577
+ return true;
578
+ })
579
+ .catch(error => {
580
+ core.debug(`docker.Install.limaInstalled error: ${error}`);
581
+ return false;
582
+ });
583
+ }
584
+ static async qemuBin() {
585
+ switch (os.arch()) {
586
+ case 'x64': {
587
+ return `qemu-system-x86_64`;
588
+ }
589
+ case 'arm64': {
590
+ return `qemu-system-aarch64`;
591
+ }
592
+ default: {
593
+ return `qemu-system-${os.arch()}`;
594
+ }
595
+ }
596
+ }
597
+ static async qemuInstalled() {
598
+ return await io
599
+ .which(await Install.qemuBin(), true)
600
+ .then(res => {
601
+ core.debug(`docker.Install.qemuInstalled ok: ${res}`);
602
+ return true;
603
+ })
604
+ .catch(error => {
605
+ core.debug(`docker.Install.qemuInstalled error: ${error}`);
606
+ return false;
607
+ });
608
+ }
609
+ static async getRelease(version, githubToken) {
610
+ const github = new GitHub({ token: githubToken });
611
+ const releases = await github.releases('Docker', {
612
+ owner: 'docker',
613
+ repo: 'actions-toolkit',
614
+ ref: 'main',
615
+ path: '.github/docker-releases.json'
616
+ });
617
+ if (!releases[version]) {
618
+ if (!releases['v' + version]) {
619
+ throw new Error(`Cannot find Docker release ${version} in releases JSON`);
620
+ }
621
+ return releases['v' + version];
622
+ }
623
+ return releases[version];
624
+ }
625
+ static limaCustomImages() {
626
+ const res = [];
627
+ const env = process.env.LIMA_IMAGES;
628
+ if (!env) {
629
+ return res;
630
+ }
631
+ for (const input of Util.getList(env, { ignoreComma: true, comment: '#' })) {
632
+ const archIndex = input.indexOf(':');
633
+ const arch = input.substring(0, archIndex).trim();
634
+ const digestIndex = input.indexOf('@');
635
+ const location = input.substring(archIndex + 1, digestIndex !== -1 ? digestIndex : undefined).trim();
636
+ const digest = digestIndex !== -1 ? input.substring(digestIndex + 1).trim() : '';
637
+ res.push({
638
+ location: location,
639
+ arch: arch,
640
+ digest: digest
641
+ });
642
+ }
643
+ return res;
644
+ }
645
+ async imageConfig(image, platform) {
646
+ const manifest = await this.regctl.manifestGet({
647
+ image: image,
648
+ platform: platform
649
+ });
650
+ const configDigest = manifest?.config?.digest;
651
+ if (!configDigest) {
652
+ throw new Error(`No config digest found for image ${image}`);
653
+ }
654
+ const blob = await this.regctl.blobGet({
655
+ repository: image,
656
+ digest: configDigest
657
+ });
658
+ return JSON.parse(blob);
659
+ }
660
+ async brewInstall(packageName, revision) {
661
+ // avoid brew to auto update and upgrade unrelated packages.
662
+ const envs = Object.assign({}, process.env, {
663
+ HOMEBREW_NO_AUTO_UPDATE: '1',
664
+ HOMEBREW_NO_INSTALL_UPGRADE: '1',
665
+ HOMEBREW_NO_INSTALL_CLEANUP: '1'
666
+ });
667
+ await core.group(`Installing ${packageName}`, async () => {
668
+ if (!revision) {
669
+ await Exec.exec('brew', ['install', packageName]);
670
+ }
671
+ else {
672
+ const dockerTap = 'docker-actions-toolkit/tap';
673
+ const hasDockerTap = await Exec.getExecOutput('brew', ['tap'], {
674
+ ignoreReturnCode: true,
675
+ silent: true,
676
+ env: envs
677
+ }).then(res => {
678
+ if (res.stderr.length > 0 && res.exitCode != 0) {
679
+ throw new Error(res.stderr);
680
+ }
681
+ for (const line of res.stdout.trim().split('\n')) {
682
+ if (line.includes(dockerTap)) {
683
+ return true;
684
+ }
685
+ }
686
+ return false;
687
+ });
688
+ if (!hasDockerTap) {
689
+ await Exec.exec('brew', ['tap-new', dockerTap], { env: envs });
690
+ }
691
+ const brewRepoTapPath = await Exec.getExecOutput('brew', ['--repo', dockerTap], {
692
+ ignoreReturnCode: true,
693
+ silent: true,
694
+ env: envs
695
+ }).then(res => {
696
+ if (res.stderr.length > 0 && res.exitCode != 0) {
697
+ throw new Error(res.stderr);
698
+ }
699
+ return res.stdout.trim();
700
+ });
701
+ const formulaURL = `https://raw.githubusercontent.com/Homebrew/homebrew-core/${revision}/Formula/${packageName.charAt(0)}/${packageName}.rb`;
702
+ await tc.downloadTool(formulaURL, path.join(brewRepoTapPath, 'Formula', `${packageName}.rb`));
703
+ const hasFormulaInstalled = await Exec.getExecOutput('brew', ['ls', '-1'], {
704
+ ignoreReturnCode: true,
705
+ silent: true,
706
+ env: envs
707
+ }).then(res => {
708
+ if (res.stderr.length > 0 && res.exitCode != 0) {
709
+ throw new Error(res.stderr);
710
+ }
711
+ for (const line of res.stdout.trim().split('\n')) {
712
+ if (line.trim() == packageName) {
713
+ return true;
714
+ }
715
+ }
716
+ return false;
717
+ });
718
+ if (hasFormulaInstalled) {
719
+ await Exec.exec('brew', ['uninstall', packageName, '--ignore-dependencies'], { env: envs });
720
+ }
721
+ await Exec.exec('brew', ['install', `${dockerTap}/${packageName}`], { env: envs });
722
+ }
723
+ });
724
+ }
725
+ }
726
+ //# sourceMappingURL=install.js.map