chore: gha #11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Publish PHP SDK to Packagist | |
| on: | |
| push: | |
| branches: | |
| - main | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| jobs: | |
| test-and-publish: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| with: | |
| fetch-depth: 0 | |
| - name: Setup PHP | |
| uses: shivammathur/setup-php@v2 | |
| with: | |
| php-version: '8.1' | |
| extensions: mbstring, intl | |
| coverage: none | |
| tools: composer:v2 | |
| - name: Validate composer.json | |
| run: composer validate --strict | |
| - name: Install dependencies | |
| run: composer install --prefer-dist | |
| - name: Run tests | |
| run: vendor/bin/phpunit tests/ | |
| # Only proceed with version bump and publishing if on main branch and tests passed | |
| - name: Get current version from git tags | |
| id: current_version | |
| if: success() && (github.ref == 'refs/heads/main' || github.event_name == 'workflow_dispatch') | |
| run: | | |
| # Get latest tag or use 0.1.0 as default if no tags exist | |
| LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.1.0") | |
| # Remove 'v' prefix if present | |
| VERSION=${LATEST_TAG#v} | |
| # Validate version format | |
| if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then | |
| echo "Setting initial version to 0.1.0" | |
| VERSION="0.1.0" | |
| fi | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| - name: Bump patch version | |
| id: bump_version | |
| if: success() && (github.ref == 'refs/heads/main' || github.event_name == 'workflow_dispatch') | |
| run: | | |
| # Get current version | |
| CURRENT_VERSION=${{ steps.current_version.outputs.version }} | |
| # Validate current version format | |
| if [[ ! "$CURRENT_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then | |
| echo "Warning: Invalid version format: $CURRENT_VERSION. Using 0.1.0 as base." | |
| CURRENT_VERSION="0.1.0" | |
| fi | |
| # Use PHP to increment patch version | |
| NEW_VERSION=$(php -r ' | |
| $version = "${{ steps.current_version.outputs.version }}"; | |
| if (!preg_match("/^[0-9]+\.[0-9]+\.[0-9]+$/", $version)) { | |
| $version = "0.1.0"; | |
| } | |
| list($major, $minor, $patch) = explode(".", $version); | |
| $patch++; | |
| echo "$major.$minor.$patch"; | |
| ') | |
| echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT | |
| # No need to update version in composer.json as we're using git tags | |
| # No need to commit version bump as we're using git tags | |
| - name: Create tag for release | |
| if: success() && (github.ref == 'refs/heads/main' || github.event_name == 'workflow_dispatch') | |
| run: | | |
| git tag v${{ steps.bump_version.outputs.new_version }} | |
| git push origin v${{ steps.bump_version.outputs.new_version }} | |
| - name: Ping Packagist | |
| if: success() && (github.ref == 'refs/heads/main' || github.event_name == 'workflow_dispatch') | |
| run: | | |
| echo "Pinging Packagist to update package..." | |
| curl -XPOST -H'content-type:application/json' \ | |
| "https://packagist.org/api/update-package?username=lingodotdev&apiToken=${{ secrets.PACKAGIST_SAFE_API_TOKEN }}" \ | |
| -d'{"repository":{"url":"https://github.com/lingodotdev/sdk-php"}}' |