43 Commits

Author SHA1 Message Date
Aarnav Tale
443c3cc7e1 chore: v1.1.0 2023-01-25 11:08:31 -05:00
Aarnav Tale
7eb54a9e39 fix: remove unnecessary lifecycle hook 2023-01-25 11:08:25 -05:00
Aarnav Tale
22c5cc3864 chore: reflect fixes in deploy task 2023-01-25 10:58:44 -05:00
Aarnav Tale
a0fc1ed3bf fix: maybe it works now 2023-01-25 10:56:26 -05:00
Aarnav Tale
9814a57069 feat: use actions/checkout to create push branch 2023-01-25 10:54:49 -05:00
Aarnav Tale
0597b11a08 fix: don't remove ignored directories 2023-01-25 10:49:49 -05:00
Aarnav Tale
0c2bcc2bf3 fix: resolve issues with actions 2023-01-25 10:44:34 -05:00
Aarnav Tale
aa17f278e9 feat: add dist to gitignore 2023-01-25 10:39:48 -05:00
Aarnav Tale
641b6b74db feat: create deploy ci 2023-01-25 10:39:31 -05:00
Aarnav Tale
c370e32093 fix: orphan branch to push 2023-01-25 10:31:06 -05:00
Aarnav Tale
d9fb2b8307 fix: use readline methods from node:readline to support node 16 2023-01-25 10:28:39 -05:00
Aarnav Tale
b061303a52 feat: build in a separate branch for testing 2023-01-25 10:24:46 -05:00
Aarnav Tale
d37d346399 fix: remove readable references as it only works in node 18 2023-01-25 10:18:56 -05:00
Aarnav Tale
36563347fa chore: build dist 2023-01-25 10:07:57 -05:00
Aarnav Tale
6ac935ba0c fix: run all actions 2023-01-25 10:07:45 -05:00
Aarnav Tale
f7e6d667ef fix: handle blank version 2023-01-25 10:07:23 -05:00
Aarnav Tale
34e21c7e3f feat: add more debug logging 2023-01-25 01:10:58 -05:00
Aarnav Tale
5fe2f65f42 test: use the latest branch for testing 2023-01-25 01:04:23 -05:00
Aarnav Tale
3ed17e8ff6 chore: v1.0.8 2023-01-25 01:00:21 -05:00
Aarnav Tale
4e7c8bb981 fix: use correct post file 2023-01-25 01:00:12 -05:00
Aarnav Tale
c6e917eb5e fix: parse the latest flag properly 2023-01-25 01:00:04 -05:00
Aarnav Tale
7446d29d60 feat: implement github actions for testing 2023-01-25 00:58:09 -05:00
Aarnav Tale
22ac3ce88d chore: add license and update readme 2023-01-25 00:51:07 -05:00
Aarnav Tale
8a18fa720b fix: actually push tags 2023-01-25 00:45:20 -05:00
Aarnav Tale
11bbbb9207 chore: v1.0.7 2023-01-25 00:45:01 -05:00
Aarnav Tale
574fbc2143 fix: make action run using node16 2023-01-25 00:44:53 -05:00
Aarnav Tale
b284f0ba18 fix: add tagging on the push command 2023-01-25 00:43:59 -05:00
Aarnav Tale
558e44eea4 chore: v1.0.6 2023-01-25 00:41:12 -05:00
Aarnav Tale
29d331ef6a feat: add new tag pushing 2023-01-25 00:41:05 -05:00
Aarnav Tale
28eb8b8850 chore: switch default version of kubectl to 'latest' 2023-01-25 00:33:55 -05:00
Aarnav Tale
692ba3e286 feat: implement version lifecycle hook to build dist 2023-01-25 00:33:03 -05:00
Aarnav Tale
c368992ffb feat: implement the action in typescript 2023-01-25 00:30:36 -05:00
Aarnav Tale
880d48e290 chore: create npm project 2023-01-25 00:28:53 -05:00
Aarnav Tale
1b27c62a93 chore: add initial project files for typescript conversion 2023-01-25 00:28:40 -05:00
Aarnav Tale
25403f865e fix: move to /tmp/bin for kubectl location 2023-01-06 12:31:02 -05:00
Aarnav Tale
f6e83f6681 chore: bump default version to 1.26 2023-01-06 12:25:02 -05:00
Aarnav Tale
b88fbf4ad6 chore: bump default kubectl version to 1.25.0 2022-09-04 11:54:55 -04:00
Aarnav Tale
fe5edd2387 feat: revert back to original system 2022-06-27 03:13:27 -04:00
Aarnav Tale
50d08134cc fix: use printf instead of cat 2022-06-27 03:09:11 -04:00
Aarnav Tale
1c88fc9f2b fix: actually write to file on b64 2022-06-27 03:04:28 -04:00
Aarnav Tale
16b4bea4f4 fix: workaround github actions not decoding properly 2022-06-27 02:50:47 -04:00
Aarnav Tale
34de7de05e fix: kubectl version is now properly set 2022-06-27 02:34:59 -04:00
Aarnav Tale
f326ba7d79 fix: add scripts to github actions path 2022-05-14 22:50:16 -04:00
17 changed files with 3955 additions and 29 deletions

3
.eslintrc Normal file
View File

@@ -0,0 +1,3 @@
{
"extends": "tale"
}

29
.github/workflows/deploy.yaml vendored Normal file
View 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 install -g pnpm
pnpm install --frozen-lockfile
pnpm 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 v1
git add -f dist README.md LICENSE action.yaml
git commit -m "chore: create ci release ($GITHUB_SHA)"
git tag --force v1
git push -f --tags origin v1

37
.github/workflows/test.yaml vendored Normal file
View File

@@ -0,0 +1,37 @@
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 install -g pnpm
pnpm install --frozen-lockfile
pnpm 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

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
node_modules/
dist/

5
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,5 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
}

21
LICENSE Normal file
View 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.

View File

@@ -1,8 +1,11 @@
# 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:
@@ -11,7 +14,8 @@ To use this action, add the following step to your GitHub Action workflow:
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 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.23.0`.
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.
```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

View File

@@ -8,18 +8,11 @@ inputs:
kubectl-version:
description: Version of the kubectl CLI to use
required: false
default: v1.23.0
default: latest
base64-kube-config:
description: A base64 encoded reference to your authorization file (~/.kube/config)
required: true
runs:
using: composite
steps:
- name: Configure kubectl CLI
run: setup-kubectl.sh
shell: bash
- name: Authorize kubectl to the cluster
run: login-kubectl.sh
shell: bash
env:
BASE64_KUBE_CONFIG: ${{ inputs.base64-kube-config }}
using: node16
main: dist/index.js
post: dist/index.js

View File

@@ -1,7 +0,0 @@
#!/usr/bin/env bash
if [ ! -d "$HOME/.kube" ]; then
mkdir -p $HOME/.kube
fi
echo "$BASE64_KUBE_CONFIG" | base64 -d > $HOME/.kube/config

21
package.json Normal file
View File

@@ -0,0 +1,21 @@
{
"name": "kubectl-action",
"version": "1.1.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'"
},
"dependencies": {
"@actions/core": "^1.10.0",
"undici": "^5.16.0"
},
"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"
}
}

3630
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,9 +0,0 @@
#!/usr/bin/env bash
curl -LO "https://dl.k8s.io/release/${{ inputs.kubectl-version }}/bin/linux/amd64/kubectl"
curl -LO "https://dl.k8s.io/${{ inputs.kubectl-version }}/bin/linux/amd64/kubectl.sha256"
echo "$(cat kubectl.sha256) kubectl" | sha256sum --check
mkdir $GITHUB_WORKSPACE/bin
mv kubectl $GITHUB_WORKSPACE/bin
echo "$GITHUB_WORKSPACE/bin" >> $GITHUB_PATH

28
src/login.ts Normal file
View File

@@ -0,0 +1,28 @@
import { mkdir, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
import { env } from 'node:process'
import { debug, getInput, saveState, setFailed } from '@actions/core'
export async function setupKubeconfig() {
debug('Running kubectl-action setupKubeconfig()')
if (env.HOME === undefined) {
setFailed('$HOME is not defined')
return
}
const config = getInput('base64-kube-config', {
required: true,
trimWhitespace: true
})
const decoded = Buffer.from(config, 'base64')
.toString('utf8')
const path = join(env.HOME, '.kube')
saveState('kubeconfig-path', path)
await mkdir(path, { recursive: true })
await writeFile(join(path, 'config'), decoded, 'utf8')
}

22
src/main.ts Normal file
View File

@@ -0,0 +1,22 @@
import { debug, getState, setFailed } from '@actions/core'
import { setupKubeconfig } from 'login'
import { installKubectl } from 'setup'
const post = Boolean(getState('isPost'))
if (!post) {
debug('Running kubectl-action setup')
// 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))
})
}

118
src/setup.ts Normal file
View File

@@ -0,0 +1,118 @@
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 { addPath, debug, getInput, saveState, setFailed, warning } from '@actions/core'
import { fetch } from 'undici'
export async function installKubectl() {
debug('Running kubectl-action installKubectl()')
if (env.RUNNER_TEMP === undefined) {
setFailed('$RUNNER_TEMP is not defined')
return
}
const input = getInput('kubectl-version', {
required: false,
trimWhitespace: true
})
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')
return
}
console.log(`Installing kubectl version ${version}`)
const kubectl = await downloadKubectl(version)
if (!kubectl) {
return
}
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
async function fetchLatestVersion() {
const response = await fetch('https://dl.k8s.io/release/stable.txt')
if (!response.ok) {
setFailed(`Failed to fetch latest kubectl version with status ${response.status}`)
return
}
const version = await response.text()
return version.trim()
}
// 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`
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
}

14
src/teardown.ts Normal file
View File

@@ -0,0 +1,14 @@
import { rm } from 'node:fs/promises'
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 })
const configPath = getState('kubeconfig-path')
await rm(configPath, { recursive: true, force: true })
}

14
tsconfig.json Normal file
View File

@@ -0,0 +1,14 @@
{
"exclude": ["dist"],
"compilerOptions": {
"baseUrl": "./src",
"rootDir": "./src",
"outDir": "./dist",
"target": "ESNext",
"module": "CommonJS",
"moduleResolution": "Node",
"skipLibCheck": true,
"strict": true,
"noEmit": true
}
}