|
| 1 | +/** |
| 2 | + * Copyright 2024 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 {Cache} from '../cache'; |
| 27 | +import {Context} from '../context'; |
| 28 | + |
| 29 | +import {GitHubRelease} from '../types/github'; |
| 30 | +import {DownloadVersion} from '../types/undock/undock'; |
| 31 | + |
| 32 | +export class Install { |
| 33 | + /* |
| 34 | + * Download undock binary from GitHub release |
| 35 | + * @param v: version semver version or latest |
| 36 | + * @param ghaNoCache: disable binary caching in GitHub Actions cache backend |
| 37 | + * @returns path to the undock binary |
| 38 | + */ |
| 39 | + public async download(v: string, ghaNoCache?: boolean): Promise<string> { |
| 40 | + const version: DownloadVersion = await Install.getDownloadVersion(v); |
| 41 | + core.debug(`Install.download version: ${version.version}`); |
| 42 | + |
| 43 | + const release: GitHubRelease = await Install.getRelease(version); |
| 44 | + core.debug(`Install.download release tag name: ${release.tag_name}`); |
| 45 | + |
| 46 | + const vspec = await this.vspec(release.tag_name); |
| 47 | + core.debug(`Install.download vspec: ${vspec}`); |
| 48 | + |
| 49 | + const c = semver.clean(vspec) || ''; |
| 50 | + if (!semver.valid(c)) { |
| 51 | + throw new Error(`Invalid Undock version "${vspec}".`); |
| 52 | + } |
| 53 | + |
| 54 | + const installCache = new Cache({ |
| 55 | + htcName: 'undock-dl-bin', |
| 56 | + htcVersion: vspec, |
| 57 | + baseCacheDir: path.join(os.homedir(), '.bin'), |
| 58 | + cacheFile: os.platform() == 'win32' ? 'undock.exe' : 'undock', |
| 59 | + ghaNoCache: ghaNoCache |
| 60 | + }); |
| 61 | + |
| 62 | + const cacheFoundPath = await installCache.find(); |
| 63 | + if (cacheFoundPath) { |
| 64 | + core.info(`Unodck binary found in ${cacheFoundPath}`); |
| 65 | + return cacheFoundPath; |
| 66 | + } |
| 67 | + |
| 68 | + const downloadURL = util.format(version.downloadURL, vspec, this.filename(vspec)); |
| 69 | + core.info(`Downloading ${downloadURL}`); |
| 70 | + |
| 71 | + const htcDownloadPath = await tc.downloadTool(downloadURL); |
| 72 | + core.debug(`Install.download htcDownloadPath: ${htcDownloadPath}`); |
| 73 | + |
| 74 | + let htcExtPath: string; |
| 75 | + if (os.platform() == 'win32') { |
| 76 | + htcExtPath = await tc.extractZip(htcDownloadPath); |
| 77 | + } else { |
| 78 | + htcExtPath = await tc.extractTar(htcDownloadPath); |
| 79 | + } |
| 80 | + core.info(`Extracted to ${htcExtPath}`); |
| 81 | + |
| 82 | + const exePath: string = path.join(htcExtPath, os.platform() == 'win32' ? 'undock.exe' : 'undock'); |
| 83 | + core.debug(`Install.download exePath: ${exePath}`); |
| 84 | + |
| 85 | + const cacheSavePath = await installCache.save(exePath); |
| 86 | + core.info(`Cached to ${cacheSavePath}`); |
| 87 | + return cacheSavePath; |
| 88 | + } |
| 89 | + |
| 90 | + public async install(binPath: string, dest?: string): Promise<string> { |
| 91 | + dest = dest || Context.tmpDir(); |
| 92 | + |
| 93 | + const binDir = path.join(dest, 'undock-bin'); |
| 94 | + if (!fs.existsSync(binDir)) { |
| 95 | + fs.mkdirSync(binDir, {recursive: true}); |
| 96 | + } |
| 97 | + const binName: string = os.platform() == 'win32' ? 'undock.exe' : 'undock'; |
| 98 | + const undockPath: string = path.join(binDir, binName); |
| 99 | + fs.copyFileSync(binPath, undockPath); |
| 100 | + |
| 101 | + core.info('Fixing perms'); |
| 102 | + fs.chmodSync(undockPath, '0755'); |
| 103 | + |
| 104 | + core.addPath(binDir); |
| 105 | + core.info('Added Unodck to PATH'); |
| 106 | + |
| 107 | + core.info(`Binary path: ${undockPath}`); |
| 108 | + return undockPath; |
| 109 | + } |
| 110 | + |
| 111 | + private filename(version: string): string { |
| 112 | + let arch: string; |
| 113 | + switch (os.arch()) { |
| 114 | + case 'x64': { |
| 115 | + arch = 'amd64'; |
| 116 | + break; |
| 117 | + } |
| 118 | + case 'ppc64': { |
| 119 | + arch = 'ppc64le'; |
| 120 | + break; |
| 121 | + } |
| 122 | + case 'arm': { |
| 123 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 124 | + const arm_version = (process.config.variables as any).arm_version; |
| 125 | + arch = arm_version ? 'armv' + arm_version : 'arm'; |
| 126 | + break; |
| 127 | + } |
| 128 | + default: { |
| 129 | + arch = os.arch(); |
| 130 | + break; |
| 131 | + } |
| 132 | + } |
| 133 | + const platform: string = os.platform() == 'win32' ? 'windows' : os.platform(); |
| 134 | + const ext: string = os.platform() == 'win32' ? '.zip' : '.tar.gz'; |
| 135 | + return util.format('undock_%s_%s_%s%s', version, platform, arch, ext); |
| 136 | + } |
| 137 | + |
| 138 | + private async vspec(version: string): Promise<string> { |
| 139 | + const v = version.replace(/^v+|v+$/g, ''); |
| 140 | + core.info(`Use ${v} version spec cache key for ${version}`); |
| 141 | + return v; |
| 142 | + } |
| 143 | + |
| 144 | + public static async getDownloadVersion(v: string): Promise<DownloadVersion> { |
| 145 | + return { |
| 146 | + version: v, |
| 147 | + downloadURL: 'https://github.com/crazy-max/undock/releases/download/v%s/%s', |
| 148 | + releasesURL: 'https://raw.githubusercontent.com/docker/actions-toolkit/main/.github/undock-releases.json' |
| 149 | + }; |
| 150 | + } |
| 151 | + |
| 152 | + public static async getRelease(version: DownloadVersion): Promise<GitHubRelease> { |
| 153 | + const http: httpm.HttpClient = new httpm.HttpClient('docker-actions-toolkit'); |
| 154 | + const resp: httpm.HttpClientResponse = await http.get(version.releasesURL); |
| 155 | + const body = await resp.readBody(); |
| 156 | + const statusCode = resp.message.statusCode || 500; |
| 157 | + if (statusCode >= 400) { |
| 158 | + throw new Error(`Failed to get Undock releases from ${version.releasesURL} with status code ${statusCode}: ${body}`); |
| 159 | + } |
| 160 | + const releases = <Record<string, GitHubRelease>>JSON.parse(body); |
| 161 | + if (!releases[version.version]) { |
| 162 | + throw new Error(`Cannot find Undock release ${version.version} in ${version.releasesURL}`); |
| 163 | + } |
| 164 | + return releases[version.version]; |
| 165 | + } |
| 166 | +} |
0 commit comments