Skip to content

Commit

Permalink
feat: only use sudo if it is installed
Browse files Browse the repository at this point in the history
  • Loading branch information
fyhertz committed Apr 5, 2023
1 parent 53f5157 commit 60ff8ff
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 21 deletions.
20 changes: 18 additions & 2 deletions .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:

jobs:
test:
test-vm:
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -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
Expand Down
20 changes: 6 additions & 14 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
49 changes: 44 additions & 5 deletions scripts/install_repository → utils.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -93,7 +91,48 @@ FZlbblsGJH+TgZAJN7rhLVOudP8lB6iR6T2R7j08jW6K5+R91d1QLh529zej93zmM7gN6J006rXN
BH83C4wFysS9GOkKFWUEFa+A2x65fkr+9trA6fdbbcZsM1isGcqM+C+T2AMf+LSoI18ao7+aDUlv
ojZwJyVEwkiJ3SyoD5P/cBxVH5mVNXUS3XB4C2E=
EOF
)

cat <<EOF > /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

0 comments on commit 60ff8ff

Please sign in to comment.