2021-03-30 18:57:12 +05:30
|
|
|
/***************************************************************************************************
|
|
|
|
* Copyright (c) Red Hat, Inc. All rights reserved.
|
|
|
|
* Licensed under the MIT License. See LICENSE file in the project root for license information.
|
|
|
|
**************************************************************************************************/
|
|
|
|
|
|
|
|
import * as core from "@actions/core";
|
2021-11-30 20:41:07 +05:30
|
|
|
import { promises as fs } from "fs";
|
2021-03-30 18:57:12 +05:30
|
|
|
import * as io from "@actions/io";
|
|
|
|
import * as os from "os";
|
2021-08-20 19:19:14 +05:30
|
|
|
import * as path from "path";
|
2021-11-30 20:41:07 +05:30
|
|
|
import { execute, getDockerConfigJson } from "./utils";
|
2021-03-31 14:31:58 +05:30
|
|
|
import * as stateHelper from "./state-helper";
|
2021-11-30 20:41:07 +05:30
|
|
|
import { Inputs } from "./generated/inputs-outputs";
|
2021-03-30 18:57:12 +05:30
|
|
|
|
|
|
|
let podmanPath: string | undefined;
|
2021-11-30 20:41:07 +05:30
|
|
|
let registry: string;
|
|
|
|
const dockerConfigPath = path.join(os.homedir(), ".docker", "config.json");
|
2021-03-30 18:57:12 +05:30
|
|
|
|
|
|
|
async function getPodmanPath(): Promise<string> {
|
|
|
|
if (podmanPath == null) {
|
|
|
|
podmanPath = await io.which("podman", true);
|
2021-04-07 12:58:15 -04:00
|
|
|
await execute(podmanPath, [ "version" ], { group: true });
|
2021-03-30 18:57:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return podmanPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function run(): Promise<void> {
|
|
|
|
if (os.platform() !== "linux") {
|
2021-03-31 14:31:58 +05:30
|
|
|
throw new Error("❌ Only supported on linux platform");
|
2021-03-30 18:57:12 +05:30
|
|
|
}
|
|
|
|
|
2021-11-30 20:41:07 +05:30
|
|
|
registry = core.getInput(Inputs.REGISTRY, { required: true });
|
|
|
|
const username = core.getInput(Inputs.USERNAME, { required: true });
|
|
|
|
const password = core.getInput(Inputs.PASSWORD, { required: true });
|
|
|
|
const logout = core.getInput(Inputs.LOGOUT) || "true";
|
2021-03-30 22:45:50 +05:30
|
|
|
|
2021-03-31 14:31:58 +05:30
|
|
|
stateHelper.setRegistry(registry);
|
2021-03-30 22:45:50 +05:30
|
|
|
stateHelper.setLogout(logout);
|
2021-03-30 18:57:12 +05:30
|
|
|
|
|
|
|
const args = [
|
|
|
|
"login",
|
|
|
|
registry,
|
|
|
|
"-u",
|
|
|
|
username,
|
|
|
|
"-p",
|
|
|
|
password,
|
|
|
|
];
|
2021-03-30 22:45:50 +05:30
|
|
|
await execute(await getPodmanPath(), args);
|
2021-04-07 12:58:15 -04:00
|
|
|
core.info(`✅ Successfully logged in to ${registry} as ${username}`);
|
2021-08-20 19:19:14 +05:30
|
|
|
|
|
|
|
// Setting REGISTRY_AUTH_FILE environment variable as buildah needs
|
|
|
|
// this environment variable to point to registry auth file
|
2021-10-20 00:19:25 +05:30
|
|
|
let authFileDir = path.join("/", "tmp", `podman-run-${process.getuid()}`);
|
|
|
|
if (process.env.XDG_RUNTIME_DIR) {
|
|
|
|
authFileDir = process.env.XDG_RUNTIME_DIR;
|
|
|
|
}
|
|
|
|
const podmanAuthFilePath = path.join(authFileDir,
|
2021-08-20 19:19:14 +05:30
|
|
|
"containers", "auth.json");
|
|
|
|
const REGISTRY_AUTH_ENVVAR = "REGISTRY_AUTH_FILE";
|
|
|
|
core.info(`Exporting ${REGISTRY_AUTH_ENVVAR}=${podmanAuthFilePath}`);
|
|
|
|
core.exportVariable(REGISTRY_AUTH_ENVVAR, podmanAuthFilePath);
|
2021-11-30 20:41:07 +05:30
|
|
|
|
|
|
|
const podmanConfigJson = await fs.readFile(podmanAuthFilePath, "utf-8");
|
|
|
|
const podmanConfig = JSON.parse(podmanConfigJson);
|
|
|
|
const generatedAuth = podmanConfig.auths[registry];
|
|
|
|
|
|
|
|
core.info(`✍️ Writing registry credentials to "${dockerConfigPath}"`);
|
|
|
|
const dockerConfig = JSON.parse(await getDockerConfigJson());
|
|
|
|
|
|
|
|
dockerConfig.auths[registry] = generatedAuth;
|
|
|
|
|
|
|
|
await fs.writeFile(dockerConfigPath, JSON.stringify(dockerConfig, undefined, 8), "utf-8");
|
2021-03-30 18:57:12 +05:30
|
|
|
}
|
|
|
|
|
2021-03-31 23:54:57 +05:30
|
|
|
async function registryLogout(): Promise<void> {
|
2021-03-30 22:45:50 +05:30
|
|
|
if (!stateHelper.logout) {
|
|
|
|
return;
|
2021-03-31 14:31:58 +05:30
|
|
|
}
|
|
|
|
await execute(await getPodmanPath(), [ "logout", stateHelper.registry ]);
|
2021-11-30 20:41:07 +05:30
|
|
|
|
|
|
|
const dockerConfig = JSON.parse(await getDockerConfigJson());
|
|
|
|
core.info(`Removing registry credentials from "${dockerConfigPath}"`);
|
|
|
|
delete dockerConfig.auths[registry];
|
|
|
|
await fs.writeFile(dockerConfigPath, JSON.stringify(dockerConfig, undefined, 8), "utf-8");
|
2021-03-30 22:45:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (!stateHelper.IsPost) {
|
2021-03-31 14:31:58 +05:30
|
|
|
run().catch(core.setFailed);
|
2021-03-30 22:45:50 +05:30
|
|
|
}
|
|
|
|
else {
|
2021-03-31 23:54:57 +05:30
|
|
|
registryLogout().catch(core.setFailed);
|
2021-03-30 22:45:50 +05:30
|
|
|
}
|