From 36d16a0abb8df8d773a86f097594be337db4cf88 Mon Sep 17 00:00:00 2001 From: mehdi-ra Date: Mon, 24 Jul 2023 15:34:01 +0330 Subject: [PATCH] feat(kubectl-set-context): now the action will set context if passed any --- action.yml | 13 +++++++++++-- src/handlers/deploy-helm.ts | 2 ++ src/handlers/kubectl-set-context.ts | 23 +++++++++++++++++++++++ src/handlers/setup-kubectl-config.ts | 2 +- src/main.ts | 10 ++++++---- 5 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 src/handlers/kubectl-set-context.ts diff --git a/action.yml b/action.yml index bd326af..a7f2302 100644 --- a/action.yml +++ b/action.yml @@ -7,9 +7,8 @@ inputs: description: 'The generic chart address' context: - required: true + required: false description: 'The kubernetes target context' - default: 'default' token: required: true @@ -19,11 +18,21 @@ inputs: required: true description: 'Kubernetes config file to connect' + releaseName: + required: false + description: 'The name of your release' + default: ${{ github.repository.name }} + valuesPath: required: false description: 'The values path to use it with generic chart' default: '.helm/values.yaml' + namespace: + required: false + description: 'The namespace you want to deploy your services' + default: 'default' + runs: using: 'node16' main: 'dist/index.js' diff --git a/src/handlers/deploy-helm.ts b/src/handlers/deploy-helm.ts index 2607e76..49b240a 100644 --- a/src/handlers/deploy-helm.ts +++ b/src/handlers/deploy-helm.ts @@ -18,4 +18,6 @@ export async function deployHelmChart( namespace: string = 'default' ): Promise { info('deploying works') + + execSync('kubectl get pods') } diff --git a/src/handlers/kubectl-set-context.ts b/src/handlers/kubectl-set-context.ts new file mode 100644 index 0000000..b24272e --- /dev/null +++ b/src/handlers/kubectl-set-context.ts @@ -0,0 +1,23 @@ +import {execSync} from 'child_process' +import {repositoryDirectory} from '../constants/repositoryDirectory' +import * as core from '@actions/core' +import {errorHandler} from '../helpers/error-handler' + +export async function setContext(context?: string) { + try { + if (!context) { + return + } + + core.info('Configuring context') + execSync(`kubectl config set-context ${context}`, { + stdio: 'inherit', + cwd: repositoryDirectory + }) + + core.info(`Context successfully is set to ${context}`) + } catch (error) { + core.error('error in setting context') + errorHandler(error) + } +} diff --git a/src/handlers/setup-kubectl-config.ts b/src/handlers/setup-kubectl-config.ts index a5f13d6..bc1e35c 100644 --- a/src/handlers/setup-kubectl-config.ts +++ b/src/handlers/setup-kubectl-config.ts @@ -11,7 +11,7 @@ export async function setupKubectlConfig(kubeConfig: string) { throw new Error('No kubeConfig provided') } - core.info('Preparing the kubeconfig file') + core.info('Preparing the kubeConfig file') await writeFile('kubeconfig', kubeConfig) execSync( diff --git a/src/main.ts b/src/main.ts index aa83979..9f75523 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,6 +5,7 @@ import {setupKubectlConfig} from './handlers/setup-kubectl-config' import {execSync} from 'child_process' import {repositoryDirectory} from './constants/repositoryDirectory' import {errorHandler} from './helpers/error-handler' +import {deployHelmChart} from './handlers/deploy-helm' async function run(): Promise { try { @@ -13,8 +14,10 @@ async function run(): Promise { trimWhitespace: true }) - const valuesPath = core.getInput('valuesPath', {required: true}) - const context = core.getInput('context', {required: true}) + const valuesPath = core.getInput('valuesPath') + const releaseName = core.getInput('releaseName') + const namespace = core.getInput('namespace') + const context = core.getInput('context', {required: false}) const token = core.getInput('token', {required: true}) const kubeConfig = core.getInput('kubeConfig', {required: true}) @@ -23,8 +26,7 @@ async function run(): Promise { await installKubectl() await setupKubectlConfig(kubeConfig) await installHelm() - - execSync(`ls -lha`, {stdio: 'inherit', cwd: repositoryDirectory}) + await deployHelmChart(releaseName, genericChart, namespace) return } catch (error) {