From 8bd845e78c86ed4e60547d0ecca72876a2d03496 Mon Sep 17 00:00:00 2001 From: Niall Byrne <9848926+niall-byrne@users.noreply.github.com> Date: Fri, 17 Feb 2023 19:03:31 -0500 Subject: [PATCH] ci(GITHUB): add caching to workflow --- .github/scripts/ansible_cache.sh | 25 ++++++++ .github/scripts/build.sh | 17 ----- .github/scripts/poetry.sh | 16 +++++ .github/scripts/setup.sh | 3 +- .github/workflows/push.yml | 103 +++++++++++++++++++++++++++---- .github/workflows/release.yml | 33 +++++++--- 6 files changed, 158 insertions(+), 39 deletions(-) create mode 100644 .github/scripts/ansible_cache.sh delete mode 100644 .github/scripts/build.sh create mode 100644 .github/scripts/poetry.sh diff --git a/.github/scripts/ansible_cache.sh b/.github/scripts/ansible_cache.sh new file mode 100644 index 0000000..a59863c --- /dev/null +++ b/.github/scripts/ansible_cache.sh @@ -0,0 +1,25 @@ +#!/bin/bash + +# .github/scripts/ansible_cache.sh +# Creates symlinks for attaching an external cache folder for Ansible. +# Separate folders for Roles, and Collections are maintained. + +# 1: The absolute path of the mount point of the external cache folder. +# 2: The absolute path of the usage point of the cache on the system. + +# CI only script + +set -eo pipefail + +main () { + + MOUNT_FOLDER="${1}" + USAGE_FOLDER="${2}" + + mkdir -p "${MOUNT_FOLDER}" + + ln -sf "${MOUNT_FOLDER}" "${USAGE_FOLDER}" + +} + +main "$@" diff --git a/.github/scripts/build.sh b/.github/scripts/build.sh deleted file mode 100644 index f91e985..0000000 --- a/.github/scripts/build.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash - -# .github/scripts/build.sh -# Builds the Python environment to test the project. - -# CI only script - -set -eo pipefail - -main() { - - pip install poetry - poetry install - -} - -main "$@" diff --git a/.github/scripts/poetry.sh b/.github/scripts/poetry.sh new file mode 100644 index 0000000..71fa638 --- /dev/null +++ b/.github/scripts/poetry.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# .github/scripts/poetry.sh +# Centralized management of poetry installs. + +# CI only script + +set -eo pipefail + +main () { + + pip3 install poetry + +} + +main "$@" diff --git a/.github/scripts/setup.sh b/.github/scripts/setup.sh index c4f196f..b0e860c 100644 --- a/.github/scripts/setup.sh +++ b/.github/scripts/setup.sh @@ -14,8 +14,9 @@ main() { { echo "ANSIBLE_ROLES_PATH=.." echo "BRANCH_OR_TAG=${BRANCH_OR_TAG}" - echo "WEBHOOK_URL=${WEBHOOK_URL}" + echo "CACHE_TTL=$(date +%d)" echo "NOTIFICATION=${PROJECT_NAME} [<${WORKFLOW_URL}|${BRANCH_OR_TAG}>]" + echo "WEBHOOK_URL=${WEBHOOK_URL}" } >> "$GITHUB_ENV" } diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 88b2713..569f60b 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -174,35 +174,65 @@ jobs: steps: - name: Molecule Lint -- Checkout Repository uses: actions/checkout@v3 + with: + path: 'role' + + - name: Molecule Lint -- Setup Environment + run: | + source ./role/.github/scripts/setup.sh + env: + WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }} + + - name: Molecule Lint -- Install Poetry + run: | + source ./role/.github/scripts/poetry.sh - name: Molecule Lint -- Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} - - name: Molecule Lint -- Setup Environment + - name: Molecule Lint -- Initialize Cache Locations run: | - source ./.github/scripts/setup.sh - env: - WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }} + mkdir -p ~/.cache/pypoetry/virtualenvs/ + source ./role/.github/scripts/ansible_cache.sh \ + "$(pwd)/ansible_cache/ansible-compat" \ + ~/.cache/ansible-compat + source ./role/.github/scripts/ansible_cache.sh \ + "$(pwd)/ansible_cache/molecule" \ + ~/.cache/molecule + + - name: Molecule Lint -- Mount Ansible Cache + uses: actions/cache@v3 + with: + key: ansible-${{ hashFiles('./role/requirements.yml') }}-${{ env.CACHE_TTL }} + path: ansible_cache + + - name: Molecule Lint -- Mount Poetry Cache + uses: actions/cache@v3 + with: + key: poetry-${{ hashFiles('./role/pyproject.toml') }}-${{ runner.os }}-${{ env.CACHE_TTL }} + path: ~/.cache/pypoetry/virtualenvs/ - name: Molecule Lint -- Install Requirements run: | - source ./.github/scripts/build.sh + cd role + poetry install - name: Molecule Lint -- Run Linter run: | + cd role poetry run molecule lint - name: Molecule Lint -- Report Job Status (Success) if: env.VERBOSE_NOTIFICATIONS == '1' run: | - ./.github/scripts/notifications.sh "${NOTIFICATION}" ":white_check_mark: molecule linting was successful!" + ./role/.github/scripts/notifications.sh "${NOTIFICATION}" ":white_check_mark: molecule linting was successful!" - name: Molecule Lint -- Report Job Status (Failure) if: failure() run: | - ./.github/scripts/notifications.sh "${NOTIFICATION}" ":x: molecule linting has failed!" + ./role/.github/scripts/notifications.sh "${NOTIFICATION}" ":x: molecule linting has failed!" osx_build: @@ -218,36 +248,83 @@ jobs: steps: - name: OSX Build -- Checkout uses: actions/checkout@v3 + with: + path: 'role' + + - name: OSX Build -- Setup Environment + run: | + source ./role/.github/scripts/setup.sh + env: + WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }} + + - name: OSX Build -- Install Poetry + run: | + source ./role/.github/scripts/poetry.sh - name: OSX Build -- Setup python uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} - - name: OSX Build -- Setup Environment + - name: OSX Build -- Initialize Cache Locations run: | - source ./.github/scripts/setup.sh - source ./.github/scripts/build.sh + mkdir -p ~/.cache/pypoetry/virtualenvs + source ./role/.github/scripts/ansible_cache.sh \ + "$(pwd)/ansible_cache/ansible-compat" \ + ~/.cache/ansible-compat + source ./role/.github/scripts/ansible_cache.sh \ + "$(pwd)/ansible_cache/molecule" \ + ~/.cache/molecule + + - name: OSX Build -- Mount Ansible Cache + uses: actions/cache@v3 + with: + key: ansible-${{ hashFiles('./role/requirements.yml') }}-${{ env.CACHE_TTL }} + path: ansible_cache + + - name: OSX Build -- Mount Poetry Cache + uses: actions/cache@v3 + with: + key: poetry-${{ hashFiles('./role/pyproject.toml') }}-${{ runner.os }}-${{ env.CACHE_TTL }} + path: ~/Library/Caches/pypoetry/virtualenvs + + - name: OSX Build -- Install Requirements + run: | + cd role + poetry install + + - name: OSX Build -- Reuse Cached Dependencies as Scenario + run: | + cd role + poetry run molecule dependency + mv ~/.cache/molecule/role/default ~/.cache/molecule/role/"${SCENARIO}" env: - WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }} + SCENARIO: ${{ matrix.scenario }} - name: OSX Build -- Molecule Scenario run: | + cd role poetry run molecule test -s "${SCENARIO}" env: SCENARIO: ${{ matrix.scenario }} + - name: OSX Build -- Reuse Scenario Dependencies as Cache + run: | + mv ~/.cache/molecule/role/"${SCENARIO}" ~/.cache/molecule/role/default + env: + SCENARIO: ${{ matrix.scenario }} + - name: OSX Build -- Report Job Status (Success) if: env.VERBOSE_NOTIFICATIONS == '1' run: | - ./.github/scripts/notifications.sh "${NOTIFICATION}" ":white_check_mark: OSX ${{ matrix.os }}, molecule test scenario '${SCENARIO}' was successful" + ./role/.github/scripts/notifications.sh "${NOTIFICATION}" ":white_check_mark: OSX ${{ matrix.os }}, molecule test scenario '${SCENARIO}' was successful" env: SCENARIO: ${{ matrix.scenario }} - name: OSX Build -- Report Job Status (Failure) if: failure() run: | - ./.github/scripts/notifications.sh "${NOTIFICATION}" ":x: OSX ${{ matrix.os }}, molecule test scenario '${SCENARIO}' failed!" + ./role/.github/scripts/notifications.sh "${NOTIFICATION}" ":x: OSX ${{ matrix.os }}, molecule test scenario '${SCENARIO}' failed!" env: SCENARIO: ${{ matrix.scenario }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 27531dc..7da87f6 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -34,6 +34,7 @@ jobs: uses: actions/checkout@v3 with: fetch-depth: 0 + path: 'role' ref: ${{ github.event.inputs.TAG }} - name: Publish to Galaxy -- Code Checkout (published release) @@ -41,34 +42,50 @@ jobs: uses: actions/checkout@v3 with: fetch-depth: 0 + path: 'role' + + - name: Publish to Galaxy -- Setup Environment + run: | + source ./role/.github/scripts/setup.sh + env: + WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }} + + - name: Publish to Galaxy -- Install Poetry + run: | + source ./role/.github/scripts/poetry.sh - name: Publish to Galaxy -- Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} - - name: Publish to Galaxy -- Setup Environment + - name: Publish to Galaxy -- Initialize Cache Locations run: | - source .github/scripts/setup.sh - env: - WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }} + mkdir -p ~/.cache/pypoetry/virtualenvs/ + + - name: Publish to Galaxy -- Mount Poetry Cache + uses: actions/cache@v3 + with: + key: poetry-${{ hashFiles('./role/pyproject.toml') }}-${{ runner.os }}-${{ env.CACHE_TTL }} + path: ~/.cache/pypoetry/virtualenvs/ - name: Publish to Galaxy -- Install Requirements run: | - pip install poetry + cd role poetry install - name: Publish to Galaxy -- Trigger Ansible Galaxy Import run: | - poetry run ansible-galaxy role import ${USERNAME} ${PROJECT_NAME} --token ${API_KEY} + cd role + poetry run ansible-galaxy role import ${USERNAME} ${PROJECT_NAME} --token ${API_KEY} env: API_KEY: ${{ secrets.GALAXY_API_KEY }} - name: Publish to Galaxy -- Report Job Status (Success) run: | - ./.github/scripts/notifications.sh "${NOTIFICATION}" ":white_check_mark: automated ansible galaxy import has been completed!" + ./role/.github/scripts/notifications.sh "${NOTIFICATION}" ":white_check_mark: automated ansible galaxy import has been completed!" - name: Publish to Galaxy -- Report Job Status (Failure) if: failure() run: | - ./.github/scripts/notifications.sh "${NOTIFICATION}" ":x: automated ansible galaxy import has failed!" + ./role/.github/scripts/notifications.sh "${NOTIFICATION}" ":x: automated ansible galaxy import has failed!"