diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 135ea31..b716818 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -172,6 +172,114 @@ jobs: php -r "exit(trim('${COUNT}') === '0' ? 0 : 255);" env: COUNT: ${{ steps.helmlistcountthird.outputs.count }} + tests-helm-install-input-overruled: + name: "Testing Helm Install (input overruled) ${{ matrix.runner }}" + runs-on: ${{ matrix.runner }} + needs: + - runner-matrix + strategy: + matrix: + runner: ${{ fromJson(needs.runner-matrix.outputs.runner) }} + steps: + - uses: actions/checkout@v1 + - name: Create k8s Kind Cluster + uses: helm/kind-action@v1.2.0 + - name: Get running image ID + id: ps + run: printf "::set-output name=id::%s" $(docker ps --format "{{.ID}}") + env: + IMAGE: ${{ steps.build.outputs.tag }} + - name: Get running image IP + id: inspect + run: printf "::set-output name=ip::%s" $(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${IMAGE_ID}) + env: + IMAGE_ID: ${{ steps.ps.outputs.id }} + - run: sed -i "s/127.0.0.1:32768/${KIND_IP}:6443/g" ~/.kube/config + env: + KIND_IP: ${{ steps.inspect.outputs.ip }} + - name: Make kubeconfig available to Helm3 action + run: | + kubeconfig=$(cat ~/.kube/config) + kubeconfig="${kubeconfig//'%'/'%25'}" + kubeconfig="${kubeconfig//$'\n'/'%0A'}" + kubeconfig="${kubeconfig//$'\r'/'%0D'}" + echo "::set-output name=kubeconfig::$kubeconfig" + id: kubeconfig + - name: List installed packages + uses: ./ + id: helmlistcountfirst + with: + exec: | + printf "count=%s\n" $(helm list | grep ducker-hub-exporter | wc -l) >> $GITHUB_OUTPUT + kubeconfig: ${{ steps.kubeconfig.outputs.kubeconfig }} + - run: | + php -r "exit(trim('${COUNT}') === '0' ? 0 : 255);" + env: + COUNT: ${{ steps.helmlistcountfirst.outputs.count }} + - name: Install ducker-hub-exporter + uses: ./ + id: helm3 + with: + exec: | + helm repo add WyriHaximusNet https://helm.wyrihaximus.net/ + helm install ducker-hub-exporter WyriHaximusNet/docker-hub-exporter --atomic + kubeconfig: ${{ steps.kubeconfig.outputs.kubeconfig }} + - name: List installed packages + uses: ./ + with: + exec: | + helm list + kubeconfig: ${{ steps.kubeconfig.outputs.kubeconfig }} + - run: | + rm ~/.kube -Rf + mkdir ~/.kube + echo "junk" > ~/.kube/config + - name: Get kubeconfig contents + uses: ./ + id: getkubeconfigcontents + with: + exec: | + helm list + printf "::set-output name=cnt::%s" $(cat ~/.kube/config) + kubeconfig: ${{ steps.kubeconfig.outputs.kubeconfig }} + - name: "Assert kubeconfig really contains: junk" + uses: nick-fields/assert-action@v1 + with: + expected: junk + actual: ${{ steps.getkubeconfigcontents.outputs.cnt }} + - name: List installed packages through a overruled kubeconfig + uses: ./ + id: helmlistcountoverruledkubeconfig + with: + exec: | + helm list + printf "::set-output name=count::%s" $(helm list | grep ducker-hub-exporter | wc -l) + kubeconfig: ${{ steps.kubeconfig.outputs.kubeconfig }} + overrule_existing_kubeconfig: "true" + - run: | + php -r "exit(trim('${COUNT}') === '1' ? 0 : 255);" + env: + COUNT: ${{ steps.helmlistcountoverruledkubeconfig.outputs.count }} + - run: | + rm ~/.kube -Rf + - name: UnInstall ducker-hub-exporter + uses: ./ + with: + exec: | + helm uninstall ducker-hub-exporter + kubeconfig: ${{ steps.kubeconfig.outputs.kubeconfig }} + - name: List installed packages + uses: ./ + id: helmlistcountthird + with: + exec: | + helm list + printf "::set-output name=count::%s" $(helm list | grep ducker-hub-exporter | wc -l) + kubeconfig: ${{ steps.kubeconfig.outputs.kubeconfig }} + - run: | + php -r "exit(trim('${COUNT}') === '0' ? 0 : 255);" + env: + COUNT: ${{ steps.helmlistcountthird.outputs.count }} tests-helm-install-file: name: "Testing Helm Install (file) ${{ matrix.runner }}" runs-on: ${{ matrix.runner }} diff --git a/README.md b/README.md index 3c23b90..d798073 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,13 @@ be appended to `~/.kube/config`, and will always be removed afterwards.* * *Required*: `no` * *Type*: `string` +## overrule_existing_kubeconfig + +When this input is set to `"true"` it will swap out the `.kube/config` with the one provided. + +* *Required*: `no` +* *Type*: `string` + ## Output This action has only one output and that's the `number` output. This is the number you see in the HTML URL of the @@ -50,6 +57,7 @@ jobs: with: exec: helm upgrade APP_NAME ./.helm/app/ --install --wait --atomic --namespace=APP_NAMESPACE --values=./.helm/app/values.yaml kubeconfig: '${{ secrets.KUBECONFIG }}' + overrule_existing_kubeconfig: "true" ``` The following example shows how you can use this action to lint your helm files in (for example) `./.helm/app/`. diff --git a/action.yml b/action.yml index 553c996..f137986 100644 --- a/action.yml +++ b/action.yml @@ -10,6 +10,9 @@ inputs: kubeconfig: description: 'The full helm command to run (including helm)' required: false + overrule_existing_kubeconfig: + description: 'The full helm command to run (including helm)' + required: false runs: using: 'node16' main: 'main.js' diff --git a/main.js b/main.js index 74dae6e..5e5e4bf 100644 --- a/main.js +++ b/main.js @@ -27,8 +27,20 @@ async function main() { }); const kubeConfigLocation = homedir + '/.kube/config'; + const kubeConfigLocationTempOld = kubeConfigLocation + '_tmp_f' + Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 13) + 'f'; const kubeConfigExists = fs.existsSync(kubeConfigLocation); - if (kubeConfigExists) { + if (kubeConfigExists && process.env.INPUT_OVERRULE_EXISTING_KUBECONFIG === "true") { + console.log("\033[36mExisting kubeconfig found, but provided kubeconfig is overruling it\033[0m"); + console.log("\033[36mWill be swapping out existing kubeconfig for the duration of the execution of this action\033[0m"); + fs.renameSync(kubeConfigLocation, kubeConfigLocationTempOld); + fs.appendFileSync( + kubeConfigLocation, + "\r\n\r\n" + process.env.INPUT_KUBECONFIG + "\r\n\r\n", + { + mode: 0o644, + } + ); + } else if (kubeConfigExists) { console.log("\033[36mExisting kubeconfig found, using that and ignoring input\033[0m"); } else { console.log("\033[36mUsing kubeconfig from input\033[0m"); @@ -104,10 +116,19 @@ async function main() { fs.unlinkSync(execShFile.name); fs.unlinkSync(dockerKubeConfig); console.log("\033[36m - exec ✅ \033[0m"); - if (!kubeConfigExists) { + if ( + !kubeConfigExists || + ( + kubeConfigExists && process.env.INPUT_OVERRULE_EXISTING_KUBECONFIG === "true" + ) + ) { fs.unlinkSync(kubeConfigLocation); console.log("\033[36m - kubeconfig ✅ \033[0m"); } + if (kubeConfigExists && process.env.INPUT_OVERRULE_EXISTING_KUBECONFIG === "true") { + fs.renameSync(kubeConfigLocationTempOld, kubeConfigLocation); + console.log("\033[36m - kubeconfig restored ✅ \033[0m"); + } } }