From 2005b9fff84ea7b7b0503244ac7ef30107ee2012 Mon Sep 17 00:00:00 2001 From: Luigi Operoso <40476330+brokenpip3@users.noreply.github.com> Date: Mon, 14 Aug 2023 17:00:52 +0200 Subject: [PATCH] feat: remove sudo if inside home directory, add cache for bats-libs, add outputs (#9) * remove sudo if inside home directory * fix: needs in local home action * fix: set the right libraries path * add outputs, support if cache in tests * add cache for all the libraries * add version to cache key * lowercase true * add debug * add cache explanation in README * debug output run only if in debug mode --- .../test-local-action-inside-home.yaml | 108 ++++++++++++++++ README.md | 18 ++- action.yaml | 118 ++++++++++++++---- 3 files changed, 221 insertions(+), 23 deletions(-) create mode 100644 .github/workflows/test-local-action-inside-home.yaml diff --git a/.github/workflows/test-local-action-inside-home.yaml b/.github/workflows/test-local-action-inside-home.yaml new file mode 100644 index 0000000..0f12a8a --- /dev/null +++ b/.github/workflows/test-local-action-inside-home.yaml @@ -0,0 +1,108 @@ +name: "Test local action inside $HOME" +on: + workflow_dispatch: + pull_request: + release: + types: [published] + push: + branches: ["main"] + tags: ["*"] + +jobs: + local_test_home: + runs-on: ubuntu-latest + env: + BATS_LIB_PATH: "${{ github.workspace }}/tests" + TERM: xterm + name: local default + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Setup Bats and Bats libs + id: setup-bats-libs + uses: ./ + with: + support-clean: "false" + support-path: "${{ github.workspace }}/tests/bats-support" + assert-clean: "false" + assert-path: "${{ github.workspace }}/tests/bats-assert" + detik-clean: "false" + detik-path: "${{ github.workspace }}/tests/bats-detik" + file-clean: "false" + file-path: "${{ github.workspace }}/tests/bats-file" + - name: Execute test to check Bats-support + if: steps.setup-bats-libs.outputs.support-installed == 'true' + run: | + cd /tmp/bats-support/ + bats test + - name: Execute test to check Bats-assert + if: steps.setup-bats-libs.outputs.assert-installed == 'true' + run: | + ls -l $BATS_LIB_PATH/ + cd /tmp/bats-assert/ + bats test + - name: Execute test to check Bats-detik + if: steps.setup-bats-libs.outputs.detik-installed == 'true' + run: | + cd /tmp/bats-detik/ + bats tests + - name: Execute test to check Bats-file + if: steps.setup-bats-libs.outputs.file-installed == 'true' + # Currently the bats-file tests are broken + # in gh runner env + continue-on-error: true + run: | + cd /tmp/bats-file/ + bats test + - name: Execute example tests + run: + bats -T -p tests + local_test_home_trigger_cache: + needs: [local_test_home] + runs-on: ubuntu-latest + env: + BATS_LIB_PATH: "${{ github.workspace }}/tests" + TERM: xterm + name: local default cache + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Setup Bats and Bats libs + id: setup-bats-libs + uses: ./ + with: + support-clean: "false" + support-path: "${{ github.workspace }}/tests/bats-support" + assert-clean: "false" + assert-path: "${{ github.workspace }}/tests/bats-assert" + detik-clean: "false" + detik-path: "${{ github.workspace }}/tests/bats-detik" + file-clean: "false" + file-path: "${{ github.workspace }}/tests/bats-file" + - name: Execute test to check Bats-support + if: steps.setup-bats-libs.outputs.support-installed == 'true' + run: | + cd /tmp/bats-support/ + bats test + - name: Execute test to check Bats-assert + if: steps.setup-bats-libs.outputs.assert-installed == 'true' + run: | + ls -l $BATS_LIB_PATH/ + cd /tmp/bats-assert/ + bats test + - name: Execute test to check Bats-detik + if: steps.setup-bats-libs.outputs.detik-installed == 'true' + run: | + cd /tmp/bats-detik/ + bats tests + - name: Execute test to check Bats-file + if: steps.setup-bats-libs.outputs.file-installed == 'true' + # Currently the bats-file tests are broken + # in gh runner env + continue-on-error: true + run: | + cd /tmp/bats-file/ + bats test + - name: Execute example tests + run: + bats -T -p tests diff --git a/README.md b/README.md index 687aa4a..6dc4c99 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,10 @@ For example, if you want to install `bats-support` in the `./test/bats-support` support-path: ${{ github.workspace }}/test/bats-support ``` +## About Caching + +The caching mechanism for the `bats binary` is always available. However, the caching for the `bats libraries` is dependent on the location of each library path. If a library is located within the $HOME directory, caching is supported. Conversely, if a library is located outside the $HOME directory (which is the default location per each library), caching is not supported. This is due to a known limitation with sudo and the cache action, as detailed in this GitHub issue: https://github.com/actions/toolkit/issues/946. + ## Inputs | Key | Default | Required | Description | @@ -63,6 +67,16 @@ For example, if you want to install `bats-support` in the `./test/bats-support` | file-path | `/usr/lib/bats-file` | false | Bats-file path | | file-clean | `true` | false | Bats-file: clean temp files | +## Outputs + +| Key | Description | +|------------------|------------------------------------------------| +| bats-installed | True/False if bats has been installed | +| support-installed| True/False if bats-support has been installed | +| assert-installed | True/False if bats-assert has been installed | +| detik-installed | True/False if bats-detik has been installed | +| file-installed | True/False if bats-file has been installed | + ## TODO * [X] Add more tests @@ -73,6 +87,6 @@ For example, if you want to install `bats-support` in the `./test/bats-support` * [X] Better Readme -* [ ] Remove sudo in case of bats libs installed in $HOME +* [x] Remove sudo in case of bats libs installed in $HOME -* [ ] Add cache for bats libs +* [x] Add cache for bats libs diff --git a/action.yaml b/action.yaml index 14ee49c..5756af0 100644 --- a/action.yaml +++ b/action.yaml @@ -81,6 +81,23 @@ inputs: description: "Bats-file: clean temp files" required: false default: true +outputs: + bats-installed: + description: "True/False if bats has been installed" + value: ${{ (steps.bats-install.outputs.bats-installed != '') }} + support-installed: + description: "True/False if bats-support has been installed" + value: ${{ (steps.support-install.outputs.support-installed != '') }} + assert-installed: + description: "True/False if bats-assert has been installed" + value: ${{ (steps.assert-install.outputs.assert-installed != '') }} + detik-installed: + description: "True/False if bats-detik has been installed" + value: ${{ (steps.detik-install.outputs.detik-installed != '') }} + file-installed: + description: "True/False if bats-file has been installed" + value: ${{ (steps.file-install.outputs.file-installed != '') }} + runs: using: composite steps: @@ -93,12 +110,14 @@ runs: path: | ~/.local/share/bats ~/.local/bin/bats - key: ${{ runner.os }}-bats + key: ${{ runner.os }}-bats-${{ inputs.bats-version }} + - name: "Download and install Bats" if: inputs.bats-install == 'true' && steps.bats-cache.outputs.cache-hit != 'true' + id: bats-install shell: bash run: | - # In $HOME to avoid sudo requirements that eventually will be removed for the library as well + # In $HOME to avoid sudo requirements VERSION=${{ inputs.bats-version }} DESTDIR="$HOME/.local/bin" LIBDIR="$HOME/.local/share/bats" @@ -131,12 +150,21 @@ runs: install -Dm755 lib/bats-core/* -t "${LIBDIR}" echo "Bats v$VERSION installed in $DESTDIR" + echo "$DESTDIR" >> "$GITHUB_PATH" + echo "bats-installed=true" >> $GITHUB_OUTPUT rm -rf ${TEMPDIR} || exit 0 - echo "$DESTDIR" >> "$GITHUB_PATH" + - name: "Set cache for Bats-support" + uses: actions/cache@v3 + if: inputs.support-install == 'true' + id: support-cache + with: + path: ${{ inputs.support-path }} + key: ${{ runner.os }}-bats-support-${{ inputs.support-version }} - name: "Download and install Bats-support" - if: inputs.support-install == 'true' + if: inputs.support-install == 'true' && steps.support-cache.outputs.cache-hit != 'true' + id: support-install shell: bash run: | VERSION=${{ inputs.support-version }} @@ -145,19 +173,31 @@ runs: url="https://github.com/bats-core/bats-support/archive/refs/tags/v${VERSION}.tar.gz" mkdir -p ${TEMPDIR} - sudo mkdir -p ${DESTDIR}/src/ + [[ "${DESTDIR}" == "$HOME"* ]] && CMD="" || CMD="sudo" + ${CMD} mkdir -p ${DESTDIR}/src/ curl -sL ${url} | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR} - # Archlinux style, except that we are not in a fakeroot env so we need to use sudo - sudo install -Dm755 load.bash ${DESTDIR}/load.bash + # Archlinux style, except that we are not in a fakeroot env + ${CMD} install -Dm755 load.bash ${DESTDIR}/load.bash for fn in src/*.bash; do - sudo install -Dm755 $fn \ + ${CMD} install -Dm755 $fn \ ${DESTDIR}/src/$(basename $fn) done echo "Bats Support v$VERSION installed in $DESTDIR" + echo "support-installed=true" >> $GITHUB_OUTPUT # Cleanup bats-support if required [[ "${{ inputs.support-clean }}" = "true" ]] && rm -rf ${TEMPDIR} || exit 0 - - name: "Download and install Bats-assert" + + - name: "Set cache for Bats-assert" + uses: actions/cache@v3 if: inputs.assert-install == 'true' + id: assert-cache + with: + path: ${{ inputs.assert-path }} + key: ${{ runner.os }}-bats-assert-${{ inputs.assert-version }} + + - name: "Download and install Bats-assert" + if: inputs.assert-install == 'true' && steps.assert-cache.outputs.cache-hit != 'true' + id: assert-install shell: bash run: | VERSION=${{ inputs.assert-version }} @@ -166,19 +206,31 @@ runs: url="https://github.com/bats-core/bats-assert/archive/refs/tags/v${VERSION}.tar.gz" mkdir -p ${TEMPDIR} - sudo mkdir -p ${DESTDIR}/src/ + [[ "${DESTDIR}" == "$HOME"* ]] && CMD="" || CMD="sudo" + ${CMD} mkdir -p ${DESTDIR}/src/ curl -sL ${url} | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR} - # Archlinux style, except that we are not in a fakeroot env so we need to use sudo - sudo install -Dm755 load.bash ${DESTDIR}/load.bash + # Archlinux style, except that we are not in a fakeroot env + ${CMD} install -Dm755 load.bash ${DESTDIR}/load.bash for fn in src/*.bash; do - sudo install -Dm755 $fn \ + ${CMD} install -Dm755 $fn \ ${DESTDIR}/src/$(basename $fn) done echo "Bats Assert v$VERSION installed in $DESTDIR" + echo "assert-installed=true" >> "$GITHUB_OUTPUT" # Cleanup bats-support if required [[ "${{ inputs.assert-clean }}" = "true" ]] && rm -rf ${TEMPDIR} || exit 0 - - name: "Download and install Bats-detik" + + - name: "Set cache for Bats-detik" + uses: actions/cache@v3 if: inputs.detik-install == 'true' + id: detik-cache + with: + path: ${{ inputs.detik-path }} + key: ${{ runner.os }}-bats-detik-${{ inputs.detik-version }} + + - name: "Download and install Bats-detik" + if: inputs.detik-install == 'true' && steps.detik-cache.outputs.cache-hit != 'true' + id: detik-install shell: bash run: | VERSION=${{ inputs.detik-version }} @@ -187,18 +239,30 @@ runs: url="https://github.com/bats-core/bats-detik/archive/refs/tags/v${VERSION}.tar.gz" mkdir -p ${TEMPDIR} - sudo mkdir -p ${DESTDIR}/src/ + [[ "${DESTDIR}" == "$HOME"* ]] && CMD="" || CMD="sudo" + ${CMD} mkdir -p ${DESTDIR}/src/ curl -sL ${url} | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR} # Archlinux style, except that we are not in a fakeroot env so we need to use sudo for fn in lib/*.bash; do - sudo install -Dm755 $fn \ + ${CMD} install -Dm755 $fn \ ${DESTDIR}/$(basename $fn) done echo "Bats Detik v$VERSION installed in $DESTDIR" + echo "detik-installed=true" >> "$GITHUB_OUTPUT" # Cleanup bats-support if required [[ "${{ inputs.detik-clean }}" = "true" ]] && rm -rf ${TEMPDIR} || exit 0 - - name: "Download and install Bats-file" + + - name: "Set cache for Bats-file" + uses: actions/cache@v3 if: inputs.file-install == 'true' + id: file-cache + with: + path: ${{ inputs.file-path }} + key: ${{ runner.os }}-bats-file-${{ inputs.file-version }} + + - name: "Download and install Bats-file" + if: inputs.file-install == 'true' && steps.file-cache.outputs.cache-hit != 'true' + id: file-install shell: bash run: | VERSION=${{ inputs.file-version }} @@ -207,14 +271,26 @@ runs: url="https://github.com/bats-core/bats-file/archive/refs/tags/v${VERSION}.tar.gz" mkdir -p ${TEMPDIR} - sudo mkdir -p ${DESTDIR}/src/ + [[ "${DESTDIR}" == "$HOME"* ]] && CMD="" || CMD="sudo" + ${CMD} mkdir -p ${DESTDIR}/src/ curl -sL ${url} | tar xz -C ${TEMPDIR} --strip-components 1 && cd ${TEMPDIR} - # Archlinux style, except that we are not in a fakeroot env so we need to use sudo - sudo install -Dm755 load.bash ${DESTDIR}/load.bash + # Archlinux style, except that we are not in a fakeroot env + ${CMD} install -Dm755 load.bash ${DESTDIR}/load.bash for fn in src/*.bash; do - sudo install -Dm755 $fn \ + ${CMD} install -Dm755 $fn \ ${DESTDIR}/src/$(basename $fn) done echo "Bats File v$VERSION installed in $DESTDIR" + echo "file-installed=true" >> "$GITHUB_OUTPUT" # Cleanup bats-support if required [[ "${{ inputs.file-clean }}" = "true" ]] && rm -rf ${TEMPDIR} || exit 0 + + - name: "Debug print if installed" + if: runner.debug == '1' + shell: bash + run: | + echo "Bats installed: ${{ (steps.bats-install.outputs.bats-installed != '') }}" + echo "Support installed: ${{ (steps.support-install.outputs.support-installed != '') }}" + echo "Assert installed: ${{ (steps.assert-install.outputs.assert-installed != '') }}" + echo "Detik installed: ${{ (steps.detik-install.outputs.detik-installed != '') }}" + echo "File installed: ${{ (steps.file-install.outputs.file-installed != '') }}"