Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9d3c93f137 | ||
|
|
ce29488755 | ||
|
|
f08750dda0 | ||
|
|
a3a1ddb586 | ||
|
|
b413e7e15e | ||
|
|
43525325f3 | ||
|
|
8c4badf72a | ||
|
|
ff8bf47b42 |
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "kubectl-action",
|
||||
"version": "1.1.1",
|
||||
"version": "1.1.3",
|
||||
"scripts": {
|
||||
"dev": "ncc -smw --license licenses.txt build src/main.ts",
|
||||
"build": "ncc -sm --license licenses.txt build src/main.ts",
|
||||
@@ -8,6 +8,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@actions/core": "^1.10.0",
|
||||
"@actions/tool-cache": "^2.0.1",
|
||||
"undici": "^5.16.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
30
pnpm-lock.yaml
generated
30
pnpm-lock.yaml
generated
@@ -2,6 +2,7 @@ lockfileVersion: 5.4
|
||||
|
||||
specifiers:
|
||||
'@actions/core': ^1.10.0
|
||||
'@actions/tool-cache': ^2.0.1
|
||||
'@types/node': ^18.11.18
|
||||
'@vercel/ncc': ^0.36.0
|
||||
eslint: ^8.32.0
|
||||
@@ -12,6 +13,7 @@ specifiers:
|
||||
|
||||
dependencies:
|
||||
'@actions/core': 1.10.0
|
||||
'@actions/tool-cache': 2.0.1
|
||||
undici: 5.16.0
|
||||
|
||||
devDependencies:
|
||||
@@ -31,12 +33,33 @@ packages:
|
||||
uuid: 8.3.2
|
||||
dev: false
|
||||
|
||||
/@actions/exec/1.1.1:
|
||||
resolution: {integrity: sha512-+sCcHHbVdk93a0XT19ECtO/gIXoxvdsgQLzb2fE2/5sIZmWQuluYyjPQtrtTHdU1YzTZ7bAPN4sITq2xi1679w==}
|
||||
dependencies:
|
||||
'@actions/io': 1.1.2
|
||||
dev: false
|
||||
|
||||
/@actions/http-client/2.0.1:
|
||||
resolution: {integrity: sha512-PIXiMVtz6VvyaRsGY268qvj57hXQEpsYogYOu2nrQhlf+XCGmZstmuZBbAybUl1nQGnvS1k1eEsQ69ZoD7xlSw==}
|
||||
dependencies:
|
||||
tunnel: 0.0.6
|
||||
dev: false
|
||||
|
||||
/@actions/io/1.1.2:
|
||||
resolution: {integrity: sha512-d+RwPlMp+2qmBfeLYPLXuSRykDIFEwdTA0MMxzS9kh4kvP1ftrc/9fzy6pX6qAjthdXruHQ6/6kjT/DNo5ALuw==}
|
||||
dev: false
|
||||
|
||||
/@actions/tool-cache/2.0.1:
|
||||
resolution: {integrity: sha512-iPU+mNwrbA8jodY8eyo/0S/QqCKDajiR8OxWTnSk/SnYg0sj8Hp4QcUEVC1YFpHWXtrfbQrE13Jz4k4HXJQKcA==}
|
||||
dependencies:
|
||||
'@actions/core': 1.10.0
|
||||
'@actions/exec': 1.1.1
|
||||
'@actions/http-client': 2.0.1
|
||||
'@actions/io': 1.1.2
|
||||
semver: 6.3.0
|
||||
uuid: 3.4.0
|
||||
dev: false
|
||||
|
||||
/@babel/code-frame/7.18.6:
|
||||
resolution: {integrity: sha512-TDCmlK5eOvH+eH7cdAFlNXeVJqWIQ7gW9tY1GJIpUtFb6CmjVyq2VM3u71bOyR8CRihcCgMUYoDNyLXao3+70Q==}
|
||||
engines: {node: '>=6.9.0'}
|
||||
@@ -3104,7 +3127,6 @@ packages:
|
||||
/semver/6.3.0:
|
||||
resolution: {integrity: sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==}
|
||||
hasBin: true
|
||||
dev: true
|
||||
|
||||
/semver/7.3.8:
|
||||
resolution: {integrity: sha512-NB1ctGL5rlHrPJtFDVIVzTyQylMLu9N9VICA6HSFJo8MCGVTMW6gfpicwKmmK/dAjTOrqu5l63JJOpDSrAis3A==}
|
||||
@@ -3510,6 +3532,12 @@ packages:
|
||||
prepend-http: 2.0.0
|
||||
dev: true
|
||||
|
||||
/uuid/3.4.0:
|
||||
resolution: {integrity: sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==}
|
||||
deprecated: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.
|
||||
hasBin: true
|
||||
dev: false
|
||||
|
||||
/uuid/8.3.2:
|
||||
resolution: {integrity: sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==}
|
||||
hasBin: true
|
||||
|
||||
17
src/main.ts
17
src/main.ts
@@ -1,10 +1,23 @@
|
||||
import { platform } from 'node:process'
|
||||
|
||||
import { debug, getState, setFailed } from '@actions/core'
|
||||
import { setupKubeconfig } from 'login'
|
||||
import { installKubectl } from 'setup'
|
||||
import { teardown } from 'teardown'
|
||||
|
||||
const post = Boolean(getState('isPost'))
|
||||
if (platform === 'win32') {
|
||||
setFailed('kubectl-action does not support Windows')
|
||||
}
|
||||
|
||||
if (!post) {
|
||||
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): ')
|
||||
debug(JSON.stringify(error))
|
||||
})
|
||||
} else {
|
||||
debug('Running kubectl-action setup')
|
||||
// eslint-disable-next-line no-async-promise-executor
|
||||
new Promise(async () => {
|
||||
|
||||
99
src/setup.ts
99
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')
|
||||
}
|
||||
|
||||
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,21 @@ 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) {
|
||||
// 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')
|
||||
}
|
||||
|
||||
// TODO: Support other platforms
|
||||
const url = `https://dl.k8s.io/release/${version}/bin/linux/amd64/kubectl`
|
||||
const hashUrl = `${url}.sha256`
|
||||
|
||||
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')
|
||||
}
|
||||
|
||||
@@ -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