From 094d4f875eb3955ed411b2a98e6ec2a5eafcf600 Mon Sep 17 00:00:00 2001 From: mehdi-ra Date: Tue, 25 Jul 2023 12:24:59 +0330 Subject: [PATCH] feat(deploy-helm): added deploy command --- action.yml | 14 +++++++----- src/handlers/add-helm-repo.ts | 30 +++++++++++++++++++++++++ src/handlers/deploy-helm.ts | 42 ++++++++++++++++++++++------------- src/main.ts | 26 ++++++++++++++++------ 4 files changed, 84 insertions(+), 28 deletions(-) create mode 100644 src/handlers/add-helm-repo.ts diff --git a/action.yml b/action.yml index a7f2302..8c4cfe2 100644 --- a/action.yml +++ b/action.yml @@ -2,18 +2,22 @@ name: 'Helm releaser' description: 'Combination of a big verity of features for deploying helm chart' author: 'devopshobbies' inputs: - genericChart: + remoteRepository: required: true description: 'The generic chart address' + chartName: + required: true + description: 'The chart name to get from remote chart' + + chartVersion: + required: true + description: 'The version of your chart' + context: required: false description: 'The kubernetes target context' - token: - required: true - description: 'Github token' - kubeConfig: required: true description: 'Kubernetes config file to connect' diff --git a/src/handlers/add-helm-repo.ts b/src/handlers/add-helm-repo.ts new file mode 100644 index 0000000..6050fdd --- /dev/null +++ b/src/handlers/add-helm-repo.ts @@ -0,0 +1,30 @@ +import {execSync} from 'child_process' +import {repositoryDirectory} from '../constants/repositoryDirectory' +import * as core from '@actions/core' + +/** + * This will adds and updates the repository then + * returns the name of added repository + * @param repository + * @param name + * @returns {Promise} + */ +export async function addHelmRepository( + repository: string, + name = 'action-repo' +): Promise { + core.info('Adding helm repository') + + execSync(`helm add repo ${name} ${repository}`, { + stdio: 'inherit', + cwd: repositoryDirectory + }) + + core.info('updating helm repository') + execSync(`helm repo update`, { + stdio: 'inherit', + cwd: repositoryDirectory + }) + + return name +} diff --git a/src/handlers/deploy-helm.ts b/src/handlers/deploy-helm.ts index 96f9e40..0d72202 100644 --- a/src/handlers/deploy-helm.ts +++ b/src/handlers/deploy-helm.ts @@ -1,6 +1,7 @@ +import * as core from '@actions/core' +import {errorHandler} from '../helpers/error-handler' import {execSync} from 'child_process' import {repositoryDirectory} from '../constants/repositoryDirectory' -import {info} from '@actions/core' /** * This method will installs the helm chart on target kubernetes cluster @@ -12,21 +13,30 @@ import {info} from '@actions/core' * @param {string} namespace default value is 'default' * @returns {Promise} */ -export async function deployHelmChart( - releaseName: string, - genericChart: string, - namespace: string = 'default' -): Promise { - info('deploying works') +export async function deployHelmChart(config: { + addedHelmRepositoryName: string + releaseName: string + chartVersion: string + chartName: string + valuesPath: string + namespace?: string +}): Promise { + try { + core.info('Deploying the helm') + execSync( + `helm upgrade --install --timeout 180s ${config.releaseName} ${ + config.addedHelmRepositoryName + }/${config.chartName} -f ${config.valuesPath} --version ${ + config.chartVersion + } ${ + config?.namespace ? `-n ${config.namespace}` : '' + } --kubeconfig kubeconfig`, + {stdio: 'inherit', cwd: repositoryDirectory} + ) - function wait() { - return new Promise((resolve, reject) => { - setTimeout(() => {}, 2000) - }) + core.info('Deploying is done') + } catch (error) { + core.error('Deploying helm error') + errorHandler(error) } - - execSync('kubectl get pods --kubeconfig=kubeconfig', { - stdio: 'inherit', - cwd: repositoryDirectory - }) } diff --git a/src/main.ts b/src/main.ts index 760282f..cf7cfdb 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,11 +2,10 @@ import * as core from '@actions/core' import {installHelm} from './handlers/install-helm' import {installKubectl} from './handlers/install-kubectl' 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' import {setKubectlContext} from './handlers/kubectl-set-context' +import {addHelmRepository} from './handlers/add-helm-repo' async function run(): Promise { try { @@ -15,18 +14,31 @@ async function run(): Promise { trimWhitespace: true }) - const valuesPath = core.getInput('valuesPath') - const releaseName = core.getInput('releaseName') + const kubeConfig = core.getInput('kubeConfig', {required: true}) + const releaseName = core.getInput('releaseName') || 'default' const namespace = core.getInput('namespace') const context = core.getInput('context') - const token = core.getInput('token', {required: true}) - const kubeConfig = core.getInput('kubeConfig', {required: true}) + + const chartRemote = core.getInput('remoteRepository', {required: true}) + const chartVersion = core.getInput('chartVersion', {required: true}) + const chartName = core.getInput('chartName', {required: true}) + const valuesPath = core.getInput('valuesPath') await installKubectl() await setupKubectlConfig(kubeConfig) await installHelm() await setKubectlContext(context) - await deployHelmChart(releaseName, genericChart, namespace) + + const addedHelmRepositoryName = await addHelmRepository(chartRemote) + + await deployHelmChart({ + addedHelmRepositoryName, + releaseName, + chartVersion, + chartName, + valuesPath, + namespace + }) return } catch (error) {