fix: use @actions/core.exportVariable instead of set-env (#18)
`set-env` is now deprecated. `@actions/core` exposes `exportVariable` to be used instead. https://github.com/actions/toolkit/tree/main/packages/core#exporting-variables
This commit is contained in:
parent
c70c4ca5c2
commit
2b166600e7
244
lib/login.js
244
lib/login.js
@ -1,122 +1,122 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||||
return new (P || (P = Promise))(function (resolve, reject) {
|
return new (P || (P = Promise))(function (resolve, reject) {
|
||||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
||||||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
||||||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||||||
const core = require("@actions/core");
|
exports.run = void 0;
|
||||||
const command_1 = require("@actions/core/lib/command");
|
const core = require("@actions/core");
|
||||||
const path = require("path");
|
const path = require("path");
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const io = require("@actions/io");
|
const io = require("@actions/io");
|
||||||
const toolCache = require("@actions/tool-cache");
|
const toolCache = require("@actions/tool-cache");
|
||||||
const os = require("os");
|
const os = require("os");
|
||||||
const toolrunner_1 = require("@actions/exec/lib/toolrunner");
|
const toolrunner_1 = require("@actions/exec/lib/toolrunner");
|
||||||
const jsyaml = require("js-yaml");
|
const jsyaml = require("js-yaml");
|
||||||
const util = require("util");
|
const util = require("util");
|
||||||
function getKubeconfig() {
|
function getKubeconfig() {
|
||||||
const method = core.getInput('method', { required: true });
|
const method = core.getInput('method', { required: true });
|
||||||
if (method == 'kubeconfig') {
|
if (method == 'kubeconfig') {
|
||||||
const kubeconfig = core.getInput('kubeconfig', { required: true });
|
const kubeconfig = core.getInput('kubeconfig', { required: true });
|
||||||
core.debug("Setting context using kubeconfig");
|
core.debug("Setting context using kubeconfig");
|
||||||
return kubeconfig;
|
return kubeconfig;
|
||||||
}
|
}
|
||||||
else if (method == 'service-account') {
|
else if (method == 'service-account') {
|
||||||
const clusterUrl = core.getInput('k8s-url', { required: true });
|
const clusterUrl = core.getInput('k8s-url', { required: true });
|
||||||
core.debug("Found clusterUrl, creating kubeconfig using certificate and token");
|
core.debug("Found clusterUrl, creating kubeconfig using certificate and token");
|
||||||
let k8sSecret = core.getInput('k8s-secret', { required: true });
|
let k8sSecret = core.getInput('k8s-secret', { required: true });
|
||||||
var parsedk8sSecret = jsyaml.safeLoad(k8sSecret);
|
var parsedk8sSecret = jsyaml.safeLoad(k8sSecret);
|
||||||
let kubernetesServiceAccountSecretFieldNotPresent = 'The service acount secret yaml does not contain %s; field. Make sure that its present and try again.';
|
let kubernetesServiceAccountSecretFieldNotPresent = 'The service acount secret yaml does not contain %s; field. Make sure that its present and try again.';
|
||||||
if (!parsedk8sSecret) {
|
if (!parsedk8sSecret) {
|
||||||
throw Error("The service account secret yaml specified is invalid. Make sure that its a valid yaml and try again.");
|
throw Error("The service account secret yaml specified is invalid. Make sure that its a valid yaml and try again.");
|
||||||
}
|
}
|
||||||
if (!parsedk8sSecret.data) {
|
if (!parsedk8sSecret.data) {
|
||||||
throw Error(util.format(kubernetesServiceAccountSecretFieldNotPresent, "data"));
|
throw Error(util.format(kubernetesServiceAccountSecretFieldNotPresent, "data"));
|
||||||
}
|
}
|
||||||
if (!parsedk8sSecret.data.token) {
|
if (!parsedk8sSecret.data.token) {
|
||||||
throw Error(util.format(kubernetesServiceAccountSecretFieldNotPresent, "data.token"));
|
throw Error(util.format(kubernetesServiceAccountSecretFieldNotPresent, "data.token"));
|
||||||
}
|
}
|
||||||
if (!parsedk8sSecret.data["ca.crt"]) {
|
if (!parsedk8sSecret.data["ca.crt"]) {
|
||||||
throw Error(util.format(kubernetesServiceAccountSecretFieldNotPresent, "data[ca.crt]"));
|
throw Error(util.format(kubernetesServiceAccountSecretFieldNotPresent, "data[ca.crt]"));
|
||||||
}
|
}
|
||||||
const certAuth = parsedk8sSecret.data["ca.crt"];
|
const certAuth = parsedk8sSecret.data["ca.crt"];
|
||||||
const token = Buffer.from(parsedk8sSecret.data.token, 'base64').toString();
|
const token = Buffer.from(parsedk8sSecret.data.token, 'base64').toString();
|
||||||
const kubeconfigObject = {
|
const kubeconfigObject = {
|
||||||
"apiVersion": "v1",
|
"apiVersion": "v1",
|
||||||
"kind": "Config",
|
"kind": "Config",
|
||||||
"clusters": [
|
"clusters": [
|
||||||
{
|
{
|
||||||
"cluster": {
|
"cluster": {
|
||||||
"certificate-authority-data": certAuth,
|
"certificate-authority-data": certAuth,
|
||||||
"server": clusterUrl
|
"server": clusterUrl
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"users": [
|
"users": [
|
||||||
{
|
{
|
||||||
"user": {
|
"user": {
|
||||||
"token": token
|
"token": token
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
return JSON.stringify(kubeconfigObject);
|
return JSON.stringify(kubeconfigObject);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw Error("Invalid method specified. Acceptable values are kubeconfig and service-account.");
|
throw Error("Invalid method specified. Acceptable values are kubeconfig and service-account.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function getExecutableExtension() {
|
function getExecutableExtension() {
|
||||||
if (os.type().match(/^Win/)) {
|
if (os.type().match(/^Win/)) {
|
||||||
return '.exe';
|
return '.exe';
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
function getKubectlPath() {
|
function getKubectlPath() {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
let kubectlPath = yield io.which('kubectl', false);
|
let kubectlPath = yield io.which('kubectl', false);
|
||||||
if (!kubectlPath) {
|
if (!kubectlPath) {
|
||||||
const allVersions = toolCache.findAllVersions('kubectl');
|
const allVersions = toolCache.findAllVersions('kubectl');
|
||||||
kubectlPath = allVersions.length > 0 ? toolCache.find('kubectl', allVersions[0]) : '';
|
kubectlPath = allVersions.length > 0 ? toolCache.find('kubectl', allVersions[0]) : '';
|
||||||
if (!kubectlPath) {
|
if (!kubectlPath) {
|
||||||
throw new Error('Kubectl is not installed');
|
throw new Error('Kubectl is not installed');
|
||||||
}
|
}
|
||||||
kubectlPath = path.join(kubectlPath, `kubectl${getExecutableExtension()}`);
|
kubectlPath = path.join(kubectlPath, `kubectl${getExecutableExtension()}`);
|
||||||
}
|
}
|
||||||
return kubectlPath;
|
return kubectlPath;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
function setContext(kubeconfigPath) {
|
function setContext(kubeconfigPath) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
let context = core.getInput('context');
|
let context = core.getInput('context');
|
||||||
if (context) {
|
if (context) {
|
||||||
//To use kubectl commands, the environment variable KUBECONFIG needs to be set for this step
|
//To use kubectl commands, the environment variable KUBECONFIG needs to be set for this step
|
||||||
process.env['KUBECONFIG'] = kubeconfigPath;
|
process.env['KUBECONFIG'] = kubeconfigPath;
|
||||||
const kubectlPath = yield getKubectlPath();
|
const kubectlPath = yield getKubectlPath();
|
||||||
let toolRunner = new toolrunner_1.ToolRunner(kubectlPath, ['config', 'use-context', context]);
|
let toolRunner = new toolrunner_1.ToolRunner(kubectlPath, ['config', 'use-context', context]);
|
||||||
yield toolRunner.exec();
|
yield toolRunner.exec();
|
||||||
toolRunner = new toolrunner_1.ToolRunner(kubectlPath, ['config', 'current-context']);
|
toolRunner = new toolrunner_1.ToolRunner(kubectlPath, ['config', 'current-context']);
|
||||||
yield toolRunner.exec();
|
yield toolRunner.exec();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
function run() {
|
function run() {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
let kubeconfig = getKubeconfig();
|
let kubeconfig = getKubeconfig();
|
||||||
const runnerTempDirectory = process.env['RUNNER_TEMP']; // Using process.env until the core libs are updated
|
const runnerTempDirectory = process.env['RUNNER_TEMP']; // Using process.env until the core libs are updated
|
||||||
const kubeconfigPath = path.join(runnerTempDirectory, `kubeconfig_${Date.now()}`);
|
const kubeconfigPath = path.join(runnerTempDirectory, `kubeconfig_${Date.now()}`);
|
||||||
core.debug(`Writing kubeconfig contents to ${kubeconfigPath}`);
|
core.debug(`Writing kubeconfig contents to ${kubeconfigPath}`);
|
||||||
fs.writeFileSync(kubeconfigPath, kubeconfig);
|
fs.writeFileSync(kubeconfigPath, kubeconfig);
|
||||||
fs.chmodSync(kubeconfigPath, '600');
|
fs.chmodSync(kubeconfigPath, '600');
|
||||||
command_1.issueCommand('set-env', { name: 'KUBECONFIG' }, kubeconfigPath);
|
core.exportVariable('KUBECONFIG', kubeconfigPath);
|
||||||
console.log('KUBECONFIG environment variable is set');
|
console.log('KUBECONFIG environment variable is set');
|
||||||
yield setContext(kubeconfigPath);
|
yield setContext(kubeconfigPath);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
exports.run = run;
|
exports.run = run;
|
||||||
run().catch(core.setFailed);
|
run().catch(core.setFailed);
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import * as core from '@actions/core';
|
import * as core from '@actions/core';
|
||||||
import { issueCommand } from '@actions/core/lib/command';
|
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
import * as io from '@actions/io';
|
import * as io from '@actions/io';
|
||||||
@ -109,7 +108,7 @@ export async function run() {
|
|||||||
core.debug(`Writing kubeconfig contents to ${kubeconfigPath}`);
|
core.debug(`Writing kubeconfig contents to ${kubeconfigPath}`);
|
||||||
fs.writeFileSync(kubeconfigPath, kubeconfig);
|
fs.writeFileSync(kubeconfigPath, kubeconfig);
|
||||||
fs.chmodSync(kubeconfigPath, '600');
|
fs.chmodSync(kubeconfigPath, '600');
|
||||||
issueCommand('set-env', { name: 'KUBECONFIG' }, kubeconfigPath);
|
core.exportVariable('KUBECONFIG', kubeconfigPath);
|
||||||
console.log('KUBECONFIG environment variable is set');
|
console.log('KUBECONFIG environment variable is set');
|
||||||
await setContext(kubeconfigPath);
|
await setContext(kubeconfigPath);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user