From 60ff8ff08768bede4ddd2525e1274423e074e6d3 Mon Sep 17 00:00:00 2001 From: Simon Guigui Date: Wed, 5 Apr 2023 11:53:03 +0200 Subject: [PATCH] feat: only use sudo if it is installed --- .github/workflows/cicd.yml | 20 +++++++++-- action.yml | 20 ++++------- scripts/install_repository => utils.sh | 49 +++++++++++++++++++++++--- 3 files changed, 68 insertions(+), 21 deletions(-) rename scripts/install_repository => utils.sh (88%) diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index 0320730..78432b4 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -4,7 +4,7 @@ on: push: jobs: - test: + test-vm: runs-on: ubuntu-latest steps: - name: Checkout @@ -18,10 +18,26 @@ jobs: kustomize=4.4.1* kubectl=1.22.4* + test-container: + runs-on: ubuntu-latest + container: + image: debian:bullseye + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Install packages + uses: ./ + with: + packages: | + helm=3.7.2* + kustomize=4.4.1* + kubectl=1.22.4* + release: runs-on: ubuntu-latest if: startsWith(github.ref, 'refs/tags/') - needs: [test] + needs: [test-container, test-vm] steps: - name: Checkout uses: actions/checkout@v3 diff --git a/action.yml b/action.yml index 510de8f..b637325 100644 --- a/action.yml +++ b/action.yml @@ -18,6 +18,7 @@ runs: - name: Fix APT cache permission run: | + source $GITHUB_ACTION_PATH/utils.sh sudo chown -R "$(id -u)" /var/cache/apt /var/lib/apt/lists/ shell: bash @@ -30,19 +31,10 @@ runs: /var/lib/apt/lists key: ${{ steps.get-cache-key.outputs.key }} - - name: Install WakeMeOps repository + - name: Install WakeMeOps repository and install packages run: | - sudo ${{ github.action_path }}/scripts/install_repository - shell: bash - - - name: Update APT cache - run: time sudo apt-get update -qq - shell: bash - - - name: Install packages - run: time sudo apt-get install -y --no-install-recommends $(echo -n "${{ inputs.packages }}") - shell: bash - - - name: Fix APT cache permission - run: sudo chown -R "$(id -u)" /var/cache/apt/archives /var/lib/apt/lists + source $GITHUB_ACTION_PATH/utils.sh + install_repository + time install_packages $(echo -n "${{ inputs.packages }}") + sudo chown -R "$(id -u)" /var/cache/apt/archives /var/lib/apt/lists shell: bash diff --git a/scripts/install_repository b/utils.sh similarity index 88% rename from scripts/install_repository rename to utils.sh index 4b685b2..7674a09 100755 --- a/scripts/install_repository +++ b/utils.sh @@ -1,8 +1,6 @@ #!/usr/bin/env bash -set -e - -cat << EOF | base64 --decode > /etc/apt/trusted.gpg.d/wakemeops-keyring.gpg +WAKEMEOPS_GPG_KEY=$(cat <<-EOF mQINBGGzicYBEADnBgApTxF3fFiAkSJuzfz2qKVXXSuouxCUkOV9owKqIWJ2pYoE7nV9cJ67U7UQ 0+DE0XbNIxBk7GU91kAE/kPjuJImcwIpsq5gHu8PxFfRGNBi4sE6SexZeiQKe+RcnXXst8S9BW45 S5aX1Rb6MZrqKU4/rDVPov63n1YbLkvJM0u1baAku1quvcSea65gVr9xaeNRZyboTpynBSg5Je4k @@ -93,7 +91,48 @@ FZlbblsGJH+TgZAJN7rhLVOudP8lB6iR6T2R7j08jW6K5+R91d1QLh529zej93zmM7gN6J006rXN BH83C4wFysS9GOkKFWUEFa+A2x65fkr+9trA6fdbbcZsM1isGcqM+C+T2AMf+LSoI18ao7+aDUlv ojZwJyVEwkiJ3SyoD5P/cBxVH5mVNXUS3XB4C2E= EOF +) -cat < /etc/apt/sources.list.d/wakemeops.list -deb http://deb.wakemeops.com/wakemeops/ stable ${@-"dev devops secops terminal"} +WAKEMEOPS_APT_SOURCE=$(cat <<-EOF +deb http://deb.wakemeops.com/wakemeops/ stable dev devops secops terminal EOF +) + +sudo () +{ + [[ $EUID = 0 ]] || set -- command sudo "$@" + "$@" +} + +install_packages () +{ + n=0 + max=2 + until [ $n -gt $max ]; do + set +e + ( + sudo apt-get update -qq && + sudo apt-get install -y --no-install-recommends "$@" + ) + CODE=$? + set -e + if [ $CODE -eq 0 ]; then + break + fi + if [ $n -eq $max ]; then + return $CODE + fi + echo "apt failed, retrying" + n=$(($n + 1)) + done +} + +install_repository () { + echo "$WAKEMEOPS_GPG_KEY" | base64 --decode | sudo tee >/dev/null /etc/apt/trusted.gpg.d/wakemeops-keyring.gpg + echo "$WAKEMEOPS_APT_SOURCE" | sudo tee >/dev/null /etc/apt/sources.list.d/wakemeops.list +} + +export DEBIAN_FRONTEND=noninteractive +export -f sudo +export -f install_packages +export -f install_repository