k8s-deploy/src/run.ts
David Gamero 01cfe404ef
Migrate to esbuild/Vitest and upgrade @actions/* to ESM-only versions (#492)
* Migrate build toolchain from ncc/Jest to esbuild/Vitest

Replace the legacy ncc/Jest/Babel build stack with a modern ESM toolchain:

Build:
- Replace @vercel/ncc with esbuild (--platform=node --target=node20 --format=esm)
- Add createRequire banner for CJS interop in ESM bundle
- Add "type": "module" to package.json
- Add tsc --noEmit typecheck script (esbuild strips types without checking)
- Add typecheck to husky pre-commit hook

Dependencies:
- Bump @actions/core@3, exec@3, io@3, tool-cache@4 (ESM-only)
- Replace jest/ts-jest/@babel/* with vitest@4

Tests:
- Convert 29 test files: jest.fn()→vi.fn(), jest.mock()→vi.mock(), jest.spyOn()→vi.spyOn()
- Fix vitest 4 compat: mockImplementation requires args, mock call tracking, await .rejects

CI:
- Update build step from ncc build → npm run build
- Update composite action to use npm run build

* Switch tsconfig to NodeNext module resolution

Change module/moduleResolution from ES2022/bundler to NodeNext/NodeNext
and target from ES2022 to ES2020.

- Add .js extensions to all relative imports across 59 source/test files
  (required by NodeNext module resolution)
- Add vitest/globals to tsconfig types array for global test API declarations
2026-02-24 11:57:56 -08:00

112 lines
3.6 KiB
TypeScript

import * as core from '@actions/core'
import {getKubectlPath, Kubectl} from './types/kubectl.js'
import {
deploy,
ResourceTypeFleet,
ResourceTypeManagedCluster
} from './actions/deploy.js'
import {ClusterType} from './inputUtils.js'
import {promote} from './actions/promote.js'
import {reject} from './actions/reject.js'
import {Action, parseAction} from './types/action.js'
import {parseDeploymentStrategy} from './types/deploymentStrategy.js'
import {getFilesFromDirectoriesAndURLs} from './utilities/fileUtils.js'
import {PrivateKubectl} from './types/privatekubectl.js'
import {parseResourceTypeInput} from './inputUtils.js'
import {parseDuration} from './utilities/durationUtils.js'
export async function run() {
// verify kubeconfig is set
if (!process.env['KUBECONFIG'])
core.warning(
'KUBECONFIG env is not explicitly set. Ensure cluster context is set by using k8s-set-context action.'
)
// get inputs
const action: Action | undefined = parseAction(
core.getInput('action', {required: true})
)
const strategy = parseDeploymentStrategy(core.getInput('strategy'))
const manifestsInput = core.getInput('manifests', {required: true})
const manifestFilePaths = manifestsInput
.split(/[\n,;]+/) // split into each individual manifest
.map((manifest) => manifest.trim()) // remove surrounding whitespace
.filter((manifest) => manifest.length > 0) // remove any blanks
const fullManifestFilePaths =
await getFilesFromDirectoriesAndURLs(manifestFilePaths)
const kubectlPath = await getKubectlPath()
const namespace = core.getInput('namespace') || '' // Sets namespace to an empty string if not provided, allowing the manifest-defined namespace to take precedence instead of "default".
const isPrivateCluster =
core.getInput('private-cluster').toLowerCase() === 'true'
const resourceGroup = core.getInput('resource-group') || ''
const resourceName = core.getInput('name') || ''
const skipTlsVerify = core.getBooleanInput('skip-tls-verify')
let resourceType: ClusterType
try {
// included in the trycatch to allow raw input to go out of scope after parsing
const resourceTypeInput = core.getInput('resource-type')
resourceType = parseResourceTypeInput(resourceTypeInput)
} catch (e) {
core.setFailed(e)
return
}
// Parse and validate timeout using extracted utility
let timeout: string
try {
const timeoutInput = core.getInput('timeout') || '10m'
timeout = parseDuration(timeoutInput)
core.debug(`Using timeout: ${timeout}`)
} catch (e) {
core.setFailed(`Invalid timeout parameter: ${e.message}`)
return
}
const kubectl = isPrivateCluster
? new PrivateKubectl(
kubectlPath,
namespace,
skipTlsVerify,
resourceGroup,
resourceName
)
: new Kubectl(kubectlPath, namespace, skipTlsVerify)
// run action
switch (action) {
case Action.DEPLOY: {
await deploy(
kubectl,
fullManifestFilePaths,
strategy,
resourceType,
timeout
)
break
}
case Action.PROMOTE: {
await promote(
kubectl,
fullManifestFilePaths,
strategy,
resourceType,
timeout
)
break
}
case Action.REJECT: {
await reject(kubectl, fullManifestFilePaths, strategy, timeout)
break
}
default: {
throw Error(
'Not a valid action. The allowed actions are "deploy", "promote", and "reject".'
)
}
}
}
run().catch(core.setFailed)