Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add post-step to do graceful logout #23

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/workflows/example-v3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,18 @@ jobs:
- name: Test
run: |
oc api-resources

- name: Log out
# We fake the post state by setting this flag
# before executing the action. This should trigger logout
env:
STATE_isPost: 1
uses: ./

- name: Test logout
run: |
set -a
oc api-resources || EXIT_CODE=$?
if [[ ${EXIT_CODE} -eq 0 ]]; then
exit 1;
fi
15 changes: 15 additions & 0 deletions .github/workflows/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,18 @@ jobs:
- name: Test
run: |
oc api-resources

- name: Log out
# We fake the post state by setting this flag
# before executing the action. This should trigger logout
env:
STATE_isPost: 1
uses: ./

- name: Test logout
run: |
set -a
oc api-resources || EXIT_CODE=$?
if [[ ${EXIT_CODE} -eq 0 ]]; then
exit 1;
fi
7 changes: 6 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ inputs:
Set this to true to skip masking the cluster name."
required: false
default: "false"
logout:
description: 'Perform logout during post job cleanup'
required: false
default: "true"


runs:
using: 'node12'
using: 'node16'
main: 'dist/index.js'
post: 'dist/index.js'
4 changes: 2 additions & 2 deletions dist/index.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions dist/index.js.backup

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ namespace Auth {

await Oc.exec([ Oc.Commands.Whoami ]);
}

export async function logout(): Promise<void> {
await Oc.exec([ Oc.Commands.Logout ]);
}
}

export default Auth;
6 changes: 6 additions & 0 deletions src/generated/inputs-outputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ export enum Inputs {
* Default: "false"
*/
INSECURE_SKIP_TLS_VERIFY = "insecure_skip_tls_verify",
/**
* Perform logout during post job cleanup
* Required: false
* Default: "true"
*/
LOGOUT = "logout",
/**
* Set current context's namespace to this, after logging in.
* Required: false
Expand Down
26 changes: 21 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { Inputs } from "./generated/inputs-outputs";
import KubeConfig from "./kubeconfig";
import * as utils from "./utils";

const IsPost = !!process.env.STATE_isPost;

async function run(): Promise<void> {
ghCore.debug(`Runner OS is ${utils.getOS()}`);
ghCore.debug(`Node version is ${process.version}`);
Expand All @@ -30,8 +32,22 @@ async function run(): Promise<void> {
await KubeConfig.writeOutKubeConfig();
}

run()
.then(() => {
ghCore.info("Success.");
})
.catch(ghCore.setFailed);
async function logout(): Promise<void> {
await Auth.logout();
await KubeConfig.deleteKubeConfig();
}

if (!IsPost) {
run()
.then(() => {
ghCore.info("Success.");
})
.catch(ghCore.setFailed);
}
else {
const performLogout = ghCore.getBooleanInput(Inputs.LOGOUT);
if (performLogout) {
logout()
.catch(ghCore.setFailed);
}
}
30 changes: 22 additions & 8 deletions src/kubeconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,7 @@ namespace KubeConfig {
});
}

/**
* Write out the current kubeconfig to a new file and export the `KUBECONFIG` env var to point to that file.
* This allows other steps in the job to reuse the kubeconfig.
*/
export async function writeOutKubeConfig(): Promise<string> {
const kubeConfigContents = await getKubeConfig();

function getKubeConfigPath(): string {
// TODO make this path configurable through env or input.
let kubeConfigDir;
const ghWorkspace = process.env.GITHUB_WORKSPACE;
Expand All @@ -94,7 +88,17 @@ namespace KubeConfig {
kubeConfigDir = process.cwd();
}

const kubeConfigPath = path.resolve(kubeConfigDir, KUBECONFIG_FILENAME);
return path.resolve(kubeConfigDir, KUBECONFIG_FILENAME);
}

/**
* Write out the current kubeconfig to a new file and export the `KUBECONFIG` env var to point to that file.
* This allows other steps in the job to reuse the kubeconfig.
*/
export async function writeOutKubeConfig(): Promise<string> {
const kubeConfigContents = await getKubeConfig();

const kubeConfigPath = getKubeConfigPath();

ghCore.info(`Writing out Kubeconfig to ${kubeConfigPath}`);
await promisify(fs.writeFile)(kubeConfigPath, kubeConfigContents);
Expand All @@ -110,6 +114,16 @@ namespace KubeConfig {
return kubeConfigPath;
}

/**
* Delete residual kube_config file
*/
export async function deleteKubeConfig(): Promise<void> {
const kubeConfigPath = getKubeConfigPath();
if (await promisify(fs.exists)(kubeConfigPath)) {
await promisify(fs.unlink)(kubeConfigPath);
}
}

export async function setCurrentContextNamespace(namespace: string): Promise<void> {
const currentContext = (await Oc.exec([ Oc.Commands.Config, Oc.Commands.CurrentContext ])).output.trim();

Expand Down
1 change: 1 addition & 0 deletions src/oc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace Oc {
*/
export enum Commands {
Login = "login",
Logout = "logout",
Config = "config",
View = "view",
SetContext = "set-context",
Expand Down