Skip to content

Commit

Permalink
feat(deploy-helm): added deploy command
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdi-ra committed Jul 25, 2023
1 parent 98931aa commit 094d4f8
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 28 deletions.
14 changes: 9 additions & 5 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
30 changes: 30 additions & 0 deletions src/handlers/add-helm-repo.ts
Original file line number Diff line number Diff line change
@@ -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<string>}
*/
export async function addHelmRepository(
repository: string,
name = 'action-repo'
): Promise<string> {
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
}
42 changes: 26 additions & 16 deletions src/handlers/deploy-helm.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,21 +13,30 @@ import {info} from '@actions/core'
* @param {string} namespace default value is 'default'
* @returns {Promise<void>}
*/
export async function deployHelmChart(
releaseName: string,
genericChart: string,
namespace: string = 'default'
): Promise<void> {
info('deploying works')
export async function deployHelmChart(config: {
addedHelmRepositoryName: string
releaseName: string
chartVersion: string
chartName: string
valuesPath: string
namespace?: string
}): Promise<void> {
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
})
}
26 changes: 19 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
try {
Expand All @@ -15,18 +14,31 @@ async function run(): Promise<void> {
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) {
Expand Down

0 comments on commit 094d4f8

Please sign in to comment.