Compare commits
52 Commits
v1.0.7
...
dependabot
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
302477698c | ||
|
|
91f29e07f4 | ||
|
|
efb34d6cb2 | ||
|
|
767276df48 | ||
|
|
2b874f780f | ||
|
|
716cfe229d | ||
|
|
e93dd2d865 | ||
|
|
9c4bb5002a | ||
|
|
560e7ff9e2 | ||
|
|
9685b6d613 | ||
|
|
c83ced7ece | ||
|
|
6e8a90cd56 | ||
|
|
5aa0d1bacd | ||
|
|
93d421ead9 | ||
|
|
e49d06518c | ||
|
|
69c8dc994c | ||
|
|
e70828b58c | ||
|
|
d7c0fa7a71 | ||
|
|
9d3c93f137 | ||
|
|
ce29488755 | ||
|
|
f08750dda0 | ||
|
|
a3a1ddb586 | ||
|
|
b413e7e15e | ||
|
|
43525325f3 | ||
|
|
8c4badf72a | ||
|
|
ff8bf47b42 | ||
|
|
4933a15eb6 | ||
|
|
c2bec5ff29 | ||
|
|
443c3cc7e1 | ||
|
|
7eb54a9e39 | ||
|
|
22c5cc3864 | ||
|
|
a0fc1ed3bf | ||
|
|
9814a57069 | ||
|
|
0597b11a08 | ||
|
|
0c2bcc2bf3 | ||
|
|
aa17f278e9 | ||
|
|
641b6b74db | ||
|
|
c370e32093 | ||
|
|
d9fb2b8307 | ||
|
|
b061303a52 | ||
|
|
d37d346399 | ||
|
|
36563347fa | ||
|
|
6ac935ba0c | ||
|
|
f7e6d667ef | ||
|
|
34e21c7e3f | ||
|
|
5fe2f65f42 | ||
|
|
3ed17e8ff6 | ||
|
|
4e7c8bb981 | ||
|
|
c6e917eb5e | ||
|
|
7446d29d60 | ||
|
|
22ac3ce88d | ||
|
|
8a18fa720b |
29
.github/workflows/deploy.yaml
vendored
Normal file
29
.github/workflows/deploy.yaml
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
name: Deploy Action
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- '*'
|
||||
- '!v1'
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
name: Deploy
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout Repository
|
||||
uses: actions/checkout@v3
|
||||
- name: Build latest dist/ folder
|
||||
run: |
|
||||
npm ci
|
||||
npm run build
|
||||
- name: Upload dist/ folder
|
||||
run: |
|
||||
git config --global user.email "<41898282+github-actions[bot]@users.noreply.github.com>"
|
||||
git config --global user.name "github-actions[bot]"
|
||||
git checkout --orphan deploy
|
||||
git add -f dist README.md LICENSE action.yaml
|
||||
git commit -m "chore: create ci release ($GITHUB_SHA)"
|
||||
git tag --force v1
|
||||
git tag --force $GITHUB_REF_NAME
|
||||
git push -f --tags origin deploy
|
||||
36
.github/workflows/test.yaml
vendored
Normal file
36
.github/workflows/test.yaml
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
name: Test Action
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: Build
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout Repository
|
||||
uses: actions/checkout@v3
|
||||
- name: Build latest dist/ folder
|
||||
run: |
|
||||
npm ci
|
||||
npm run build
|
||||
- name: Upload dist/ folder
|
||||
run: |
|
||||
git config --global user.email "<41898282+github-actions[bot]@users.noreply.github.com>"
|
||||
git config --global user.name "github-actions[bot]"
|
||||
git checkout --orphan ci
|
||||
git add -f dist README.md LICENSE action.yaml
|
||||
git commit -m "chore: create ci release ($GITHUB_SHA)"
|
||||
git push -f origin ci
|
||||
test:
|
||||
name: Test
|
||||
runs-on: ubuntu-latest
|
||||
needs: build
|
||||
steps:
|
||||
- name: Setup tale/kubectl-action
|
||||
uses: tale/kubectl-action@ci
|
||||
with:
|
||||
base64-kube-config: ${{ secrets.KUBE_CONFIG }}
|
||||
- name: Test the output of `kubectl cluster-info`
|
||||
run: kubectl cluster-info
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
node_modules/
|
||||
dist/
|
||||
|
||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023 Aarnav Tale
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
41
README.md
41
README.md
@@ -1,17 +1,21 @@
|
||||
# kubectl-action
|
||||
|
||||
GitHub Action to manage a K8s (Kubernetes) cluster using kubectl.
|
||||
|
||||
# Usage
|
||||
## Usage
|
||||
|
||||
To use this action, add the following step to your GitHub Action workflow:
|
||||
|
||||
```yaml
|
||||
- uses: tale/kubectl-action@v1
|
||||
with:
|
||||
base64-kube-config: ${{ secrets.KUBE_CONFIG }}
|
||||
```
|
||||
|
||||
Keep in mind that the action expects a base64 encoded string of your Kubernetes configuration. The simplest way to do that is to run `cat $HOME/.kube/config | base64` and save that output as an action secret.
|
||||
Keep in mind that the action expects a base64 encoded string of your Kubernetes configuration. The simplest way to do that is to run `cat $HOME/.kube/config | base64` and save that output as an action secret. It's additionally possible to generate a config file using the `aws` CLI for EKS or any other tools with other cloud providers.
|
||||
|
||||
It's also possible to specify the version of the [kubectl](https://kubernetes.io/docs/reference/kubectl/) CLI to use. The current default release used by this action is the latest version.
|
||||
|
||||
It's also possible to specify the version of the [kubectl](https://kubernetes.io/docs/reference/kubectl/) CLI to use. The current default release used by this action is `v1.26.0`.
|
||||
```yaml
|
||||
- uses: tale/kubectl-action@v1
|
||||
with:
|
||||
@@ -20,6 +24,7 @@ It's also possible to specify the version of the [kubectl](https://kubernetes.io
|
||||
```
|
||||
|
||||
Once you've completed this setup, you have direct access to the `kubectl` binary and command in the rest of your actions. Here's a full example to give you some inspiration:
|
||||
|
||||
```yaml
|
||||
name: Kubectl Action
|
||||
|
||||
@@ -35,3 +40,33 @@ jobs:
|
||||
base64-kube-config: ${{ secrets.KUBE_CONFIG }}
|
||||
- run: kubectl get pods
|
||||
```
|
||||
|
||||
Here's an example using AWS EKS:
|
||||
|
||||
```yaml
|
||||
name: Kubectl Action
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Configure AWS Credentials
|
||||
uses: aws-actions/configure-aws-credentials@v4
|
||||
with:
|
||||
role-to-assume: arn:aws:iam::123456789100:role/my-github-actions-role
|
||||
aws-region: us-east-2
|
||||
- name: Generate kubeconfig
|
||||
run: |
|
||||
{
|
||||
echo 'EKS_CREDS<<EOF'
|
||||
aws eks update-kubeconfig --region us-east-2 --name my-cluster --dry-run | base64
|
||||
echo EOF
|
||||
} >> $GITHUB_ENV
|
||||
- uses: tale/kubectl-action@v1
|
||||
with:
|
||||
base64-kube-config: ${{ env.EKS_CREDS }}
|
||||
- run: kubectl get pods
|
||||
```
|
||||
|
||||
@@ -13,6 +13,6 @@ inputs:
|
||||
description: A base64 encoded reference to your authorization file (~/.kube/config)
|
||||
required: true
|
||||
runs:
|
||||
using: node16
|
||||
using: node20
|
||||
main: dist/index.js
|
||||
post: dist/post.js
|
||||
post: dist/index.js
|
||||
|
||||
4
dist/index.js
vendored
4
dist/index.js
vendored
File diff suppressed because one or more lines are too long
1
dist/index.js.map
vendored
1
dist/index.js.map
vendored
File diff suppressed because one or more lines are too long
142
dist/licenses.txt
vendored
142
dist/licenses.txt
vendored
@@ -1,142 +0,0 @@
|
||||
@actions/core
|
||||
MIT
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright 2019 GitHub
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
@actions/http-client
|
||||
MIT
|
||||
Actions Http Client for Node.js
|
||||
|
||||
Copyright (c) GitHub, Inc.
|
||||
|
||||
All rights reserved.
|
||||
|
||||
MIT License
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||
associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
||||
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
||||
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
|
||||
busboy
|
||||
MIT
|
||||
Copyright Brian White. All rights reserved.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to
|
||||
deal in the Software without restriction, including without limitation the
|
||||
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
sell copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
IN THE SOFTWARE.
|
||||
|
||||
streamsearch
|
||||
MIT
|
||||
Copyright Brian White. All rights reserved.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to
|
||||
deal in the Software without restriction, including without limitation the
|
||||
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
sell copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
IN THE SOFTWARE.
|
||||
|
||||
tunnel
|
||||
MIT
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2012 Koichi Kobayashi
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
|
||||
undici
|
||||
MIT
|
||||
MIT License
|
||||
|
||||
Copyright (c) Matteo Collina and Undici contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
|
||||
uuid
|
||||
MIT
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2010-2020 Robert Kieffer and other contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
1
dist/sourcemap-register.js
vendored
1
dist/sourcemap-register.js
vendored
File diff suppressed because one or more lines are too long
8565
package-lock.json
generated
Normal file
8565
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
21
package.json
21
package.json
@@ -1,22 +1,21 @@
|
||||
{
|
||||
"name": "kubectl-action",
|
||||
"version": "1.0.7",
|
||||
"version": "1.4.0",
|
||||
"scripts": {
|
||||
"dev": "ncc -smw --license licenses.txt build src/main.ts",
|
||||
"build": "ncc -sm --license licenses.txt build src/main.ts",
|
||||
"push": "np --no-cleanup --no-publish --no-tests --message 'chore: v%s' && git tag --force v1 && git push -f",
|
||||
"version": "pnpm run build && git add dist"
|
||||
"push": "np --no-cleanup --no-publish --no-tests --message 'chore: v%s'"
|
||||
},
|
||||
"dependencies": {
|
||||
"@actions/core": "^1.10.0",
|
||||
"undici": "^5.16.0"
|
||||
"@actions/core": "^1.10.1",
|
||||
"@actions/tool-cache": "^2.0.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.11.30",
|
||||
"@vercel/ncc": "^0.38.1",
|
||||
"eslint": "^8.57.0",
|
||||
"eslint-config-tale": "^1.0.16",
|
||||
"np": "^9.2.0",
|
||||
"typescript": "^5.4.2"
|
||||
}
|
||||
}
|
||||
|
||||
3630
pnpm-lock.yaml
generated
3630
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
30
src/main.ts
30
src/main.ts
@@ -1,12 +1,34 @@
|
||||
/* eslint-disable unicorn/prefer-top-level-await */
|
||||
import { env, exit, 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 (env.RUNNER_OS === 'Windows' || platform === 'win32') {
|
||||
setFailed('kubectl-action does not support Windows')
|
||||
exit(1)
|
||||
}
|
||||
|
||||
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 teardown kubectl (this is a bug in kubectl-action): ')
|
||||
debug(JSON.stringify(error))
|
||||
})
|
||||
} else {
|
||||
debug('Running kubectl-action setup')
|
||||
installKubectl()
|
||||
// eslint-disable-next-line no-async-promise-executor
|
||||
new Promise(async () => {
|
||||
await installKubectl()
|
||||
debug('kubectl-action setup complete')
|
||||
|
||||
await setupKubeconfig()
|
||||
debug('kubectl-action kubeconfig setup complete')
|
||||
})
|
||||
// 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))
|
||||
|
||||
127
src/setup.ts
127
src/setup.ts
@@ -1,12 +1,9 @@
|
||||
import { createHash, randomUUID } from 'node:crypto'
|
||||
import { createWriteStream } from 'node:fs'
|
||||
import { mkdir } from 'node:fs/promises'
|
||||
import { join } from 'node:path'
|
||||
import { env, stdout } from 'node:process'
|
||||
import { Readable } from 'node:stream'
|
||||
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 { fetch } from 'undici'
|
||||
import { addPath, debug, getInput, setFailed } from '@actions/core'
|
||||
import { cacheFile, downloadTool, find } from '@actions/tool-cache'
|
||||
|
||||
export async function installKubectl() {
|
||||
debug('Running kubectl-action installKubectl()')
|
||||
@@ -21,7 +18,8 @@ export async function installKubectl() {
|
||||
trimWhitespace: true
|
||||
})
|
||||
|
||||
const version = input || await fetchLatestVersion()
|
||||
const version = input === 'latest' || input === '' ? await fetchLatestVersion() : input
|
||||
debug(`kubectl-version: ${version ?? 'undefined'}`)
|
||||
|
||||
if (!version?.startsWith('v')) {
|
||||
setFailed('Unable to determine the `kubectl` version to install')
|
||||
@@ -30,27 +28,15 @@ 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)
|
||||
|
||||
const stream = createWriteStream(join(path, 'kubectl'))
|
||||
|
||||
kubectl.pipe(stream)
|
||||
|
||||
console.log(`Installing kubectl to ${path}`)
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
stream.on('finish', resolve)
|
||||
stream.on('error', reject)
|
||||
})
|
||||
|
||||
addPath(path)
|
||||
}
|
||||
|
||||
// Fetches the latest kubectl version from the Kubernetes release server
|
||||
@@ -66,61 +52,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})`)
|
||||
|
||||
const hashResponse = await fetch(hashUrl)
|
||||
if (!hashResponse.ok) {
|
||||
warning(`Skipping checksum verification for kubectl ${version}`)
|
||||
}
|
||||
|
||||
const hash = hashResponse.ok ? await hashResponse.text() : ''
|
||||
|
||||
const response = await fetch(url)
|
||||
if (!response.ok || !response.body) {
|
||||
setFailed(`Failed to download kubectl with status ${response.status}`)
|
||||
return
|
||||
}
|
||||
|
||||
const hashStream = createHash('sha256')
|
||||
const body = Readable.fromWeb(response.body)
|
||||
const size = Number(response.headers.get('content-length'))
|
||||
|
||||
return new Promise<Readable | void>((resolve, reject) => {
|
||||
let downloaded = 0
|
||||
let progressed = 0
|
||||
|
||||
body.on('data', (chunk: Buffer) => {
|
||||
hashStream.update(chunk)
|
||||
downloaded += chunk.length
|
||||
|
||||
if (Math.floor((downloaded / size) * 80) > progressed) {
|
||||
stdout.clearLine(0)
|
||||
stdout.cursorTo(0)
|
||||
|
||||
progressed++
|
||||
stdout.write(`[${'='.repeat(progressed)}>${' '.repeat(80 - progressed)}]`)
|
||||
}
|
||||
})
|
||||
|
||||
body.on('end', () => {
|
||||
stdout.clearLine(0)
|
||||
stdout.cursorTo(0)
|
||||
console.log(`[${'='.repeat(80)}]`)
|
||||
|
||||
const hashSum = hashStream.digest('hex')
|
||||
if (hashResponse.ok && hashSum !== hash) {
|
||||
setFailed(`Checksum verification failed for kubectl ${version}`)
|
||||
resolve()
|
||||
}
|
||||
|
||||
resolve(body)
|
||||
})
|
||||
|
||||
body.on('error', reject)
|
||||
})
|
||||
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