@docker/actions-toolkit
Version:
Toolkit for Docker (GitHub) Actions
190 lines • 7.55 kB
JavaScript
/**
* Copyright 2025 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 fs from 'fs';
import os from 'os';
import path from 'path';
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';
import * as semver from 'semver';
import * as util from 'util';
import { Cache } from '../cache.js';
import { Context } from '../context.js';
import { Docker } from '../docker/docker.js';
import { GitHub } from '../github/github.js';
export class Install {
standalone;
githubToken;
constructor(opts) {
this.standalone = opts?.standalone;
this.githubToken = opts?.githubToken || process.env.GITHUB_TOKEN;
}
/*
* Download compose binary from GitHub release
* @param v: version semver version or latest
* @param ghaNoCache: disable binary caching in GitHub Actions cache backend
* @returns path to the compose binary
*/
async download(v, ghaNoCache) {
const version = await Install.getDownloadVersion(v);
core.debug(`Install.download version: ${version.version}`);
const release = await Install.getRelease(version, this.githubToken);
core.debug(`Install.download release tag name: ${release.tag_name}`);
const vspec = await this.vspec(release.tag_name);
core.debug(`Install.download vspec: ${vspec}`);
const c = semver.clean(vspec) || '';
if (!semver.valid(c)) {
throw new Error(`Invalid Compose version "${vspec}".`);
}
const installCache = new Cache({
htcName: version.key != 'official' ? `compose-dl-bin-${version.key}` : 'compose-dl-bin',
htcVersion: vspec,
baseCacheDir: path.join(os.homedir(), '.bin', 'docker-compose'),
cacheFile: os.platform() == 'win32' ? 'docker-compose.exe' : 'docker-compose',
ghaNoCache: ghaNoCache
});
const cacheFoundPath = await installCache.find();
if (cacheFoundPath) {
core.info(`Compose binary found in ${cacheFoundPath}`);
return cacheFoundPath;
}
const downloadURL = util.format(version.downloadURL, vspec, this.filename());
core.info(`Downloading ${downloadURL}`);
const htcDownloadPath = await tc.downloadTool(downloadURL, undefined, this.githubToken);
core.debug(`Install.download htcDownloadPath: ${htcDownloadPath}`);
const cacheSavePath = await installCache.save(htcDownloadPath);
core.info(`Cached to ${cacheSavePath}`);
return cacheSavePath;
}
async installStandalone(binPath, dest) {
core.info('Standalone mode');
dest = dest || Context.tmpDir();
const binDir = path.join(dest, 'compose-bin-standalone');
if (!fs.existsSync(binDir)) {
fs.mkdirSync(binDir, { recursive: true });
}
const binName = os.platform() == 'win32' ? 'compose.exe' : 'compose';
const composePath = path.join(binDir, binName);
fs.copyFileSync(binPath, composePath);
core.info('Fixing perms');
fs.chmodSync(composePath, '0755');
core.addPath(binDir);
core.info('Added Compose to PATH');
core.info(`Binary path: ${composePath}`);
return composePath;
}
async installPlugin(binPath, dest) {
core.info('Docker plugin mode');
dest = dest || Docker.configDir;
const pluginsDir = path.join(dest, 'cli-plugins');
if (!fs.existsSync(pluginsDir)) {
fs.mkdirSync(pluginsDir, { recursive: true });
}
const binName = os.platform() == 'win32' ? 'docker-compose.exe' : 'docker-compose';
const pluginPath = path.join(pluginsDir, binName);
fs.copyFileSync(binPath, pluginPath);
core.info('Fixing perms');
fs.chmodSync(pluginPath, '0755');
core.info(`Plugin path: ${pluginPath}`);
return pluginPath;
}
async isStandalone() {
const standalone = this.standalone ?? !(await Docker.isAvailable());
core.debug(`Install.isStandalone: ${standalone}`);
return standalone;
}
filename() {
let arch;
switch (os.arch()) {
case 'x64': {
arch = 'x86_64';
break;
}
case 'ppc64': {
arch = 'ppc64le';
break;
}
case 'arm': {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const arm_version = process.config.variables.arm_version;
arch = arm_version ? 'armv' + arm_version : 'arm';
break;
}
case 'arm64': {
arch = 'aarch64';
break;
}
default: {
arch = os.arch();
break;
}
}
const platform = os.platform() == 'win32' ? 'windows' : os.platform();
const ext = os.platform() == 'win32' ? '.exe' : '';
return util.format('docker-compose-%s-%s%s', platform, arch, ext);
}
async vspec(version) {
const v = version.replace(/^v+|v+$/g, '');
core.info(`Use ${v} version spec cache key for ${version}`);
return v;
}
static async getDownloadVersion(v) {
let [repoKey, version] = v.split(':');
if (!version) {
version = repoKey;
repoKey = 'official';
}
switch (repoKey) {
case 'official': {
return {
key: repoKey,
version: version,
downloadURL: 'https://github.com/docker/compose/releases/download/v%s/%s',
contentOpts: {
owner: 'docker',
repo: 'actions-toolkit',
ref: 'main',
path: '.github/compose-releases.json'
}
};
}
case 'cloud': {
return {
key: repoKey,
version: version,
downloadURL: 'https://github.com/docker/compose-desktop/releases/download/v%s/%s',
contentOpts: {
owner: 'docker',
repo: 'actions-toolkit',
ref: 'main',
path: '.github/compose-lab-releases.json'
}
};
}
default: {
throw new Error(`Cannot find compose version for ${v}`);
}
}
}
static async getRelease(version, githubToken) {
const github = new GitHub({ token: githubToken });
const releases = await github.releases('Compose', version.contentOpts);
if (!releases[version.version]) {
throw new Error(`Cannot find Compose release ${version.version} in releases JSON`);
}
return releases[version.version];
}
}
//# sourceMappingURL=install.js.map