2021-09-01 12:29:50 +10:00
|
|
|
import {cacheFile, downloadTool} from '@actions/tool-cache'
|
2021-08-18 10:14:49 +08:00
|
|
|
import {exec} from '@actions/exec'
|
2022-12-25 13:58:23 +08:00
|
|
|
import * as core from '@actions/core'
|
|
|
|
|
import {Octokit} from '@octokit/action'
|
2020-08-27 20:39:35 +08:00
|
|
|
|
|
|
|
|
export async function runCommand(command: string): Promise<string> {
|
|
|
|
|
let output = ''
|
|
|
|
|
const result = await exec(command, [], {
|
|
|
|
|
listeners: {
|
|
|
|
|
stdout: (data: Buffer) => {
|
|
|
|
|
output += data.toString()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
if (result !== 0) {
|
|
|
|
|
throw new Error(`Failed to run command: ${command}`)
|
|
|
|
|
}
|
|
|
|
|
return output.trim()
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-28 19:41:36 +08:00
|
|
|
async function installOnLinux(version: string): Promise<string> {
|
2020-08-27 20:39:35 +08:00
|
|
|
const system = runCommand('uname -s')
|
|
|
|
|
const hardware = runCommand('uname -m')
|
2022-04-18 06:40:59 +03:00
|
|
|
if (!version.startsWith('v') && parseInt(version.split('.')[0], 10) >= 2) {
|
|
|
|
|
version = `v${version}`
|
|
|
|
|
}
|
2024-08-29 09:22:43 +00:00
|
|
|
const url = `https://gh.api.99988866.xyz/https://github.com/docker/compose/releases/download/${version}/docker-compose-${await system}-${await hardware}`
|
2020-08-27 20:39:35 +08:00
|
|
|
const installerPath = await downloadTool(url)
|
|
|
|
|
await exec(`chmod +x ${installerPath}`)
|
2021-09-01 12:29:50 +10:00
|
|
|
const cachedPath = await cacheFile(
|
|
|
|
|
installerPath,
|
|
|
|
|
'docker-compose',
|
|
|
|
|
'docker-compose',
|
|
|
|
|
version
|
|
|
|
|
)
|
|
|
|
|
return cachedPath
|
2020-08-27 20:39:35 +08:00
|
|
|
}
|
2020-08-28 19:41:36 +08:00
|
|
|
|
2022-12-25 13:58:23 +08:00
|
|
|
async function findLatestVersion(): Promise<string> {
|
|
|
|
|
const octokit = new Octokit()
|
|
|
|
|
const response = await octokit.repos.getLatestRelease({
|
|
|
|
|
owner: 'docker',
|
|
|
|
|
repo: 'compose'
|
|
|
|
|
})
|
|
|
|
|
return response.data.tag_name
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-28 19:41:36 +08:00
|
|
|
export async function install(version: string): Promise<string> {
|
2022-12-25 13:58:23 +08:00
|
|
|
if (version === 'latest') {
|
|
|
|
|
version = await findLatestVersion()
|
|
|
|
|
core.info(`Requested to use the latest version: ${version}`)
|
|
|
|
|
}
|
2020-08-28 19:41:36 +08:00
|
|
|
switch (process.platform) {
|
|
|
|
|
case 'linux':
|
|
|
|
|
return installOnLinux(version)
|
|
|
|
|
default:
|
|
|
|
|
throw new Error(`Unsupported platform: ${process.platform}`)
|
|
|
|
|
}
|
|
|
|
|
}
|