|
| 1 | +name: Composer update |
| 2 | +description: Restores the Composer download cache, runs `composer update` with retries and saves the cache for the resolved dependencies. |
| 3 | + |
| 4 | +inputs: |
| 5 | + php-version: |
| 6 | + description: PHP version used as part of the cache key |
| 7 | + required: true |
| 8 | + options: |
| 9 | + description: Additional options appended to the `composer update` call |
| 10 | + required: false |
| 11 | + default: '' |
| 12 | + github-token: |
| 13 | + description: Token used to authenticate the downloads from GitHub |
| 14 | + required: false |
| 15 | + default: ${{ github.token }} |
| 16 | + |
| 17 | +runs: |
| 18 | + using: composite |
| 19 | + steps: |
| 20 | + - name: Get composer cache directory |
| 21 | + id: composer-cache |
| 22 | + shell: bash |
| 23 | + run: echo "COMPOSER_CACHE_FILES_DIR=$(composer config cache-files-dir)" >> "$GITHUB_OUTPUT" |
| 24 | + |
| 25 | + - name: Restore composer cache |
| 26 | + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 |
| 27 | + with: |
| 28 | + path: ${{ steps.composer-cache.outputs.COMPOSER_CACHE_FILES_DIR }} |
| 29 | + # Never matches an entry, so the restore always falls back to restore-keys. |
| 30 | + key: composer-php-${{ inputs.php-version }}-restore-${{ github.run_id }}-${{ github.run_attempt }} |
| 31 | + restore-keys: | |
| 32 | + composer-php-${{ inputs.php-version }}- |
| 33 | + composer- |
| 34 | +
|
| 35 | + - name: Install dependencies |
| 36 | + shell: bash |
| 37 | + env: |
| 38 | + GITHUB_TOKEN: ${{ inputs.github-token }} |
| 39 | + COMPOSER_OPTIONS: ${{ inputs.options }} |
| 40 | + run: | |
| 41 | + if [ -n "$GITHUB_TOKEN" ]; then |
| 42 | + composer config --global github-oauth.github.com "$GITHUB_TOKEN" |
| 43 | + fi |
| 44 | +
|
| 45 | + attempt=1 |
| 46 | + until composer update --ansi --no-interaction $COMPOSER_OPTIONS; do |
| 47 | + if [ "$attempt" -ge 3 ]; then |
| 48 | + echo "::error::composer update failed after $attempt attempts." |
| 49 | + exit 1 |
| 50 | + fi |
| 51 | +
|
| 52 | + delay=$((attempt * 30)) |
| 53 | + echo "::warning::composer update failed on attempt $attempt. Retrying in ${delay}s." |
| 54 | + sleep "$delay" |
| 55 | + attempt=$((attempt + 1)) |
| 56 | + done |
| 57 | +
|
| 58 | + - name: Save composer cache |
| 59 | + uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 |
| 60 | + with: |
| 61 | + path: ${{ steps.composer-cache.outputs.COMPOSER_CACHE_FILES_DIR }} |
| 62 | + key: composer-php-${{ inputs.php-version }}-${{ hashFiles('composer.lock') }} |
0 commit comments