|
| 1 | +name: Publish PHP SDK to Packagist |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + test: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - name: Checkout code |
| 17 | + uses: actions/checkout@v3 |
| 18 | + |
| 19 | + - name: Setup PHP |
| 20 | + uses: shivammathur/setup-php@v2 |
| 21 | + with: |
| 22 | + php-version: '8.1' |
| 23 | + extensions: mbstring, intl |
| 24 | + coverage: none |
| 25 | + tools: composer:v2 |
| 26 | + |
| 27 | + - name: Validate composer.json |
| 28 | + run: composer validate --no-check-publish |
| 29 | + |
| 30 | + - name: Install dependencies |
| 31 | + run: composer install --prefer-dist |
| 32 | + |
| 33 | + - name: Run tests |
| 34 | + run: vendor/bin/phpunit tests/ |
| 35 | + |
| 36 | + publish: |
| 37 | + needs: test |
| 38 | + if: github.ref == 'refs/heads/main' || github.event_name == 'workflow_dispatch' |
| 39 | + runs-on: ubuntu-latest |
| 40 | + steps: |
| 41 | + - name: Checkout code |
| 42 | + uses: actions/checkout@v3 |
| 43 | + with: |
| 44 | + fetch-depth: 0 |
| 45 | + |
| 46 | + - name: Setup PHP |
| 47 | + uses: shivammathur/setup-php@v2 |
| 48 | + with: |
| 49 | + php-version: '8.1' |
| 50 | + extensions: mbstring, intl |
| 51 | + coverage: none |
| 52 | + tools: composer:v2 |
| 53 | + |
| 54 | + - name: Get current version |
| 55 | + id: current_version |
| 56 | + run: | |
| 57 | + # Check if version exists in composer.json |
| 58 | + VERSION=$(php -r ' |
| 59 | + $composerJson = json_decode(file_get_contents("composer.json"), true); |
| 60 | + echo isset($composerJson["version"]) ? $composerJson["version"] : ""; |
| 61 | + ') |
| 62 | + |
| 63 | + if [[ -z "$VERSION" || ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 64 | + echo "Setting initial version to 0.1.0" |
| 65 | + VERSION="0.1.0" |
| 66 | + fi |
| 67 | + |
| 68 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 69 | +
|
| 70 | + - name: Bump patch version |
| 71 | + id: bump_version |
| 72 | + run: | |
| 73 | + # Get current version |
| 74 | + CURRENT_VERSION=${{ steps.current_version.outputs.version }} |
| 75 | + |
| 76 | + # Validate current version format |
| 77 | + if [[ ! "$CURRENT_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 78 | + echo "Warning: Invalid version format: $CURRENT_VERSION. Using 0.1.0 as base." |
| 79 | + CURRENT_VERSION="0.1.0" |
| 80 | + fi |
| 81 | + |
| 82 | + # Use PHP to increment patch version |
| 83 | + NEW_VERSION=$(php -r ' |
| 84 | + $version = "${{ steps.current_version.outputs.version }}"; |
| 85 | + if (!preg_match("/^[0-9]+\.[0-9]+\.[0-9]+$/", $version)) { |
| 86 | + $version = "0.1.0"; |
| 87 | + } |
| 88 | + list($major, $minor, $patch) = explode(".", $version); |
| 89 | + $patch++; |
| 90 | + echo "$major.$minor.$patch"; |
| 91 | + ') |
| 92 | + |
| 93 | + echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT |
| 94 | + |
| 95 | + # Update version in composer.json |
| 96 | + php -r ' |
| 97 | + $composerJson = json_decode(file_get_contents("composer.json"), true); |
| 98 | + $composerJson["version"] = "${{ steps.bump_version.outputs.new_version }}"; |
| 99 | + file_put_contents("composer.json", json_encode($composerJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); |
| 100 | + ' |
| 101 | +
|
| 102 | + - name: Commit and push version bump |
| 103 | + uses: stefanzweifel/git-auto-commit-action@v4 |
| 104 | + with: |
| 105 | + commit_message: "chore: bump PHP SDK version to ${{ steps.bump_version.outputs.new_version }}" |
| 106 | + file_pattern: composer.json |
| 107 | + commit_user_name: "Lingo.dev" |
| 108 | + commit_user_email: "hi@lingo.dev" |
| 109 | + |
| 110 | + - name: Create tag for release |
| 111 | + run: | |
| 112 | + git tag v${{ steps.bump_version.outputs.new_version }} |
| 113 | + git push origin v${{ steps.bump_version.outputs.new_version }} |
| 114 | +
|
| 115 | + - name: Publish to Packagist |
| 116 | + run: | |
| 117 | + php scripts/packagist-publish.php |
| 118 | + env: |
| 119 | + PACKAGIST_USERNAME: lingodotdev |
| 120 | + PACKAGIST_API_TOKEN: ${{ secrets.PACKAGIST_API_TOKEN }} |
| 121 | + PACKAGE_NAME: lingodotdev/sdk |
0 commit comments