Skip to content

Commit

Permalink
fix: make working dir part of cache key and improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ramsey committed Dec 23, 2021
1 parent 6b7b7a1 commit f22409e
Show file tree
Hide file tree
Showing 38 changed files with 281 additions and 740 deletions.
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
/conventional-commits.json export-ignore
/docs/ export-ignore
/SECURITY.md export-ignore
/subdirectory/ export-ignore
/tests/ export-ignore
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- "--ignore-platform-reqs"
- ""
working-directory:
- "subdirectory"
- "tests/fixtures/with-lock-file"
- ""
ignore-cache:
- "yes"
Expand Down
25 changes: 17 additions & 8 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ runs:
using: "composite"
steps:
- name: "Determine PHP version"
id: "php-version"
id: "php"
shell: "bash"
run: "${GITHUB_ACTION_PATH}/bin/php_version.sh"

Expand All @@ -44,30 +44,37 @@ runs:
shell: "bash"
run: "${GITHUB_ACTION_PATH}/bin/should_cache.sh \"${{ inputs.ignore-cache }}\""

- name: "Determine Composer cache directory"
id: "composer-cache"
- name: "Determine Composer paths"
id: "composer"
if: steps.should-cache.outputs.do-cache == 1
shell: "bash"
run: "${GITHUB_ACTION_PATH}/bin/composer_cache_dir.sh"
run: |
${GITHUB_ACTION_PATH}/bin/composer_paths.sh \
"" \
"${{ inputs.working-directory }}" \
"${{ steps.php.outputs.path }}"
- name: "Determine cache key"
id: "cache-key"
if: steps.should-cache.outputs.do-cache == 1
shell: "bash"
run: |
composer_json="${{ steps.composer.outputs.json }}"
composer_lock="${{ steps.composer.outputs.lock }}"
${GITHUB_ACTION_PATH}/bin/cache_key.sh \
"${{ runner.os }}" \
"${{ steps.php-version.outputs.php }}" \
"${{ steps.php.outputs.version }}" \
"${{ inputs.dependency-versions }}" \
"${{ inputs.composer-options }}" \
"${{ hashFiles('**/composer.json', '**/composer.lock') }}" \
"${{ inputs.custom-cache-key }}"
"${{ inputs.custom-cache-key }}" \
"${{ inputs.working-directory }}"
- name: "Cache Composer dependencies"
if: steps.should-cache.outputs.do-cache == 1
uses: "actions/cache@v2"
with:
path: "${{ steps.composer-cache.outputs.directory }}"
path: "${{ steps.composer.outputs.cache-dir }}"
key: "${{ steps.cache-key.outputs.key }}"
restore-keys: |
${{ env.CACHE_RESTORE_KEY }}
Expand All @@ -78,4 +85,6 @@ runs:
${GITHUB_ACTION_PATH}/bin/composer_install.sh \
"${{ inputs.dependency-versions }}" \
"${{ inputs.composer-options }}" \
"${{ inputs.working-directory }}"
"${{ inputs.working-directory }}" \
"${{ steps.php.outputs.path }}" \
"${{ steps.composer.outputs.command }}"
15 changes: 10 additions & 5 deletions bin/cache_key.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dependency_versions="${3:-locked}"
composer_options="${4}"
files_hash="${5}"
custom_cache_key="${6}"
working_directory="${7}"

key=()
restore_key=()
Expand All @@ -20,12 +21,16 @@ function join_by {
if [ -n "${custom_cache_key}" ]; then
key+=("${custom_cache_key}")
else
key+=("${runner_os}" "php" "${php_version}" "composer")
key+=(
"${runner_os}"
"php"
"${php_version}"
"composer"
"${composer_options}"
"${dependency_versions}"
"${working_directory}"
)

key+=("${dependency_versions}")
restore_key=("$(join_by - ${key[@]/#/})-" "${restore_key[@]}")

key+=("${composer_options}")
restore_key=("$(join_by - ${key[@]/#/})-" "${restore_key[@]}")

key+=("${files_hash}")
Expand Down
6 changes: 0 additions & 6 deletions bin/composer_cache_dir.sh

This file was deleted.

4 changes: 3 additions & 1 deletion bin/composer_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
dependency_versions="${1:-locked}"
additional_composer_options="${2}"
working_directory="${3}"
php_path="${4:-$(which php)}"
composer_path="${5:-$(which composer)}"

composer_command="update"
composer_options=(
Expand All @@ -29,4 +31,4 @@ if [ -n "${working_directory}" ]; then
fi

echo "::notice title=Composer::Using the following Composer command: 'composer ${composer_command} ${composer_options[*]}'"
composer "${composer_command}" ${composer_options[*]}
"${php_path}" "${composer_path}" "${composer_command}" ${composer_options[*]}
62 changes: 62 additions & 0 deletions bin/composer_paths.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env bash

composer_path="${1:-$(which composer)}"
working_directory="${2:-.}"
php_path="${3:-$(which php)}"

function test_composer {
"${php_path}" "${composer_path}" --version > /dev/null 2>&1
}

function validate_composer {
"${php_path}" "${composer_path}" \
validate \
--no-check-publish \
--no-check-version \
--working-dir "${working_directory}" \
> /dev/null 2>&1
}

if ! test_composer; then
echo "::error title=Composer Not Found::Unable to find Composer at '${composer_path}'"
exit 1
fi

composer_json="composer.json"
composer_lock="composer.lock"

if [ -n "${working_directory}" ]; then
if [ ! -d "${working_directory}" ]; then
echo "::error title=Working Directory Not Found::Unable to find working directory at '${working_directory}'"
exit 1
fi

composer_json="${working_directory}/composer.json"
composer_lock="${working_directory}/composer.lock"
fi

if [ ! -f "${composer_json}" ]; then
echo "::error title=composer.json Not Found::Unable to find composer.json at '${composer_json}'"
exit 1
fi

if ! validate_composer; then
echo "::error title=Invalid composer.json::The composer.json file at '${composer_json}' does not validate; run 'composer validate' to check for errors"
exit 1
fi

if [ ! -f "${composer_lock}" ]; then
echo "::notice title=composer.lock Not Found::Unable to find composer.lock at '${composer_lock}'"
composer_lock=""
fi

cache_dir="$($composer_path config cache-dir)"

echo "::notice title=Composer Path::Composer path is '${composer_path}'"
echo "::notice title=Composer Cache::Composer cache directory found at '${cache_dir}'"
echo "::notice title=composer.json::File composer.json found at '${composer_json}'"
echo "::notice title=composer.lock::File composer.lock path computed as '${composer_lock}'"
echo "::set-output name=command::${composer_path}"
echo "::set-output name=cache-dir::${cache_dir}"
echo "::set-output name=json::${composer_json}"
echo "::set-output name=lock::${composer_lock}"
18 changes: 16 additions & 2 deletions bin/php_version.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
#!/usr/bin/env bash

php_version=$(php -r 'echo phpversion();')
php_path="${1:-$(which php)}"

# Test PHP command.
function test_php {
$php_path -v > /dev/null 2>&1
}

if ! test_php; then
echo "::error title=PHP Not Found::Unable to find PHP at '${php_path}'"
exit 1
fi

php_version=$($php_path -r 'echo phpversion();')

echo "::notice title=PHP Path::PHP path is '${php_path}'"
echo "::notice title=PHP Version::PHP version is '${php_version}'"
echo "::set-output name=php::${php_version}"
echo "::set-output name=path::${php_path}"
echo "::set-output name=version::${php_version}"
14 changes: 0 additions & 14 deletions subdirectory/composer.json

This file was deleted.

Loading

0 comments on commit f22409e

Please sign in to comment.