mirror of
https://github.com/Azure/setup-kubectl.git
synced 2026-04-07 13:22:18 +08:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0c5e050edf |
2
.github/CODEOWNERS
vendored
2
.github/CODEOWNERS
vendored
@ -1 +1 @@
|
|||||||
* @Azure/cloud-native-github-action-owners
|
* @Azure/aks-atlanta
|
||||||
6
.github/workflows/codeql.yml
vendored
6
.github/workflows/codeql.yml
vendored
@ -55,11 +55,11 @@ jobs:
|
|||||||
# your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages
|
# your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||||
|
|
||||||
# Initializes the CodeQL tools for scanning.
|
# Initializes the CodeQL tools for scanning.
|
||||||
- name: Initialize CodeQL
|
- name: Initialize CodeQL
|
||||||
uses: github/codeql-action/init@c10b8064de6f491fea524254123dbe5e09572f13 # v4.35.1
|
uses: github/codeql-action/init@17a820bf2e43b47be2c72b39cc905417bc1ab6d0 # v3.28.6
|
||||||
with:
|
with:
|
||||||
languages: ${{ matrix.language }}
|
languages: ${{ matrix.language }}
|
||||||
build-mode: ${{ matrix.build-mode }}
|
build-mode: ${{ matrix.build-mode }}
|
||||||
@ -86,6 +86,6 @@ jobs:
|
|||||||
echo ' make release'
|
echo ' make release'
|
||||||
exit 1
|
exit 1
|
||||||
- name: Perform CodeQL Analysis
|
- name: Perform CodeQL Analysis
|
||||||
uses: github/codeql-action/analyze@c10b8064de6f491fea524254123dbe5e09572f13 # v4.35.1
|
uses: github/codeql-action/analyze@17a820bf2e43b47be2c72b39cc905417bc1ab6d0 # v3.28.6
|
||||||
with:
|
with:
|
||||||
category: '/language:${{matrix.language}}'
|
category: '/language:${{matrix.language}}'
|
||||||
|
|||||||
4
.github/workflows/defaultLabels.yml
vendored
4
.github/workflows/defaultLabels.yml
vendored
@ -8,7 +8,7 @@ jobs:
|
|||||||
label-issues:
|
label-issues:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/stale@b5d41d4e1d5dceea10e7104786b73624c18a190f # v10.2.0
|
- uses: actions/stale@5bef64f19d7facfb25b37b414482c7164d639639 # v9.1.0
|
||||||
name: Setting issue as idle
|
name: Setting issue as idle
|
||||||
with:
|
with:
|
||||||
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
@ -19,7 +19,7 @@ jobs:
|
|||||||
operations-per-run: 100
|
operations-per-run: 100
|
||||||
exempt-issue-labels: 'backlog'
|
exempt-issue-labels: 'backlog'
|
||||||
|
|
||||||
- uses: actions/stale@b5d41d4e1d5dceea10e7104786b73624c18a190f # v10.2.0
|
- uses: actions/stale@5bef64f19d7facfb25b37b414482c7164d639639 # v9.1.0
|
||||||
name: Setting PR as idle
|
name: Setting PR as idle
|
||||||
with:
|
with:
|
||||||
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|||||||
6
.github/workflows/integration-tests.yml
vendored
6
.github/workflows/integration-tests.yml
vendored
@ -17,7 +17,7 @@ jobs:
|
|||||||
KUBECONFIG: /home/runner/.kube/config
|
KUBECONFIG: /home/runner/.kube/config
|
||||||
PR_BASE_REF: ${{ github.event.pull_request.base.ref }}
|
PR_BASE_REF: ${{ github.event.pull_request.base.ref }}
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||||
name: Checkout from PR branch
|
name: Checkout from PR branch
|
||||||
|
|
||||||
- id: action-npm-build
|
- id: action-npm-build
|
||||||
@ -27,11 +27,9 @@ jobs:
|
|||||||
if [[ $PR_BASE_REF != releases/* ]]; then
|
if [[ $PR_BASE_REF != releases/* ]]; then
|
||||||
npm install
|
npm install
|
||||||
npm run build
|
npm run build
|
||||||
# remove node_modules to match production environment where only index.js is present
|
|
||||||
rm -rf node_modules
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
- uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
|
- uses: actions/setup-python@42375524e23c412d93fb67b49958b491fce71c38 # v5.4.0
|
||||||
name: Install Python
|
name: Install Python
|
||||||
with:
|
with:
|
||||||
python-version: '3.x'
|
python-version: '3.x'
|
||||||
|
|||||||
4
.github/workflows/prettify-code.yml
vendored
4
.github/workflows/prettify-code.yml
vendored
@ -10,10 +10,10 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout Repository
|
- name: Checkout Repository
|
||||||
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||||
|
|
||||||
- name: Setup Node.js
|
- name: Setup Node.js
|
||||||
uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
|
uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
|
||||||
with:
|
with:
|
||||||
node-version: 'lts/*'
|
node-version: 'lts/*'
|
||||||
cache: 'npm'
|
cache: 'npm'
|
||||||
|
|||||||
2
.github/workflows/unit-tests.yml
vendored
2
.github/workflows/unit-tests.yml
vendored
@ -13,7 +13,7 @@ jobs:
|
|||||||
build: # make sure build/ci works properly
|
build: # make sure build/ci works properly
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||||
|
|
||||||
- name: Build and run L0 tests.
|
- name: Build and run L0 tests.
|
||||||
run: |
|
run: |
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@ -330,4 +330,3 @@ ASALocalRun/
|
|||||||
node_modules
|
node_modules
|
||||||
|
|
||||||
# Transpiled JS
|
# Transpiled JS
|
||||||
lib/
|
|
||||||
|
|||||||
@ -1,9 +0,0 @@
|
|||||||
set +e
|
|
||||||
npm test
|
|
||||||
# Run format check
|
|
||||||
npm run format-check || {
|
|
||||||
echo ""
|
|
||||||
echo "❌ Formatting check failed."
|
|
||||||
echo "💡 Run 'npm run format' or 'prettier --write .' to fix formatting issues."
|
|
||||||
exit 1
|
|
||||||
}
|
|
||||||
24
CHANGELOG.md
24
CHANGELOG.md
@ -1,28 +1,6 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
## [5.0.0] - 2026-03-25
|
## [v4.0.0] - 2024-01-30
|
||||||
|
|
||||||
### Changed
|
|
||||||
|
|
||||||
- #233 [Update Node.js runtime from node20 to node24](https://github.com/Azure/setup-kubectl/pull/233)
|
|
||||||
- #228 [Replace cdn.dl.k8s.io with dl.k8s.io](https://github.com/Azure/setup-kubectl/pull/228)
|
|
||||||
- #219 [Remove download redirects, use cdn.dl.k8s.io domain](https://github.com/Azure/setup-kubectl/pull/219)
|
|
||||||
- #190 [Update stableVersionUrl to dl.k8s.io](https://github.com/Azure/setup-kubectl/pull/190)
|
|
||||||
- #235 [Bump undici from 6.23.0 to 6.24.1](https://github.com/Azure/setup-kubectl/pull/235)
|
|
||||||
- #226 [Bump undici and @actions/http-client](https://github.com/Azure/setup-kubectl/pull/226)
|
|
||||||
- #230 [Bump minimatch](https://github.com/Azure/setup-kubectl/pull/230)
|
|
||||||
|
|
||||||
### Added
|
|
||||||
|
|
||||||
- #172 [Enhance version handling: auto-resolve kubectl major.minor to latest patch](https://github.com/Azure/setup-kubectl/pull/172)
|
|
||||||
- #171 [Add husky precommit check](https://github.com/Azure/setup-kubectl/pull/171)
|
|
||||||
|
|
||||||
## [4.0.1] - 2025-06-17
|
|
||||||
|
|
||||||
- Remove erronious 'v' prefix on previous changelog for v4.0.0 that led to "vv4.0.0" tag issue
|
|
||||||
- Dependabot fixes
|
|
||||||
|
|
||||||
## [4.0.0] - 2024-01-30
|
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
name: 'Kubectl tool installer'
|
name: 'Kubectl tool installer'
|
||||||
description: 'Install a specific version of kubectl binary. Acceptable values are latest or any semantic version string like "v1.15.0"'
|
description: 'Install a specific version of kubectl binary. Acceptable values are latest or any semantic version string like 1.15.0'
|
||||||
inputs:
|
inputs:
|
||||||
version:
|
version:
|
||||||
description: 'Version of kubectl'
|
description: 'Version of kubectl'
|
||||||
@ -11,5 +11,5 @@ outputs:
|
|||||||
branding:
|
branding:
|
||||||
color: 'blue'
|
color: 'blue'
|
||||||
runs:
|
runs:
|
||||||
using: 'node24'
|
using: 'node20'
|
||||||
main: 'lib/index.js'
|
main: 'lib/index.js'
|
||||||
|
|||||||
18
jest.config.js
Normal file
18
jest.config.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
module.exports = {
|
||||||
|
clearMocks: true,
|
||||||
|
moduleFileExtensions: ['js', 'ts'],
|
||||||
|
testEnvironment: 'node',
|
||||||
|
testMatch: ['**/*.test.ts'],
|
||||||
|
transform: {
|
||||||
|
'^.+\\.ts$': 'ts-jest'
|
||||||
|
},
|
||||||
|
verbose: true,
|
||||||
|
coverageThreshold: {
|
||||||
|
global: {
|
||||||
|
branches: 0,
|
||||||
|
functions: 14,
|
||||||
|
lines: 27,
|
||||||
|
statements: 27
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
6280
lib/index.js
Normal file
6280
lib/index.js
Normal file
File diff suppressed because it is too large
Load Diff
7987
package-lock.json
generated
7987
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
33
package.json
33
package.json
@ -1,17 +1,14 @@
|
|||||||
{
|
{
|
||||||
"name": "setup-kubectl-action",
|
"name": "setup-kubectl-action",
|
||||||
"version": "5.0.0",
|
"version": "0.0.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"type": "module",
|
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc --noEmit && esbuild src/index.ts --bundle --platform=node --target=node20 --format=esm --outfile=lib/index.js --banner:js=\"import { createRequire } from 'module';const require = createRequire(import.meta.url);\"",
|
"build": "npm i ncc && npx ncc build src/index.ts -o lib",
|
||||||
"typecheck": "tsc --noEmit",
|
"test": "jest",
|
||||||
"test": "vitest run",
|
"test-coverage": "jest --coverage",
|
||||||
"test-coverage": "vitest run --coverage",
|
|
||||||
"format": "prettier --write .",
|
"format": "prettier --write .",
|
||||||
"format-check": "prettier --check .",
|
"format-check": "prettier --check ."
|
||||||
"prepare": "husky"
|
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"actions",
|
"actions",
|
||||||
@ -21,16 +18,18 @@
|
|||||||
"author": "GitHub",
|
"author": "GitHub",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@actions/core": "^3.0.0",
|
"@actions/core": "^1.11.1",
|
||||||
"@actions/exec": "^3.0.0",
|
"@actions/exec": "^1.0.0",
|
||||||
"@actions/tool-cache": "^4.0.0"
|
"@actions/tool-cache": "^2.0.2",
|
||||||
|
"ncc": "^0.3.6"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^25.0.9",
|
"@types/jest": "^29.5.14",
|
||||||
"esbuild": "^0.27.4",
|
"@types/node": "^22.10.10",
|
||||||
"husky": "^9.1.7",
|
"@vercel/ncc": "^0.38.3",
|
||||||
"prettier": "3.8.0",
|
"jest": "^29.7.0",
|
||||||
"typescript": "5.9.3",
|
"prettier": "3.4.2",
|
||||||
"vitest": "^4.0.18"
|
"ts-jest": "^29.2.5",
|
||||||
|
"typescript": "5.7.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,6 @@
|
|||||||
import * as os from 'os'
|
import * as os from 'os'
|
||||||
import * as util from 'util'
|
import * as util from 'util'
|
||||||
import * as fs from 'fs'
|
|
||||||
import * as core from '@actions/core'
|
|
||||||
import * as toolCache from '@actions/tool-cache'
|
|
||||||
export function getKubectlArch(): string {
|
export function getKubectlArch(): string {
|
||||||
const arch = os.arch()
|
const arch = os.arch()
|
||||||
if (arch === 'x64') {
|
if (arch === 'x64') {
|
||||||
@ -25,29 +23,6 @@ export function getkubectlDownloadURL(version: string, arch: string): string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getLatestPatchVersion(
|
|
||||||
major: string,
|
|
||||||
minor: string
|
|
||||||
): Promise<string> {
|
|
||||||
const version = `${major}.${minor}`
|
|
||||||
const sourceURL = `https://dl.k8s.io/release/stable-${version}.txt`
|
|
||||||
try {
|
|
||||||
const downloadPath = await toolCache.downloadTool(sourceURL)
|
|
||||||
const latestPatch = fs
|
|
||||||
.readFileSync(downloadPath, 'utf8')
|
|
||||||
.toString()
|
|
||||||
.trim()
|
|
||||||
if (!latestPatch) {
|
|
||||||
throw new Error(`No patch version found for ${version}`)
|
|
||||||
}
|
|
||||||
return latestPatch
|
|
||||||
} catch (error) {
|
|
||||||
core.debug(String(error))
|
|
||||||
core.warning('GetLatestPatchVersionFailed')
|
|
||||||
throw new Error(`Failed to get latest patch version for ${version}`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getExecutableExtension(): string {
|
export function getExecutableExtension(): string {
|
||||||
if (os.type().match(/^Win/)) {
|
if (os.type().match(/^Win/)) {
|
||||||
return '.exe'
|
return '.exe'
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import {run} from './run.js'
|
import {run} from './run'
|
||||||
import * as core from '@actions/core'
|
import * as core from '@actions/core'
|
||||||
|
|
||||||
run().catch(core.setFailed)
|
run().catch(core.setFailed)
|
||||||
|
|||||||
227
src/run.test.ts
227
src/run.test.ts
@ -1,45 +1,26 @@
|
|||||||
import {vi, describe, test, expect, beforeEach} from 'vitest'
|
import * as run from './run'
|
||||||
import * as path from 'path'
|
import {
|
||||||
import * as util from 'util'
|
|
||||||
|
|
||||||
vi.mock('os')
|
|
||||||
vi.mock('fs')
|
|
||||||
vi.mock('@actions/tool-cache', async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import('@actions/tool-cache')>()
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
downloadTool: vi.fn(),
|
|
||||||
find: vi.fn(),
|
|
||||||
cacheFile: vi.fn()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
vi.mock('@actions/core')
|
|
||||||
|
|
||||||
const os = await import('os')
|
|
||||||
const fs = await import('fs')
|
|
||||||
const toolCache = await import('@actions/tool-cache')
|
|
||||||
const core = await import('@actions/core')
|
|
||||||
const run = await import('./run.js')
|
|
||||||
const {
|
|
||||||
getkubectlDownloadURL,
|
getkubectlDownloadURL,
|
||||||
getKubectlArch,
|
getKubectlArch,
|
||||||
getExecutableExtension,
|
getExecutableExtension
|
||||||
getLatestPatchVersion
|
} from './helpers'
|
||||||
} = await import('./helpers.js')
|
import * as os from 'os'
|
||||||
|
import * as toolCache from '@actions/tool-cache'
|
||||||
|
import * as fs from 'fs'
|
||||||
|
import * as path from 'path'
|
||||||
|
import * as core from '@actions/core'
|
||||||
|
import * as util from 'util'
|
||||||
|
|
||||||
describe('Testing all functions in run file.', () => {
|
describe('Testing all functions in run file.', () => {
|
||||||
beforeEach(() => {
|
|
||||||
vi.clearAllMocks()
|
|
||||||
})
|
|
||||||
test('getExecutableExtension() - return .exe when os is Windows', () => {
|
test('getExecutableExtension() - return .exe when os is Windows', () => {
|
||||||
vi.mocked(os.type).mockReturnValue('Windows_NT')
|
jest.spyOn(os, 'type').mockReturnValue('Windows_NT')
|
||||||
expect(getExecutableExtension()).toBe('.exe')
|
expect(getExecutableExtension()).toBe('.exe')
|
||||||
expect(os.type).toHaveBeenCalled()
|
expect(os.type).toBeCalled()
|
||||||
})
|
})
|
||||||
test('getExecutableExtension() - return empty string for non-windows OS', () => {
|
test('getExecutableExtension() - return empty string for non-windows OS', () => {
|
||||||
vi.mocked(os.type).mockReturnValue('Darwin')
|
jest.spyOn(os, 'type').mockReturnValue('Darwin')
|
||||||
expect(getExecutableExtension()).toBe('')
|
expect(getExecutableExtension()).toBe('')
|
||||||
expect(os.type).toHaveBeenCalled()
|
expect(os.type).toBeCalled()
|
||||||
})
|
})
|
||||||
test.each([
|
test.each([
|
||||||
['arm', 'arm'],
|
['arm', 'arm'],
|
||||||
@ -48,101 +29,111 @@ describe('Testing all functions in run file.', () => {
|
|||||||
])(
|
])(
|
||||||
'getKubectlArch() - return on %s os arch %s kubectl arch',
|
'getKubectlArch() - return on %s os arch %s kubectl arch',
|
||||||
(osArch, kubectlArch) => {
|
(osArch, kubectlArch) => {
|
||||||
vi.mocked(os.arch).mockReturnValue(osArch as NodeJS.Architecture)
|
jest.spyOn(os, 'arch').mockReturnValue(osArch)
|
||||||
expect(getKubectlArch()).toBe(kubectlArch)
|
expect(getKubectlArch()).toBe(kubectlArch)
|
||||||
expect(os.arch).toHaveBeenCalled()
|
expect(os.arch).toBeCalled()
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
test.each([['arm'], ['arm64'], ['amd64']])(
|
test.each([['arm'], ['arm64'], ['amd64']])(
|
||||||
'getkubectlDownloadURL() - return the URL to download %s kubectl for Linux',
|
'getkubectlDownloadURL() - return the URL to download %s kubectl for Linux',
|
||||||
(arch) => {
|
(arch) => {
|
||||||
vi.mocked(os.type).mockReturnValue('Linux')
|
jest.spyOn(os, 'type').mockReturnValue('Linux')
|
||||||
const kubectlLinuxUrl = util.format(
|
const kubectlLinuxUrl = util.format(
|
||||||
'https://dl.k8s.io/release/v1.15.0/bin/linux/%s/kubectl',
|
'https://dl.k8s.io/release/v1.15.0/bin/linux/%s/kubectl',
|
||||||
arch
|
arch
|
||||||
)
|
)
|
||||||
expect(getkubectlDownloadURL('v1.15.0', arch)).toBe(kubectlLinuxUrl)
|
expect(getkubectlDownloadURL('v1.15.0', arch)).toBe(kubectlLinuxUrl)
|
||||||
expect(os.type).toHaveBeenCalled()
|
expect(os.type).toBeCalled()
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
test.each([['arm'], ['arm64'], ['amd64']])(
|
test.each([['arm'], ['arm64'], ['amd64']])(
|
||||||
'getkubectlDownloadURL() - return the URL to download %s kubectl for Darwin',
|
'getkubectlDownloadURL() - return the URL to download %s kubectl for Darwin',
|
||||||
(arch) => {
|
(arch) => {
|
||||||
vi.mocked(os.type).mockReturnValue('Darwin')
|
jest.spyOn(os, 'type').mockReturnValue('Darwin')
|
||||||
const kubectlDarwinUrl = util.format(
|
const kubectlDarwinUrl = util.format(
|
||||||
'https://dl.k8s.io/release/v1.15.0/bin/darwin/%s/kubectl',
|
'https://dl.k8s.io/release/v1.15.0/bin/darwin/%s/kubectl',
|
||||||
arch
|
arch
|
||||||
)
|
)
|
||||||
expect(getkubectlDownloadURL('v1.15.0', arch)).toBe(kubectlDarwinUrl)
|
expect(getkubectlDownloadURL('v1.15.0', arch)).toBe(kubectlDarwinUrl)
|
||||||
expect(os.type).toHaveBeenCalled()
|
expect(os.type).toBeCalled()
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
test.each([['arm'], ['arm64'], ['amd64']])(
|
test.each([['arm'], ['arm64'], ['amd64']])(
|
||||||
'getkubectlDownloadURL() - return the URL to download %s kubectl for Windows',
|
'getkubectlDownloadURL() - return the URL to download %s kubectl for Windows',
|
||||||
(arch) => {
|
(arch) => {
|
||||||
vi.mocked(os.type).mockReturnValue('Windows_NT')
|
jest.spyOn(os, 'type').mockReturnValue('Windows_NT')
|
||||||
const kubectlWindowsUrl = util.format(
|
const kubectlWindowsUrl = util.format(
|
||||||
'https://dl.k8s.io/release/v1.15.0/bin/windows/%s/kubectl.exe',
|
'https://dl.k8s.io/release/v1.15.0/bin/windows/%s/kubectl.exe',
|
||||||
arch
|
arch
|
||||||
)
|
)
|
||||||
expect(getkubectlDownloadURL('v1.15.0', arch)).toBe(kubectlWindowsUrl)
|
expect(getkubectlDownloadURL('v1.15.0', arch)).toBe(kubectlWindowsUrl)
|
||||||
expect(os.type).toHaveBeenCalled()
|
expect(os.type).toBeCalled()
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
test('getStableKubectlVersion() - download stable version file, read version and return it', async () => {
|
test('getStableKubectlVersion() - download stable version file, read version and return it', async () => {
|
||||||
vi.mocked(toolCache.downloadTool).mockResolvedValue('pathToTool')
|
jest
|
||||||
vi.mocked(fs.readFileSync).mockReturnValue('v1.20.4')
|
.spyOn(toolCache, 'downloadTool')
|
||||||
|
.mockReturnValue(Promise.resolve('pathToTool'))
|
||||||
|
jest.spyOn(fs, 'readFileSync').mockReturnValue('v1.20.4')
|
||||||
expect(await run.getStableKubectlVersion()).toBe('v1.20.4')
|
expect(await run.getStableKubectlVersion()).toBe('v1.20.4')
|
||||||
expect(toolCache.downloadTool).toHaveBeenCalled()
|
expect(toolCache.downloadTool).toBeCalled()
|
||||||
expect(fs.readFileSync).toHaveBeenCalledWith('pathToTool', 'utf8')
|
expect(fs.readFileSync).toHaveBeenCalledWith('pathToTool', 'utf8')
|
||||||
})
|
})
|
||||||
test('getStableKubectlVersion() - return default v1.15.0 if version read is empty', async () => {
|
test('getStableKubectlVersion() - return default v1.15.0 if version read is empty', async () => {
|
||||||
vi.mocked(toolCache.downloadTool).mockResolvedValue('pathToTool')
|
jest
|
||||||
vi.mocked(fs.readFileSync).mockReturnValue('')
|
.spyOn(toolCache, 'downloadTool')
|
||||||
|
.mockReturnValue(Promise.resolve('pathToTool'))
|
||||||
|
jest.spyOn(fs, 'readFileSync').mockReturnValue('')
|
||||||
expect(await run.getStableKubectlVersion()).toBe('v1.15.0')
|
expect(await run.getStableKubectlVersion()).toBe('v1.15.0')
|
||||||
expect(toolCache.downloadTool).toHaveBeenCalled()
|
expect(toolCache.downloadTool).toBeCalled()
|
||||||
expect(fs.readFileSync).toHaveBeenCalledWith('pathToTool', 'utf8')
|
expect(fs.readFileSync).toHaveBeenCalledWith('pathToTool', 'utf8')
|
||||||
})
|
})
|
||||||
test('getStableKubectlVersion() - return default v1.15.0 if unable to download file', async () => {
|
test('getStableKubectlVersion() - return default v1.15.0 if unable to download file', async () => {
|
||||||
vi.mocked(toolCache.downloadTool).mockRejectedValue('Unable to download.')
|
jest
|
||||||
|
.spyOn(toolCache, 'downloadTool')
|
||||||
|
.mockRejectedValue('Unable to download.')
|
||||||
expect(await run.getStableKubectlVersion()).toBe('v1.15.0')
|
expect(await run.getStableKubectlVersion()).toBe('v1.15.0')
|
||||||
expect(toolCache.downloadTool).toHaveBeenCalled()
|
expect(toolCache.downloadTool).toBeCalled()
|
||||||
})
|
})
|
||||||
test('downloadKubectl() - download kubectl, add it to toolCache and return path to it', async () => {
|
test('downloadKubectl() - download kubectl, add it to toolCache and return path to it', async () => {
|
||||||
vi.mocked(toolCache.find).mockReturnValue('')
|
jest.spyOn(toolCache, 'find').mockReturnValue('')
|
||||||
vi.mocked(toolCache.downloadTool).mockResolvedValue('pathToTool')
|
jest
|
||||||
vi.mocked(toolCache.cacheFile).mockResolvedValue('pathToCachedTool')
|
.spyOn(toolCache, 'downloadTool')
|
||||||
vi.mocked(os.type).mockReturnValue('Windows_NT')
|
.mockReturnValue(Promise.resolve('pathToTool'))
|
||||||
vi.mocked(fs.chmodSync).mockImplementation(() => {})
|
jest
|
||||||
|
.spyOn(toolCache, 'cacheFile')
|
||||||
|
.mockReturnValue(Promise.resolve('pathToCachedTool'))
|
||||||
|
jest.spyOn(os, 'type').mockReturnValue('Windows_NT')
|
||||||
|
jest.spyOn(fs, 'chmodSync').mockImplementation(() => {})
|
||||||
expect(await run.downloadKubectl('v1.15.0')).toBe(
|
expect(await run.downloadKubectl('v1.15.0')).toBe(
|
||||||
path.join('pathToCachedTool', 'kubectl.exe')
|
path.join('pathToCachedTool', 'kubectl.exe')
|
||||||
)
|
)
|
||||||
expect(toolCache.find).toHaveBeenCalledWith('kubectl', 'v1.15.0')
|
expect(toolCache.find).toHaveBeenCalledWith('kubectl', 'v1.15.0')
|
||||||
expect(toolCache.downloadTool).toHaveBeenCalled()
|
expect(toolCache.downloadTool).toBeCalled()
|
||||||
expect(toolCache.cacheFile).toHaveBeenCalled()
|
expect(toolCache.cacheFile).toBeCalled()
|
||||||
expect(os.type).toHaveBeenCalled()
|
expect(os.type).toBeCalled()
|
||||||
expect(fs.chmodSync).toHaveBeenCalledWith(
|
expect(fs.chmodSync).toHaveBeenCalledWith(
|
||||||
path.join('pathToCachedTool', 'kubectl.exe'),
|
path.join('pathToCachedTool', 'kubectl.exe'),
|
||||||
'775'
|
'775'
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
test('downloadKubectl() - throw DownloadKubectlFailed error when unable to download kubectl', async () => {
|
test('downloadKubectl() - throw DownloadKubectlFailed error when unable to download kubectl', async () => {
|
||||||
vi.mocked(toolCache.find).mockReturnValue('')
|
jest.spyOn(toolCache, 'find').mockReturnValue('')
|
||||||
vi.mocked(toolCache.downloadTool).mockRejectedValue(
|
jest
|
||||||
'Unable to download kubectl.'
|
.spyOn(toolCache, 'downloadTool')
|
||||||
)
|
.mockRejectedValue('Unable to download kubectl.')
|
||||||
await expect(run.downloadKubectl('v1.15.0')).rejects.toThrow(
|
await expect(run.downloadKubectl('v1.15.0')).rejects.toThrow(
|
||||||
'DownloadKubectlFailed'
|
'DownloadKubectlFailed'
|
||||||
)
|
)
|
||||||
expect(toolCache.find).toHaveBeenCalledWith('kubectl', 'v1.15.0')
|
expect(toolCache.find).toHaveBeenCalledWith('kubectl', 'v1.15.0')
|
||||||
expect(toolCache.downloadTool).toHaveBeenCalled()
|
expect(toolCache.downloadTool).toBeCalled()
|
||||||
})
|
})
|
||||||
test('downloadKubectl() - throw kubectl not found error when receive 404 response', async () => {
|
test('downloadKubectl() - throw kubectl not found error when receive 404 response', async () => {
|
||||||
const kubectlVersion = 'v1.15.0'
|
const kubectlVersion = 'v1.15.0'
|
||||||
const arch = 'arm128'
|
const arch = 'arm128'
|
||||||
vi.mocked(os.arch).mockReturnValue(arch as NodeJS.Architecture)
|
jest.spyOn(os, 'arch').mockReturnValue(arch)
|
||||||
vi.mocked(toolCache.find).mockReturnValue('')
|
jest.spyOn(toolCache, 'find').mockReturnValue('')
|
||||||
vi.mocked(toolCache.downloadTool).mockImplementation((_) => {
|
jest.spyOn(toolCache, 'downloadTool').mockImplementation((_) => {
|
||||||
throw new toolCache.HTTPError(404)
|
throw new toolCache.HTTPError(404)
|
||||||
})
|
})
|
||||||
await expect(run.downloadKubectl(kubectlVersion)).rejects.toThrow(
|
await expect(run.downloadKubectl(kubectlVersion)).rejects.toThrow(
|
||||||
@ -152,87 +143,35 @@ describe('Testing all functions in run file.', () => {
|
|||||||
arch
|
arch
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
expect(os.arch).toHaveBeenCalled()
|
expect(os.arch).toBeCalled()
|
||||||
expect(toolCache.find).toHaveBeenCalledWith('kubectl', kubectlVersion)
|
expect(toolCache.find).toHaveBeenCalledWith('kubectl', kubectlVersion)
|
||||||
expect(toolCache.downloadTool).toHaveBeenCalled()
|
expect(toolCache.downloadTool).toBeCalled()
|
||||||
})
|
})
|
||||||
test('downloadKubectl() - return path to existing cache of kubectl', async () => {
|
test('downloadKubectl() - return path to existing cache of kubectl', async () => {
|
||||||
vi.mocked(core.getInput).mockImplementation(() => 'v1.15.5')
|
jest.spyOn(core, 'getInput').mockImplementation(() => 'v1.15.5')
|
||||||
vi.mocked(toolCache.find).mockReturnValue('pathToCachedTool')
|
jest.spyOn(toolCache, 'find').mockReturnValue('pathToCachedTool')
|
||||||
vi.mocked(os.type).mockReturnValue('Windows_NT')
|
jest.spyOn(os, 'type').mockReturnValue('Windows_NT')
|
||||||
vi.mocked(fs.chmodSync).mockImplementation(() => {})
|
jest.spyOn(fs, 'chmodSync').mockImplementation(() => {})
|
||||||
|
jest.spyOn(toolCache, 'downloadTool')
|
||||||
expect(await run.downloadKubectl('v1.15.0')).toBe(
|
expect(await run.downloadKubectl('v1.15.0')).toBe(
|
||||||
path.join('pathToCachedTool', 'kubectl.exe')
|
path.join('pathToCachedTool', 'kubectl.exe')
|
||||||
)
|
)
|
||||||
expect(toolCache.find).toHaveBeenCalledWith('kubectl', 'v1.15.0')
|
expect(toolCache.find).toHaveBeenCalledWith('kubectl', 'v1.15.0')
|
||||||
expect(os.type).toHaveBeenCalled()
|
expect(os.type).toBeCalled()
|
||||||
expect(fs.chmodSync).toHaveBeenCalledWith(
|
expect(fs.chmodSync).toHaveBeenCalledWith(
|
||||||
path.join('pathToCachedTool', 'kubectl.exe'),
|
path.join('pathToCachedTool', 'kubectl.exe'),
|
||||||
'775'
|
'775'
|
||||||
)
|
)
|
||||||
expect(toolCache.downloadTool).not.toHaveBeenCalled()
|
expect(toolCache.downloadTool).not.toBeCalled()
|
||||||
})
|
|
||||||
test('getLatestPatchVersion() - download and return latest patch version', async () => {
|
|
||||||
vi.mocked(toolCache.downloadTool).mockResolvedValue('pathToTool')
|
|
||||||
vi.mocked(fs.readFileSync).mockReturnValue('v1.27.15')
|
|
||||||
|
|
||||||
const result = await getLatestPatchVersion('1', '27')
|
|
||||||
|
|
||||||
expect(result).toBe('v1.27.15')
|
|
||||||
expect(toolCache.downloadTool).toHaveBeenCalledWith(
|
|
||||||
'https://dl.k8s.io/release/stable-1.27.txt'
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('getLatestPatchVersion() - throw error when patch version is empty', async () => {
|
|
||||||
vi.mocked(toolCache.downloadTool).mockResolvedValue('pathToTool')
|
|
||||||
vi.mocked(fs.readFileSync).mockReturnValue('')
|
|
||||||
|
|
||||||
await expect(getLatestPatchVersion('1', '27')).rejects.toThrow(
|
|
||||||
'Failed to get latest patch version for 1.27'
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
test('getLatestPatchVersion() - throw error when download fails', async () => {
|
|
||||||
vi.mocked(toolCache.downloadTool).mockRejectedValue(
|
|
||||||
new Error('Network error')
|
|
||||||
)
|
|
||||||
|
|
||||||
await expect(getLatestPatchVersion('1', '27')).rejects.toThrow(
|
|
||||||
'Failed to get latest patch version for 1.27'
|
|
||||||
)
|
|
||||||
})
|
|
||||||
test('resolveKubectlVersion() - expands major.minor to latest patch', async () => {
|
|
||||||
vi.mocked(toolCache.downloadTool).mockResolvedValue('pathToTool')
|
|
||||||
vi.mocked(fs.readFileSync).mockReturnValue('v1.27.15')
|
|
||||||
|
|
||||||
const result = await run.resolveKubectlVersion('1.27')
|
|
||||||
expect(result).toBe('v1.27.15')
|
|
||||||
})
|
|
||||||
|
|
||||||
test('resolveKubectlVersion() - returns full version unchanged', async () => {
|
|
||||||
const result = await run.resolveKubectlVersion('v1.27.15')
|
|
||||||
expect(result).toBe('v1.27.15')
|
|
||||||
})
|
|
||||||
test('resolveKubectlVersion() - adds v prefix to full version', async () => {
|
|
||||||
const result = await run.resolveKubectlVersion('1.27.15')
|
|
||||||
expect(result).toBe('v1.27.15')
|
|
||||||
})
|
|
||||||
test('resolveKubectlVersion() - expands v-prefixed major.minor to latest patch', async () => {
|
|
||||||
vi.mocked(toolCache.downloadTool).mockResolvedValue('pathToTool')
|
|
||||||
vi.mocked(fs.readFileSync).mockReturnValue('v1.27.15')
|
|
||||||
|
|
||||||
const result = await run.resolveKubectlVersion('v1.27')
|
|
||||||
expect(result).toBe('v1.27.15')
|
|
||||||
})
|
})
|
||||||
test('run() - download specified version and set output', async () => {
|
test('run() - download specified version and set output', async () => {
|
||||||
vi.mocked(core.getInput).mockReturnValue('v1.15.5')
|
jest.spyOn(core, 'getInput').mockReturnValue('v1.15.5')
|
||||||
vi.mocked(toolCache.find).mockReturnValue('pathToCachedTool')
|
jest.spyOn(toolCache, 'find').mockReturnValue('pathToCachedTool')
|
||||||
vi.mocked(os.type).mockReturnValue('Windows_NT')
|
jest.spyOn(os, 'type').mockReturnValue('Windows_NT')
|
||||||
vi.mocked(fs.chmodSync).mockImplementation()
|
jest.spyOn(fs, 'chmodSync').mockImplementation()
|
||||||
vi.mocked(core.addPath).mockImplementation()
|
jest.spyOn(core, 'addPath').mockImplementation()
|
||||||
vi.spyOn(console, 'log').mockImplementation()
|
jest.spyOn(console, 'log').mockImplementation()
|
||||||
vi.mocked(core.setOutput).mockImplementation()
|
jest.spyOn(core, 'setOutput').mockImplementation()
|
||||||
expect(await run.run()).toBeUndefined()
|
expect(await run.run()).toBeUndefined()
|
||||||
expect(core.getInput).toHaveBeenCalledWith('version', {required: true})
|
expect(core.getInput).toHaveBeenCalledWith('version', {required: true})
|
||||||
expect(core.addPath).toHaveBeenCalledWith('pathToCachedTool')
|
expect(core.addPath).toHaveBeenCalledWith('pathToCachedTool')
|
||||||
@ -242,18 +181,20 @@ describe('Testing all functions in run file.', () => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
test('run() - get latest version, download it and set output', async () => {
|
test('run() - get latest version, download it and set output', async () => {
|
||||||
vi.mocked(core.getInput).mockReturnValue('latest')
|
jest.spyOn(core, 'getInput').mockReturnValue('latest')
|
||||||
vi.mocked(toolCache.downloadTool).mockResolvedValue('pathToTool')
|
jest
|
||||||
vi.mocked(fs.readFileSync).mockReturnValue('v1.20.4')
|
.spyOn(toolCache, 'downloadTool')
|
||||||
vi.mocked(toolCache.find).mockReturnValue('pathToCachedTool')
|
.mockReturnValue(Promise.resolve('pathToTool'))
|
||||||
vi.mocked(os.type).mockReturnValue('Windows_NT')
|
jest.spyOn(fs, 'readFileSync').mockReturnValue('v1.20.4')
|
||||||
vi.mocked(fs.chmodSync).mockImplementation()
|
jest.spyOn(toolCache, 'find').mockReturnValue('pathToCachedTool')
|
||||||
vi.mocked(core.addPath).mockImplementation()
|
jest.spyOn(os, 'type').mockReturnValue('Windows_NT')
|
||||||
vi.spyOn(console, 'log').mockImplementation()
|
jest.spyOn(fs, 'chmodSync').mockImplementation()
|
||||||
vi.mocked(core.setOutput).mockImplementation()
|
jest.spyOn(core, 'addPath').mockImplementation()
|
||||||
|
jest.spyOn(console, 'log').mockImplementation()
|
||||||
|
jest.spyOn(core, 'setOutput').mockImplementation()
|
||||||
expect(await run.run()).toBeUndefined()
|
expect(await run.run()).toBeUndefined()
|
||||||
expect(toolCache.downloadTool).toHaveBeenCalledWith(
|
expect(toolCache.downloadTool).toHaveBeenCalledWith(
|
||||||
'https://dl.k8s.io/release/stable.txt'
|
'https://storage.googleapis.com/kubernetes-release/release/stable.txt'
|
||||||
)
|
)
|
||||||
expect(core.getInput).toHaveBeenCalledWith('version', {required: true})
|
expect(core.getInput).toHaveBeenCalledWith('version', {required: true})
|
||||||
expect(core.addPath).toHaveBeenCalledWith('pathToCachedTool')
|
expect(core.addPath).toHaveBeenCalledWith('pathToCachedTool')
|
||||||
|
|||||||
37
src/run.ts
37
src/run.ts
@ -1,25 +1,25 @@
|
|||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
import * as util from 'util'
|
import * as util from 'util'
|
||||||
import * as fs from 'fs'
|
import * as fs from 'fs'
|
||||||
|
|
||||||
import * as toolCache from '@actions/tool-cache'
|
import * as toolCache from '@actions/tool-cache'
|
||||||
import * as core from '@actions/core'
|
import * as core from '@actions/core'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
getkubectlDownloadURL,
|
getkubectlDownloadURL,
|
||||||
getKubectlArch,
|
getKubectlArch,
|
||||||
getExecutableExtension,
|
getExecutableExtension
|
||||||
getLatestPatchVersion
|
} from './helpers'
|
||||||
} from './helpers.js'
|
|
||||||
|
|
||||||
const kubectlToolName = 'kubectl'
|
const kubectlToolName = 'kubectl'
|
||||||
const stableKubectlVersion = 'v1.15.0'
|
const stableKubectlVersion = 'v1.15.0'
|
||||||
const stableVersionUrl = 'https://dl.k8s.io/release/stable.txt'
|
const stableVersionUrl =
|
||||||
|
'https://storage.googleapis.com/kubernetes-release/release/stable.txt'
|
||||||
|
|
||||||
export async function run() {
|
export async function run() {
|
||||||
let version = core.getInput('version', {required: true})
|
let version = core.getInput('version', {required: true})
|
||||||
if (version.toLocaleLowerCase() === 'latest') {
|
if (version.toLocaleLowerCase() === 'latest') {
|
||||||
version = await getStableKubectlVersion()
|
version = await getStableKubectlVersion()
|
||||||
} else {
|
|
||||||
version = await resolveKubectlVersion(version)
|
|
||||||
}
|
}
|
||||||
const cachedPath = await downloadKubectl(version)
|
const cachedPath = await downloadKubectl(version)
|
||||||
|
|
||||||
@ -89,28 +89,3 @@ export async function downloadKubectl(version: string): Promise<string> {
|
|||||||
fs.chmodSync(kubectlPath, '775')
|
fs.chmodSync(kubectlPath, '775')
|
||||||
return kubectlPath
|
return kubectlPath
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function resolveKubectlVersion(version: string): Promise<string> {
|
|
||||||
const cleanedVersion = version.trim()
|
|
||||||
const versionMatch = cleanedVersion.match(
|
|
||||||
/^v?(?<major>\d+)\.(?<minor>\d+)(?:\.(?<patch>\d+))?$/
|
|
||||||
)
|
|
||||||
|
|
||||||
if (!versionMatch?.groups) {
|
|
||||||
throw new Error(
|
|
||||||
`Invalid version format: "${version}". Version must be in "major.minor" or "major.minor.patch" format (e.g., "1.27" or "v1.27.15").`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
const {major, minor, patch} = versionMatch.groups
|
|
||||||
|
|
||||||
if (patch) {
|
|
||||||
// Full version was provided, just ensure it has a 'v' prefix
|
|
||||||
return cleanedVersion.startsWith('v')
|
|
||||||
? cleanedVersion
|
|
||||||
: `v${cleanedVersion}`
|
|
||||||
}
|
|
||||||
|
|
||||||
// Patch version is missing, fetch the latest
|
|
||||||
return await getLatestPatchVersion(major, minor)
|
|
||||||
}
|
|
||||||
|
|||||||
@ -10,7 +10,7 @@ def get_latest_version():
|
|||||||
time_to_sleep = 2
|
time_to_sleep = 2
|
||||||
for _ in range(10):
|
for _ in range(10):
|
||||||
response = requests.get(
|
response = requests.get(
|
||||||
'https://dl.k8s.io/release/stable.txt')
|
'https://storage.googleapis.com/kubernetes-release/release/stable.txt')
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
break
|
break
|
||||||
print('Failed to obtain latest version info, retrying.')
|
print('Failed to obtain latest version info, retrying.')
|
||||||
|
|||||||
@ -1,12 +1,7 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "ES2020",
|
"target": "ES6",
|
||||||
"module": "NodeNext",
|
"module": "commonjs"
|
||||||
"moduleResolution": "NodeNext",
|
|
||||||
"rootDir": "./src",
|
|
||||||
"outDir": "./lib",
|
|
||||||
"skipLibCheck": true,
|
|
||||||
"noEmit": true
|
|
||||||
},
|
},
|
||||||
"exclude": ["node_modules", "**/*.test.ts", "vitest.config.ts"]
|
"exclude": ["node_modules", "test"]
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,10 +0,0 @@
|
|||||||
import {defineConfig} from 'vitest/config'
|
|
||||||
|
|
||||||
export default defineConfig({
|
|
||||||
test: {
|
|
||||||
globals: true,
|
|
||||||
environment: 'node',
|
|
||||||
include: ['**/*.test.ts'],
|
|
||||||
clearMocks: true
|
|
||||||
}
|
|
||||||
})
|
|
||||||
Loading…
x
Reference in New Issue
Block a user