From 5b3f81b37184e192b6eb9c660110e27c0faa0d61 Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Wed, 22 Dec 2021 14:20:58 +0000 Subject: [PATCH] Cache phar for each PHP version and channel --- .github/workflows/cache.yml | 49 ++++++--------------- cache.sh | 86 +++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 36 deletions(-) create mode 100644 cache.sh diff --git a/.github/workflows/cache.yml b/.github/workflows/cache.yml index 54efc8f..1b66c9b 100644 --- a/.github/workflows/cache.yml +++ b/.github/workflows/cache.yml @@ -2,6 +2,15 @@ name: 'Cache' on: repository_dispatch: workflow_dispatch: + inputs: + force: + type: choice + description: Update phars by force + options: + - true + - false + default: 'false' + required: false schedule: - cron: '0 * * * *' jobs: @@ -9,41 +18,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - name: Release - run: | - versions=$(curl -sSL https://getcomposer.org/versions) - release_versions=$(curl -sSL https://github.com/shivammathur/composer-cache/releases/latest/download/versions) - if [ "$versions" = "$release_versions" ]; then - echo "No new releases" - else - for v in stable preview snapshot 1 2; do - curl -o composer-$v.phar -sSL https://getcomposer.org$(echo $versions | jq -r ".\"$v\"[].path" | head -n 1) - php -r "try {\$p=new Phar('composer-$v.phar', 0);exit(0);} catch(Exception \$e) {exit(1);}" - if [ $? -eq 1 ]; then - echo "Could not download composer $v" - exit 1; - else - sudo cp ./composer-$v.phar /usr/local/bin/composer - sudo chmod a+x /usr/local/bin/composer - release=$(composer -V) - echo $release - fi - done - curl -o versions -sSL https://getcomposer.org/versions - set -x - assets=() - for asset in ./*.phar; do - assets+=("$asset") - done - assets+=("./versions") - echo "${assets[@]}" - if ! gh release view versions; then - gh release create "versions" "${assets[@]}" -t "versions" -F ./versions - else - gh release upload "versions" "${assets[@]}" --clobber - endpoint=$(gh api repos/${{ github.repository }}/releases/tags/versions -H Accept:application/vnd.github.v3+json | jq -r '.url') - gh api "$endpoint" -H Accept:application/vnd.github.v3+json -H Content-Type:application/json -X PATCH -f body="$(cat ./versions)" --silent - fi - fi + - name: Cache + run: bash cache.sh env: + FORCE: ${{ github.event.inputs.force }} + GITHUB_REPOSITORY: ${{ github.repository }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/cache.sh b/cache.sh new file mode 100644 index 0000000..931e8c1 --- /dev/null +++ b/cache.sh @@ -0,0 +1,86 @@ +#!/usr/bin/env bash + +check_phar() { + phar=$1 + echo "Checking $phar" + php -r "try {\$p=new Phar('$phar', 0);exit(0);} catch(Exception \$e) {exit(1);}" + if [ $? -eq 1 ]; then + echo "ERROR: Broken $phar found" + exit 1 + else + php "$phar" -V + fi +} + +fetch_phar_for_php_version() { + php_version=$1 + channel=$2 + installer=$3 + php_version_semver="$php_version.99" + php_version_id=$(echo "${php_version_semver}" | awk 'BEGIN { FS = "."; } { printf "%d", ($1 * 100 + $2) * 100 + $3;}') + cp -f "$installer" "$installer-${php_version}" + sed -i -e "s/PHP_VERSION_ID/'$php_version_id'/g" -e "s/PHP_VERSION/'$php_version_semver'/g" "$installer-${php_version}" + php "$installer-${php_version}" --"${channel}" --install-dir="$(pwd)" --filename=composer-"${php_version}"-"${channel}".phar + check_phar composer-"${php_version}"-"${channel}".phar +} + +fetch_installer() { + installer=$1 + EXPECTED_CHECKSUM="$(curl -sL https://composer.github.io/installer.sig)" + curl -o "$installer" -sL https://getcomposer.org/installer + ACTUAL_CHECKSUM="$(openssl dgst -sha384 "$installer" | cut -d ' ' -f 2)" + if [ "$EXPECTED_CHECKSUM" != "$ACTUAL_CHECKSUM" ]; then + echo 'ERROR: Invalid installer checksum' + exit 1 + fi +} + +update_release() { + release=$1 + release_notes_file=$2 + assets=() + for asset in ./*.phar; do + assets+=("$asset") + done + assets+=("$release_notes_file") + echo "${assets[@]}" + if ! gh release view "$release"; then + gh release create "$release" "${assets[@]}" -t "$release" -F "$release_notes_file" + else + gh release upload "$release" "${assets[@]}" --clobber + endpoint="$(gh api repos/"$GITHUB_REPOSITORY"/releases/tags/"$release" -H Accept:application/vnd.github.v3+json | jq -r '.url')" + gh api "$endpoint" -H Accept:application/vnd.github.v3+json -H Content-Type:application/json -X PATCH -f body="$(cat "$release_notes_file")" --silent + fi +} + +check_manifest() { + manifest_file=$1 + release=$2 + curl -o "$manifest_file" -sSL https://getcomposer.org/versions + release_versions="$(curl -sSL https://github.com/"$GITHUB_REPOSITORY"/releases/latest/download/"$release")" + if [ "$FORCE" != 'true' ] && [ "$(cat "$manifest_file")" = "$release_versions" ]; then + echo "No new releases" + exit 0 + fi +} + +channels=(stable preview snapshot 1 2) +php_versions=(5.3 5.4 5.5 5.6 7.0 7.1 7.2 7.3 7.4 8.0 8.1 8.2) +release=versions +manifest_file=versions +installer=installer + + +check_manifest "$manifest_file" "$release" + +for channel in "${channels[@]}"; do + curl -o composer-"$channel".phar -sSL https://getcomposer.org"$(jq -r ".\"$channel\"[].path" "$manifest_file" | head -n 1)" + check_phar composer-"$channel".phar + + fetch_installer "$installer" + for php_version in "${php_versions[@]}"; do + fetch_phar_for_php_version "$php_version" "$channel" "$installer" + done +done + +update_release "$release" "$manifest_file"