|
| 1 | +name: Publish Package to packagist |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + pull_request: |
| 8 | + branches: |
| 9 | + - main |
| 10 | + |
| 11 | +jobs: |
| 12 | + test: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + strategy: |
| 15 | + matrix: |
| 16 | + php-version: ["8.1", "8.2", "8.3"] |
| 17 | + |
| 18 | + steps: |
| 19 | + - name: Checkout code |
| 20 | + uses: actions/checkout@v4 |
| 21 | + |
| 22 | + - name: Setup PHP |
| 23 | + uses: shivammathur/setup-php@v2 |
| 24 | + with: |
| 25 | + php-version: ${{ matrix.php-version }} |
| 26 | + extensions: json, curl |
| 27 | + coverage: xdebug |
| 28 | + |
| 29 | + - name: Validate composer.json and composer.lock |
| 30 | + run: composer validate --strict |
| 31 | + |
| 32 | + - name: Cache Composer packages |
| 33 | + id: composer-cache |
| 34 | + uses: actions/cache@v3 |
| 35 | + with: |
| 36 | + path: vendor |
| 37 | + key: ${{ runner.os }}-php-${{ matrix.php-version }}-${{ hashFiles('**/composer.lock') }} |
| 38 | + restore-keys: | |
| 39 | + ${{ runner.os }}-php-${{ matrix.php-version }}- |
| 40 | +
|
| 41 | + - name: Install dependencies |
| 42 | + run: composer install --prefer-dist --no-progress |
| 43 | + |
| 44 | + - name: Run PHPUnit tests |
| 45 | + run: composer run-script test |
| 46 | + |
| 47 | + - name: Run PHPStan static analysis |
| 48 | + run: composer run-script phpstan |
| 49 | + |
| 50 | + - name: Run PHP CodeSniffer |
| 51 | + run: composer run-script cs |
| 52 | + |
| 53 | + deploy: |
| 54 | + needs: test |
| 55 | + runs-on: ubuntu-latest |
| 56 | + if: github.ref == 'refs/heads/main' && github.event_name == 'push' |
| 57 | + |
| 58 | + steps: |
| 59 | + - name: Checkout code |
| 60 | + uses: actions/checkout@v4 |
| 61 | + |
| 62 | + - name: Setup PHP |
| 63 | + uses: shivammathur/setup-php@v2 |
| 64 | + with: |
| 65 | + php-version: "8.1" |
| 66 | + extensions: json, curl |
| 67 | + |
| 68 | + - name: Install dependencies |
| 69 | + run: composer install --prefer-dist --no-progress --no-dev --optimize-autoloader |
| 70 | + |
| 71 | + - name: Create tag from version |
| 72 | + id: create_tag |
| 73 | + run: | |
| 74 | + VERSION=$(php -r "echo json_decode(file_get_contents('composer.json'))->version;") |
| 75 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 76 | + if git rev-parse "v$VERSION" >/dev/null 2>&1; then |
| 77 | + echo "Tag v$VERSION already exists" |
| 78 | + echo "tag_exists=true" >> $GITHUB_OUTPUT |
| 79 | + else |
| 80 | + git config --local user.email "action@github.com" |
| 81 | + git config --local user.name "GitHub Action" |
| 82 | + git tag "v$VERSION" |
| 83 | + git push origin "v$VERSION" |
| 84 | + echo "tag_exists=false" >> $GITHUB_OUTPUT |
| 85 | + fi |
| 86 | +
|
| 87 | + - name: Trigger Packagist update |
| 88 | + if: steps.create_tag.outputs.tag_exists == 'false' |
| 89 | + run: | |
| 90 | + curl -X POST \ |
| 91 | + -H "Content-Type: application/json" \ |
| 92 | + -d '{"repository":{"url":"https://github.com/logdash-io/php-sdk"}}' \ |
| 93 | + "https://packagist.org/api/update-package?username=${{ secrets.PACKAGIST_USERNAME }}&apiToken=${{ secrets.PACKAGIST_TOKEN }}" |
| 94 | +
|
| 95 | + - name: Create GitHub Release |
| 96 | + if: steps.create_tag.outputs.tag_exists == 'false' |
| 97 | + uses: actions/create-release@v1 |
| 98 | + env: |
| 99 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 100 | + with: |
| 101 | + tag_name: v${{ steps.create_tag.outputs.version }} |
| 102 | + release_name: Release v${{ steps.create_tag.outputs.version }} |
| 103 | + body: | |
| 104 | + ## Changes in v${{ steps.create_tag.outputs.version }} |
| 105 | +
|
| 106 | + See [CHANGELOG.md](CHANGELOG.md) for detailed changes. |
| 107 | + draft: false |
| 108 | + prerelease: false |
0 commit comments