-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
245 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* Copyright 2024 actions-toolkit authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import {beforeEach, describe, expect, it, jest} from '@jest/globals'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
import {Buildx} from '../../src/buildx/buildx'; | ||
import {History} from '../../src/buildx/history'; | ||
import {Exec} from '../../src/exec'; | ||
|
||
const fixturesDir = path.join(__dirname, '..', 'fixtures'); | ||
|
||
// prettier-ignore | ||
const tmpDir = path.join(process.env.TEMP || '/tmp', 'buildx-history-jest'); | ||
|
||
const maybe = !process.env.GITHUB_ACTIONS || (process.env.GITHUB_ACTIONS === 'true' && process.env.ImageOS && process.env.ImageOS.startsWith('ubuntu')) ? describe : describe.skip; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
maybe('export', () => { | ||
it('exports build record', async () => { | ||
const buildx = new Buildx(); | ||
|
||
fs.mkdirSync(tmpDir, {recursive: true}); | ||
const metadataFile = path.join(tmpDir, 'history-export-metadata.json'); | ||
|
||
await expect( | ||
(async () => { | ||
// prettier-ignore | ||
const buildCmd = await buildx.getCommand([ | ||
'build', | ||
'-f', path.join(fixturesDir, 'hello.Dockerfile'), | ||
'--metadata-file', metadataFile, | ||
fixturesDir | ||
]); | ||
await Exec.exec(buildCmd.command, buildCmd.args); | ||
})() | ||
).resolves.not.toThrow(); | ||
|
||
expect(fs.existsSync(metadataFile)).toBe(true); | ||
const metadata = JSON.parse(fs.readFileSync(metadataFile, {encoding: 'utf-8'})); | ||
expect(metadata).toBeDefined(); | ||
console.log('metadata', JSON.stringify(metadata, null, 2)); | ||
expect(metadata['buildx.build.ref']).toBeDefined(); | ||
const buildRef = metadata['buildx.build.ref']; | ||
console.log('buildRef', buildRef); | ||
|
||
const history = new History({buildx: buildx}); | ||
const res = await history.export({ | ||
ref: metadata['buildx.build.ref'] | ||
}); | ||
expect(res).toBeDefined(); | ||
expect(res?.filename).toBeDefined(); | ||
expect(res?.size).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# syntax=docker/dockerfile:1 | ||
|
||
# Copyright 2024 actions-toolkit authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
FROM busybox | ||
RUN echo "Hello, World!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/** | ||
* Copyright 2024 actions-toolkit authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import {ChildProcessByStdio, spawn} from 'child_process'; | ||
import fs from 'fs'; | ||
import {Readable, Writable} from 'node:stream'; | ||
import os from 'os'; | ||
import path from 'path'; | ||
import * as core from '@actions/core'; | ||
|
||
import {Buildx} from './buildx'; | ||
import {Context} from '../context'; | ||
import {Exec} from '../exec'; | ||
|
||
import {ExportRecordOpts, ExportRecordResponse} from '../types/history'; | ||
|
||
export interface HistoryOpts { | ||
buildx?: Buildx; | ||
} | ||
|
||
export class History { | ||
private readonly buildx: Buildx; | ||
|
||
private static readonly EXPORT_TOOL_IMAGE: string = 'docker.io/tonistiigi/test:export-build'; | ||
|
||
constructor(opts?: HistoryOpts) { | ||
this.buildx = opts?.buildx || new Buildx(); | ||
} | ||
|
||
public async export(opts: ExportRecordOpts): Promise<ExportRecordResponse> { | ||
if (os.platform() === 'win32') { | ||
throw new Error('Exporting a build record is currently not supported on Windows'); | ||
} | ||
|
||
const refParts = opts.ref.split('/'); | ||
if (refParts.length != 3) { | ||
throw new Error(`Invalid build ref: ${opts.ref}`); | ||
} | ||
|
||
const builderName = refParts[0]; | ||
const nodeName = refParts[1]; | ||
const refID = refParts[2]; | ||
const outDir = path.join(Context.tmpDir(), 'export'); | ||
const outFile = `${builderName}-${refID}.dockerbuild`; | ||
const outFilename = path.join(outDir, outFile); | ||
|
||
core.info(`exporting build record to ${outFilename}`); | ||
fs.mkdirSync(outDir, {recursive: true}); | ||
|
||
const buildxInFifoPath = Context.tmpName({ | ||
template: 'buildx-in-XXXXXX.fifo', | ||
tmpdir: Context.tmpDir() | ||
}); | ||
await Exec.exec('mkfifo', [buildxInFifoPath]); | ||
|
||
const buildxOutFifoPath = Context.tmpName({ | ||
template: 'buildx-out-XXXXXX.fifo', | ||
tmpdir: Context.tmpDir() | ||
}); | ||
await Exec.exec('mkfifo', [buildxOutFifoPath]); | ||
|
||
const buildxDialStdioProc = History.spawn('docker', ['buildx', '--builder', builderName, 'dial-stdio']); | ||
fs.createReadStream(buildxInFifoPath).pipe(buildxDialStdioProc.stdin); | ||
buildxDialStdioProc.stdout.pipe(fs.createWriteStream(buildxOutFifoPath)); | ||
|
||
return new Promise<ExportRecordResponse>((resolve, reject) => { | ||
const dockerRunProc = History.spawn('docker', ['run', '-i', '-v', `${outDir}:/out`, opts.image || History.EXPORT_TOOL_IMAGE, `--ref=${refID}`, `--output=${outFile}`]); | ||
fs.createReadStream(buildxOutFifoPath).pipe(dockerRunProc.stdin); | ||
dockerRunProc.stdout.pipe(fs.createWriteStream(buildxInFifoPath)); | ||
|
||
dockerRunProc.on('close', code => { | ||
if (code === 0) { | ||
const outStats = fs.statSync(outFilename); | ||
resolve({ | ||
filename: outFilename, | ||
size: outStats.size, | ||
builderName: builderName, | ||
nodeName: nodeName, | ||
refID: refID | ||
}); | ||
} else { | ||
reject(new Error(`Process "docker run" exited with code ${code}`)); | ||
} | ||
}); | ||
|
||
dockerRunProc.on('error', err => { | ||
core.error(`Error executing buildx dial-stdio: ${err}`); | ||
reject(err); | ||
}); | ||
}); | ||
} | ||
|
||
private static spawn(command: string, args?: ReadonlyArray<string>): ChildProcessByStdio<Writable, Readable, null> { | ||
core.info(`[command]${command}${args ? ` ${args.join(' ')}` : ''}`); | ||
return spawn(command, args || [], { | ||
stdio: ['pipe', 'pipe', 'inherit'] | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Copyright 2024 actions-toolkit authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export interface ExportRecordOpts { | ||
ref: string; | ||
image?: string; | ||
} | ||
|
||
export interface ExportRecordResponse { | ||
filename: string; | ||
size: number; | ||
builderName: string; | ||
nodeName: string; | ||
refID: string; | ||
} |