From 1693db3f0707e0d1201282dab40857cd341d6664 Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Thu, 11 Mar 2021 02:38:35 +0530 Subject: [PATCH] Cache PHP post-install Cache same PHP packages for all versions Refactor workflow Remove bintray and use cloudsmith for build mirror Use latest zstd from homebrew --- .github/workflows/build.yml | 129 ------------------------------------ .github/workflows/cache.yml | 64 ++++++++++++++++++ .gitignore | 1 - LICENSE | 2 +- README.md | 9 ++- src/configure-git.sh | 8 +++ src/install-php.sh | 11 +++ src/install-requirements.sh | 9 +++ src/install-zstd.sh | 4 ++ src/install.sh | 22 ++++++ src/package.sh | 21 ++++++ src/release.sh | 40 +++++++++++ 12 files changed, 186 insertions(+), 134 deletions(-) delete mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/cache.yml delete mode 100644 .gitignore create mode 100644 src/configure-git.sh create mode 100644 src/install-php.sh create mode 100644 src/install-requirements.sh create mode 100644 src/install-zstd.sh create mode 100644 src/install.sh create mode 100644 src/package.sh create mode 100644 src/release.sh diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 63530a7..0000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,129 +0,0 @@ -name: Cache PHP -on: - - push - - repository_dispatch - - workflow_dispatch -jobs: - build: - runs-on: ${{ matrix.operating-system }} - strategy: - fail-fast: false - matrix: - operating-system: [ubuntu-20.04, ubuntu-18.04, ubuntu-16.04] - php-versions: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0'] - name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }} - if: "!contains(github.event.head_commit.message, 'skip-build')" - steps: - - name: Checkout - uses: actions/checkout@v2 - - - name: Add PPA - run: | - LC_ALL=C.UTF-8 sudo apt-add-repository ppa:ondrej/php -y - sudo DEBIAN_FRONTEND=noninteractive apt-get update - - - name: Clean PHP - run: | - sudo apt-get purge 'php*' - sudo rm -rf /var/cache/apt/archives/* - - - name: Restore PHP - run: | - . /etc/lsb-release - version='${{ matrix.php-versions }}' - versions="" - if [ "$DISTRIB_RELEASE" = "20.04" ]; then - versions="7.4 8.0" - elif [ "$DISTRIB_RELEASE" = "18.04" ]; then - versions="7.1 7.2 7.3 7.4 8.0" - elif [ "$DISTRIB_RELEASE" = "16.04" ]; then - versions="5.6 7.0 7.1 7.2 7.3 7.4 8.0" - fi - for i in $versions; do - if [ "$i" = "$version" ]; then - curl -o /tmp/php.sh -sSL https://raw.githubusercontent.com/actions/virtual-environments/main/images/linux/scripts/installers/php.sh - sed -i "s/^php_versions.*/php_versions=$version/" /tmp/php.sh - sed -i 's/php-/php$version-/' /tmp/php.sh - sed -i 's/php$version-pear/php-pear/' /tmp/php.sh - grep 'php_versions=' /tmp/php.sh - sudo bash /tmp/php.sh || true - fi - done - - - name: Setup PHP - uses: shivammathur/setup-php@develop - with: - php-version: ${{ matrix.php-versions }} - extensions: mysql - tools: pecl - env: - update: true - use_package_cache: false - - - name: Test PHP version - run: php -v && php -m - - - name: Upload - id: upload - env: - BINTRAY_KEY: ${{ SECRETS.BINTRAY_KEY }} - BINTRAY_USER: shivammathur - BINTRAY_REPO: php - GITHUB_REPOSITORY: ${{ github.repository }} - VERSION: ${{ matrix.php-versions }} - run: | - . /etc/lsb-release - SEMVER=$(php -v | head -n 1 | cut -f 2 -d ' ' | cut -f 1 -d '-') - curl \ - --user "$BINTRAY_USER":"$BINTRAY_KEY" \ - --header "Content-Type: application/json" \ - --data " \ - {\"name\": \"$VERSION-linux\", \ - \"vcs_url\": \"$GITHUB_REPOSITORY\", \ - \"licenses\": [\"MIT\"], \ - \"public_download_numbers\": true, \ - \"public_stats\": true \ - }" \ - https://api.bintray.com/packages/"$BINTRAY_USER"/"$BINTRAY_REPO" || true - export PATH="$HOME/.linuxbrew/bin:$PATH" - echo "export PATH=$HOME/.linuxbrew/bin:\$PATH" >> "$GITHUB_ENV" - brew install zstd >/dev/null 2>&1 && zstd -V - mkdir "$VERSION" && sudo mv /var/cache/apt/archives/*.deb ./"$VERSION"/ - if [ -e ./"$VERSION"/php"$VERSION"-cli* ]; then - sudo tar cf - "$VERSION" | zstd -22 -T0 --ultra > php_"$VERSION"+ubuntu"$DISTRIB_RELEASE".tar.zst - cp php_"$VERSION"+ubuntu"$DISTRIB_RELEASE".tar.zst php_"$SEMVER"+ubuntu"$DISTRIB_RELEASE".tar.zst - curl --user "$BINTRAY_USER":"$BINTRAY_KEY" -X DELETE https://api.bintray.com/content/"$BINTRAY_USER"/"$BINTRAY_REPO"/php_"$VERSION"+ubuntu"$DISTRIB_RELEASE".tar.zst || true - curl --user "$BINTRAY_USER":"$BINTRAY_KEY" -X DELETE https://api.bintray.com/content/"$BINTRAY_USER"/"$BINTRAY_REPO"/php_"$SEMVER"+ubuntu"$DISTRIB_RELEASE".tar.zst || true - curl --user "$BINTRAY_USER":"$BINTRAY_KEY" -T php_"$VERSION"+ubuntu"$DISTRIB_RELEASE".tar.zst https://api.bintray.com/content/shivammathur/php/"$VERSION"-linux/"$VERSION"+ubuntu"$DISTRIB_RELEASE"/php_"$VERSION"+ubuntu"$DISTRIB_RELEASE".tar.zst || true - curl --user "$BINTRAY_USER":"$BINTRAY_KEY" -T php_"$SEMVER"+ubuntu"$DISTRIB_RELEASE".tar.zst https://api.bintray.com/content/shivammathur/php/"$VERSION"-linux/"$VERSION"+ubuntu"$DISTRIB_RELEASE"/php_"$SEMVER"+ubuntu"$DISTRIB_RELEASE".tar.zst || true - curl --user "$BINTRAY_USER":"$BINTRAY_KEY" -X POST https://api.bintray.com/content/"$BINTRAY_USER"/"$BINTRAY_REPO"/"$VERSION"-linux/"$VERSION"+ubuntu"$DISTRIB_RELEASE"/publish || true - fi - mkdir builds - sudo mv *.zst ./builds - - name: Upload Artifact - uses: actions/upload-artifact@v2 - with: - name: ${{ matrix.php-versions }} - path: builds - release: - runs-on: ubuntu-latest - needs: build - steps: - - uses: actions/checkout@v2 - - run: mkdir builds - - uses: actions/download-artifact@v2 - with: - path: builds - - name: Release - run: | - set -x - curl -o install.sh -sL https://dl.bintray.com/shivammathur/php/php-ubuntu.sh - assets=() - for asset in ./builds/*/*; do - assets+=("$asset") - done - assets+=("./install.sh") - version=$(date '+%Y.%m.%d') - gh release create "$version" "${assets[@]}" -n "$version" -t "$version" - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/cache.yml b/.github/workflows/cache.yml new file mode 100644 index 0000000..48134d5 --- /dev/null +++ b/.github/workflows/cache.yml @@ -0,0 +1,64 @@ +name: Cache PHP +on: + - push + - repository_dispatch + - workflow_dispatch +jobs: + cache: + container: ubuntu:${{ matrix.container }} + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + container: [20.04, 18.04, 16.04] + php-versions: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0'] + if: "!contains(github.event.head_commit.message, 'skip-build')" + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Install requirements + run: bash src/install-requirements.sh + - name: Set up Homebrew + env: + HOMEBREW_FORCE_HOMEBREW_ON_LINUX: 1 + uses: shivammathur/actions/setup-homebrew@shivammathur-patch-git-retry + - name: Install zstd + run: bash src/install-zstd.sh + - name: Configure git + run: bash src/configure-git.sh + - name: Setup PHP + env: + PHP_VERSION: ${{ matrix.php-versions }} + run: bash src/install-php.sh + - name: Build package + env: + GITHUB_WORKSPACE: ${{ github.workspace }} + PHP_VERSION: ${{ matrix.php-versions }} + run: bash src/package.sh + - name: Upload Artifact + uses: actions/upload-artifact@v2 + with: + name: ${{ matrix.php-versions }} + path: builds + release: + runs-on: ubuntu-latest + needs: cache + steps: + - uses: actions/checkout@v2 + - run: mkdir builds + - uses: actions/download-artifact@v2 + with: + path: builds + - name: Setup Python + uses: actions/setup-python@v2 + with: + python-version: 3.x + - name: Install cloudsmith-cli + run: pip install --upgrade cloudsmith-cli + - name: Release + run: bash src/release.sh + env: + CLOUDSMITH_API_KEY: ${{ secrets.CLOUDSMITH_API_KEY }} + GITHUB_REPOSITORY: ${{ github.repository }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_WORKSPACE: ${{ github.workspace }} diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 95f1618..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/install.sh diff --git a/LICENSE b/LICENSE index 74a5455..9fbeb57 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2020 Shivam Mathur +Copyright (c) 2020-21 Shivam Mathur Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 29b6d06..97cec46 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,9 @@ # PHP Ubuntu Build status -LICENSE -PHP Versions Supported +LICENSE +PHP Versions Supported +cloudsmith mirror > Cache PHP packages for installing on GitHub Actions @@ -13,5 +14,7 @@ The code in this project is licensed under the [MIT license](LICENSE). ## Dependencies -- [ppa:ondrej/php](https://launchpad.net/~ondrej/+archive/ubuntu/php "ppa:ondrej/php") - [actions/virtual-environments](https://github.com/actions/virtual-environments "actions/virtual-environments") +- [facebook/zstd](https://github.com/facebook/zstd "facebook/zstd") +- [homebrew/setup-homebrew](https://github.com/Homebrew/actions/tree/master/setup-homebrew "Setup Homebrew") +- [ppa:ondrej/php](https://launchpad.net/~ondrej/+archive/ubuntu/php "ppa:ondrej/php") diff --git a/src/configure-git.sh b/src/configure-git.sh new file mode 100644 index 0000000..f0b6c2c --- /dev/null +++ b/src/configure-git.sh @@ -0,0 +1,8 @@ +set -x + +cd / || exit 1 +git config --global user.email "you@example.com" +git init +git add /bin /lib /lib64 /sbin /usr /var +find /etc -maxdepth 1 -mindepth 1 -type d -exec git add {} \; +git commit -m "init" \ No newline at end of file diff --git a/src/install-php.sh b/src/install-php.sh new file mode 100644 index 0000000..fdbbb32 --- /dev/null +++ b/src/install-php.sh @@ -0,0 +1,11 @@ +set -x + +cd /tmp || exit +curl -o /tmp/php.sh -sSL https://raw.githubusercontent.com/actions/virtual-environments/main/images/linux/scripts/installers/php.sh +sed -i "s/^php_versions.*/php_versions=$PHP_VERSION/" /tmp/php.sh +sed -i '/ snmp\|php-pear\|composer\|phpunit\|invoke_tests\|source/d' /tmp/php.sh +sudo DEBIAN_FRONTEND=noninteractive bash /tmp/php.sh || true +sudo rm -rf /var/cache/apt/archives/*.deb || true +for extension in ast imagick pcov raphf propro http; do + sudo apt-get install "php$PHP_VERSION-$extension" -y || true +done \ No newline at end of file diff --git a/src/install-requirements.sh b/src/install-requirements.sh new file mode 100644 index 0000000..5a4e349 --- /dev/null +++ b/src/install-requirements.sh @@ -0,0 +1,9 @@ +set -x + +export _APTMGR=apt-get +apt-get update && apt-get install -y curl sudo software-properties-common +add-apt-repository ppa:git-core/ppa +add-apt-repository ppa:apt-fast/stable +LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php +apt-get update +DEBIAN_FRONTEND=noninteractive apt-get install -y git sudo apt-fast gcc make automake pkg-config shtool snmp diff --git a/src/install-zstd.sh b/src/install-zstd.sh new file mode 100644 index 0000000..1a8e964 --- /dev/null +++ b/src/install-zstd.sh @@ -0,0 +1,4 @@ +set -x + +brew install zstd +zstd -V diff --git a/src/install.sh b/src/install.sh new file mode 100644 index 0000000..365aaf9 --- /dev/null +++ b/src/install.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +get() { + file_path=$1 + shift + links=("$@") + for link in "${links[@]}"; do + status_code=$(sudo curl -w "%{http_code}" -o "$file_path" -sL "$link") + [ "$status_code" = "200" ] && break + done +} + +install() { + sudo mkdir -p /tmp/php + get /tmp/"$tar_file" "https://github.com/shivammathur/php-ubuntu/releases/latest/download/$tar_file" "https://dl.cloudsmith.io/public/shivammathur/php-ubuntu/raw/files/$tar_file" + sudo tar -I zstd -xf /tmp/"$tar_file" -C / +} + +. /etc/lsb-release +version=$1 +tar_file=php_"$version"%2Bubuntu"$DISTRIB_RELEASE".tar.zst +install diff --git a/src/package.sh b/src/package.sh new file mode 100644 index 0000000..bc8ba64 --- /dev/null +++ b/src/package.sh @@ -0,0 +1,21 @@ +set -x + +cd / || exit 1 +git add /bin /lib /lib64 /sbin /usr /var +find /etc -maxdepth 1 -mindepth 1 -type d -exec git add {} \; +git commit -m "installed php" +mkdir -p /tmp/php +for file in $(git log -p -n 1 --name-only | sed 's/^.*\(\s\).*$/\1/' | xargs -L1 echo); do + sudo cp -r -p --parents "$file" /tmp/php || true +done + +. /etc/lsb-release +SEMVER="$(php -v | head -n 1 | cut -f 2 -d ' ' | cut -f 1 -d '-')" +( + cd /tmp/php || exit 1 + sudo tar cf - ./* | zstd -22 -T0 --ultra > ../php_"$PHP_VERSION"+ubuntu"$DISTRIB_RELEASE".tar.zst + cp ../php_"$PHP_VERSION"+ubuntu"$DISTRIB_RELEASE".tar.zst ../php_"$SEMVER"+ubuntu"$DISTRIB_RELEASE".tar.zst +) +cd "$GITHUB_WORKSPACE" || exit 1 +mkdir builds +sudo mv /tmp/*.zst ./builds \ No newline at end of file diff --git a/src/release.sh b/src/release.sh new file mode 100644 index 0000000..18d95e3 --- /dev/null +++ b/src/release.sh @@ -0,0 +1,40 @@ +set -x + +release_cds() { + for asset in ./builds/*/*; do + assets+=("$asset") + cloudsmith push raw "$repo" "$asset" --republish --summary "$asset" --description "$asset" & + to_wait+=("$!") + done +} + +release_create() { + curl -o install.sh -sL https://dl.cloudsmith.io/public/"$repo"/raw/files/php-ubuntu.sh + release_cds + assets+=("./install.sh") + gh release create "builds" "${assets[@]}" -n "builds $version" -t "builds" +} + +release_upload() { + gh release download -p "build.log" || true + release_cds + gh release upload "builds" "${assets[@]}" --clobber +} + +log() { + echo "$version" | sudo tee -a build.log + gh release upload "builds" build.log --clobber +} + +version=$(date '+%Y.%m.%d') +repo="$GITHUB_REPOSITORY" +assets=() +to_wait=() +cd "$GITHUB_WORKSPACE" || exit 1 +if ! gh release view builds; then + release_create +else + release_upload +fi +wait "${to_wait[@]}" +log