Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5aa0d1bacd | ||
|
|
93d421ead9 | ||
|
|
e49d06518c | ||
|
|
69c8dc994c | ||
|
|
e70828b58c | ||
|
|
d7c0fa7a71 | ||
|
|
9d3c93f137 | ||
|
|
ce29488755 | ||
|
|
f08750dda0 | ||
|
|
a3a1ddb586 | ||
|
|
b413e7e15e | ||
|
|
43525325f3 |
17
package.json
17
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "kubectl-action",
|
||||
"version": "1.1.2",
|
||||
"version": "1.2.0",
|
||||
"scripts": {
|
||||
"dev": "ncc -smw --license licenses.txt build src/main.ts",
|
||||
"build": "ncc -sm --license licenses.txt build src/main.ts",
|
||||
@@ -8,14 +8,15 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@actions/core": "^1.10.0",
|
||||
"undici": "^5.16.0"
|
||||
"@actions/tool-cache": "^2.0.1",
|
||||
"undici": "^5.22.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^18.11.18",
|
||||
"@vercel/ncc": "^0.36.0",
|
||||
"eslint": "^8.32.0",
|
||||
"eslint-config-tale": "^1.0.15",
|
||||
"np": "^7.6.3",
|
||||
"typescript": "^4.9.4"
|
||||
"@types/node": "^20.3.2",
|
||||
"@vercel/ncc": "^0.36.1",
|
||||
"eslint": "^8.43.0",
|
||||
"eslint-config-tale": "^1.0.16",
|
||||
"np": "^8.0.4",
|
||||
"typescript": "^5.1.6"
|
||||
}
|
||||
}
|
||||
|
||||
2925
pnpm-lock.yaml
generated
2925
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -1,14 +1,20 @@
|
||||
import { env, platform } from 'node:process'
|
||||
|
||||
import { debug, getState, setFailed } from '@actions/core'
|
||||
import { setupKubeconfig } from 'login'
|
||||
import { installKubectl } from 'setup'
|
||||
import { teardown } from 'teardown'
|
||||
|
||||
if (env.RUNNER_OS === 'Windows' || platform === 'win32') {
|
||||
setFailed('kubectl-action does not support Windows')
|
||||
}
|
||||
|
||||
if (getState('kubectl-path')) {
|
||||
debug('Running post kubectl-action setup')
|
||||
teardown()
|
||||
// eslint-disable-next-line unicorn/prefer-top-level-await
|
||||
.catch(error => {
|
||||
setFailed('Failed to install kubectl (this is a bug in kubectl-action): ')
|
||||
setFailed('Failed to teardown kubectl (this is a bug in kubectl-action): ')
|
||||
debug(JSON.stringify(error))
|
||||
})
|
||||
} else {
|
||||
|
||||
114
src/setup.ts
114
src/setup.ts
@@ -1,10 +1,9 @@
|
||||
import { createHash, randomUUID } from 'node:crypto'
|
||||
import { mkdir, writeFile } from 'node:fs/promises'
|
||||
import { join } from 'node:path'
|
||||
import { env, stdout } from 'node:process'
|
||||
import { clearLine, cursorTo } from 'node:readline'
|
||||
import { chmod } from 'node:fs/promises'
|
||||
import { dirname, join } from 'node:path'
|
||||
import { env } from 'node:process'
|
||||
|
||||
import { addPath, debug, getInput, saveState, setFailed, warning } from '@actions/core'
|
||||
import { addPath, debug, getInput, setFailed } from '@actions/core'
|
||||
import { cacheFile, downloadTool, find } from '@actions/tool-cache'
|
||||
import { fetch } from 'undici'
|
||||
|
||||
export async function installKubectl() {
|
||||
@@ -29,19 +28,16 @@ export async function installKubectl() {
|
||||
}
|
||||
|
||||
console.log(`Installing kubectl version ${version}`)
|
||||
const kubectl = await downloadKubectl(version)
|
||||
|
||||
if (!kubectl) {
|
||||
return
|
||||
try {
|
||||
const path = await fetchKubectl(version)
|
||||
await chmod(path, '775')
|
||||
addPath(dirname(path))
|
||||
debug(`kubectl ${version} installed and cached at ${path}`)
|
||||
} catch {
|
||||
debug('Failed to download kubectl from dl.k8s.io')
|
||||
setFailed('Failed to download kubectl from dl.k8s.io\nPlease check the version you specified is valid')
|
||||
}
|
||||
|
||||
const path = join(env.RUNNER_TEMP, randomUUID())
|
||||
await mkdir(path, { recursive: true })
|
||||
saveState('kubectl-path', path)
|
||||
|
||||
console.log(`Installing kubectl to ${path}`)
|
||||
await writeFile(join(path, 'kubectl'), kubectl)
|
||||
addPath(path)
|
||||
}
|
||||
|
||||
// Fetches the latest kubectl version from the Kubernetes release server
|
||||
@@ -57,62 +53,34 @@ async function fetchLatestVersion() {
|
||||
}
|
||||
|
||||
// Downloads the kubectl binary from the Kubernetes release server
|
||||
// Also runs a checksum verification on the downloaded binary
|
||||
async function downloadKubectl(version: string) {
|
||||
const url = `https://dl.k8s.io/release/${version}/bin/linux/amd64/kubectl`
|
||||
const hashUrl = `${url}.sha256`
|
||||
// If already downloaded, returns the path to the cached binary
|
||||
async function fetchKubectl(version: string) {
|
||||
const cachedPath = find('kubectl', version)
|
||||
|
||||
// Cached path is a directory containing the kubectl binary
|
||||
if (cachedPath) {
|
||||
debug(`kubectl ${version} already installed`)
|
||||
return join(cachedPath, 'kubectl')
|
||||
}
|
||||
|
||||
const url = `https://dl.k8s.io/release/${version}/bin/${retrieveRunnerMetadata()}/kubectl`
|
||||
|
||||
console.log(`Downloading kubectl (${url})`)
|
||||
debug(`Downloading kubectl checksum (${hashUrl})`)
|
||||
|
||||
const hashResponse = await fetch(hashUrl)
|
||||
if (!hashResponse.ok) {
|
||||
debug(`Failed to download kubectl checksum with status ${hashResponse.status}`)
|
||||
warning(`Skipping checksum verification for kubectl ${version}`)
|
||||
}
|
||||
|
||||
const hash = hashResponse.ok ? await hashResponse.text() : ''
|
||||
|
||||
const response = await fetch(url)
|
||||
if (!response.ok || !response.body) {
|
||||
debug(`Failed to download kubectl with status ${response.status}`)
|
||||
setFailed(`Failed to download kubectl with status ${response.status}`)
|
||||
return
|
||||
}
|
||||
|
||||
const hashStream = createHash('sha256')
|
||||
const { body, headers } = response
|
||||
const size = Number(headers.get('content-length'))
|
||||
debug(`Downloaded kubectl (${size} bytes)`)
|
||||
|
||||
let downloaded = 0
|
||||
let progressed = 0
|
||||
const buffer = Buffer.alloc(size)
|
||||
|
||||
for await (const chunk of body as AsyncIterable<Buffer>) {
|
||||
buffer.write(chunk.toString('binary'), downloaded, 'binary')
|
||||
hashStream.update(chunk)
|
||||
downloaded += chunk.length
|
||||
|
||||
if (Math.floor((downloaded / size) * 80) > progressed) {
|
||||
clearLine(stdout, 0)
|
||||
cursorTo(stdout, 0)
|
||||
|
||||
progressed++
|
||||
stdout.write(`[${'='.repeat(progressed)}>${' '.repeat(80 - progressed)}]`)
|
||||
}
|
||||
}
|
||||
|
||||
clearLine(stdout, 0)
|
||||
cursorTo(stdout, 0)
|
||||
console.log(`[${'='.repeat(80)}]`)
|
||||
|
||||
const hashSum = hashStream.digest('hex')
|
||||
if (hashResponse.ok && hashSum !== hash) {
|
||||
debug(`Checksum verification failed for kubectl ${version}`)
|
||||
setFailed(`Checksum verification failed for kubectl ${version}`)
|
||||
return
|
||||
}
|
||||
|
||||
return buffer
|
||||
const downloadPath = await downloadTool(url)
|
||||
const toolPath = await cacheFile(downloadPath, 'kubectl', 'kubectl', version)
|
||||
return join(toolPath, 'kubectl')
|
||||
}
|
||||
|
||||
// Gets the proper architecture and OS for the current platform
|
||||
// This doesn't use node functions, but instead CI variables provided by GitHub
|
||||
function retrieveRunnerMetadata() {
|
||||
// Currently we don't support win32 platforms anyways
|
||||
const runnerSystem = env.RUNNER_OS === 'Linux' ? 'linux' : 'darwin'
|
||||
const runnerArch = env.RUNNER_ARCH?.toLowerCase()
|
||||
|
||||
if (runnerArch?.includes('arm')) {
|
||||
return `${runnerSystem}/arm64`
|
||||
}
|
||||
|
||||
return `${runnerSystem}/amd64`
|
||||
}
|
||||
|
||||
@@ -4,10 +4,7 @@ import { debug, getState } from '@actions/core'
|
||||
|
||||
export async function teardown() {
|
||||
debug('Running kubectl-action teardown()')
|
||||
console.log('Removing kubectl and kubeconfig')
|
||||
|
||||
const path = getState('kubectl-path')
|
||||
await rm(path, { recursive: true, force: true })
|
||||
console.log('Removing kubeconfig')
|
||||
|
||||
const configPath = getState('kubeconfig-path')
|
||||
await rm(configPath, { recursive: true, force: true })
|
||||
|
||||
Reference in New Issue
Block a user