Skip to content

Commit

Permalink
Cache phar for each PHP version and channel
Browse files Browse the repository at this point in the history
  • Loading branch information
shivammathur authored Dec 22, 2021
1 parent 72c0d09 commit 5b3f81b
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 36 deletions.
49 changes: 13 additions & 36 deletions .github/workflows/cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,25 @@ 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:
cache:
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 }}
86 changes: 86 additions & 0 deletions cache.sh
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 5b3f81b

Please sign in to comment.