podman-login/src/index.ts

75 lines
2.3 KiB
TypeScript
Raw Normal View History

/***************************************************************************************************
* 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";
import * as io from "@actions/io";
import * as os from "os";
import * as path from "path";
import { getInputs } from "./context";
import { execute } from "./utils";
import * as stateHelper from "./state-helper";
let podmanPath: string | undefined;
async function getPodmanPath(): Promise<string> {
if (podmanPath == null) {
podmanPath = await io.which("podman", true);
await execute(podmanPath, [ "version" ], { group: true });
}
return podmanPath;
}
async function run(): Promise<void> {
if (os.platform() !== "linux") {
throw new Error("❌ Only supported on linux platform");
}
const {
registry, username, password, logout,
} = getInputs();
stateHelper.setRegistry(registry);
stateHelper.setLogout(logout);
const args = [
"login",
registry,
"-u",
username,
"-p",
password,
];
await execute(await getPodmanPath(), args);
core.info(`✅ Successfully logged in to ${registry} as ${username}`);
// Setting REGISTRY_AUTH_FILE environment variable as buildah needs
// this environment variable to point to registry auth file
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,
"containers", "auth.json");
const REGISTRY_AUTH_ENVVAR = "REGISTRY_AUTH_FILE";
core.info(`Exporting ${REGISTRY_AUTH_ENVVAR}=${podmanAuthFilePath}`);
core.exportVariable(REGISTRY_AUTH_ENVVAR, podmanAuthFilePath);
}
async function registryLogout(): Promise<void> {
if (!stateHelper.logout) {
return;
}
await execute(await getPodmanPath(), [ "logout", stateHelper.registry ]);
}
if (!stateHelper.IsPost) {
run().catch(core.setFailed);
}
else {
registryLogout().catch(core.setFailed);
}