test: workflow testing #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: Preview Package Publishing | |
| on: | |
| pull_request: | |
| branches: [feat/v3] | |
| paths: | |
| - 'packages/**' | |
| types: [opened, synchronize, reopened] | |
| jobs: | |
| check-changes: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| packages-changed: ${{ steps.changes.outputs.packages }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - uses: dorny/paths-filter@v2 | |
| id: changes | |
| with: | |
| filters: | | |
| packages: | |
| - 'packages/**' | |
| publish-preview: | |
| needs: check-changes | |
| if: needs.check-changes.outputs.packages-changed == 'true' | |
| runs-on: ubuntu-latest | |
| outputs: | |
| published: ${{ steps.publish.outputs.published }} | |
| version: ${{ steps.publish.outputs.version }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| cache: 'yarn' | |
| - name: Install dependencies | |
| run: yarn install --frozen-lockfile | |
| - name: Generate preview version | |
| id: version | |
| run: | | |
| PR_NUMBER=${{ github.event.pull_request.number }} | |
| SHORT_SHA=$(git rev-parse --short HEAD) | |
| PREVIEW_VERSION="0.0.0-pr-${PR_NUMBER}-${SHORT_SHA}" | |
| echo "version=${PREVIEW_VERSION}" >> $GITHUB_OUTPUT | |
| echo "Generated preview version: ${PREVIEW_VERSION}" | |
| - name: Update package versions | |
| run: | | |
| # Update only root-level package.json files in packages/ directory (not node_modules) | |
| find packages -maxdepth 2 -name "package.json" -not -path "*/node_modules/*" -type f | while read file; do | |
| # Create backup | |
| cp "$file" "$file.bak" | |
| # Update version using Node.js | |
| node -e " | |
| const fs = require('fs'); | |
| const pkg = JSON.parse(fs.readFileSync('$file', 'utf8')); | |
| pkg.version = '${{ steps.version.outputs.version }}'; | |
| fs.writeFileSync('$file', JSON.stringify(pkg, null, 2) + '\n'); | |
| " | |
| echo "Updated version in $file to ${{ steps.version.outputs.version }}" | |
| done | |
| - name: Setup NPM authentication | |
| run: | | |
| echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}" > ~/.npmrc | |
| - name: Publish preview packages | |
| id: publish | |
| run: | | |
| # Publish only root-level packages in packages/ directory (not node_modules) | |
| PUBLISHED_PACKAGES="" | |
| find packages -maxdepth 2 -name "package.json" -not -path "*/node_modules/*" -type f | while read file; do | |
| PACKAGE_DIR=$(dirname "$file") | |
| PACKAGE_JSON=$(cat "$file") | |
| PACKAGE_NAME=$(echo "$PACKAGE_JSON" | node -e "console.log(JSON.parse(require('fs').readFileSync('/dev/stdin', 'utf8')).name)") | |
| IS_PRIVATE=$(echo "$PACKAGE_JSON" | node -e "console.log(JSON.parse(require('fs').readFileSync('/dev/stdin', 'utf8')).private || false)") | |
| if [ "$IS_PRIVATE" = "true" ]; then | |
| echo "⏭️ Skipping private package: $PACKAGE_NAME" | |
| continue | |
| fi | |
| echo "📦 Publishing $PACKAGE_NAME from $PACKAGE_DIR..." | |
| cd "$PACKAGE_DIR" | |
| # Build if build script exists | |
| if yarn run --list 2>/dev/null | grep -q "build"; then | |
| echo "🔨 Building $PACKAGE_NAME..." | |
| yarn build | |
| fi | |
| # Publish with preview tag | |
| npm publish --tag preview --access public | |
| if [ $? -eq 0 ]; then | |
| echo "✅ Successfully published $PACKAGE_NAME@${{ steps.version.outputs.version }}" | |
| PUBLISHED_PACKAGES="$PUBLISHED_PACKAGES $PACKAGE_NAME@${{ steps.version.outputs.version }}" | |
| else | |
| echo "❌ Failed to publish $PACKAGE_NAME" | |
| exit 1 | |
| fi | |
| cd - > /dev/null | |
| done | |
| echo "published=true" >> $GITHUB_OUTPUT | |
| echo "packages=$PUBLISHED_PACKAGES" >> $GITHUB_OUTPUT | |
| - name: Restore package.json files | |
| if: always() | |
| run: | | |
| # Restore original package.json files | |
| find packages -maxdepth 2 -name "package.json.bak" -not -path "*/node_modules/*" -type f | while read backup; do | |
| original="${backup%.bak}" | |
| mv "$backup" "$original" | |
| echo "Restored $original" | |
| done | |
| - name: Comment on PR | |
| if: steps.publish.outputs.published == 'true' | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const comment = `🚀 **Preview packages published successfully!** | |
| **Version:** \`${{ steps.version.outputs.version }}\` | |
| **Published packages:** ${{ steps.publish.outputs.packages }} | |
| You can install these preview packages using: | |
| \`\`\`bash | |
| npm install <package-name>@${{ steps.version.outputs.version }} | |
| # or | |
| yarn add <package-name>@${{ steps.version.outputs.version }} | |
| \`\`\` | |
| **Note:** These are preview packages and should only be used for testing.`; | |
| github.rest.issues.createComment({ | |
| issue_number: context.issue.number, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: comment | |
| }); | |
| trigger-vercel-preview: | |
| needs: [check-changes, publish-preview] | |
| if: needs.publish-preview.outputs.published == 'true' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20' | |
| - name: Trigger Vercel Preview Deployments | |
| id: deploy | |
| run: | | |
| # Parse project IDs from secrets | |
| PROJECT_IDS='${{ secrets.VERCEL_PROJECT_IDS }}' | |
| DEPLOYED_PROJECTS="" | |
| DEPLOYMENT_URLS="" | |
| echo "$PROJECT_IDS" | jq -r 'to_entries[] | "\(.key):\(.value)"' | while IFS=':' read -r PROJECT_NAME PROJECT_ID; do | |
| echo "🚀 Deploying $PROJECT_NAME (ID: $PROJECT_ID)..." | |
| # Create deployment using Vercel API with Git source for code changes | |
| RESPONSE=$(curl -s -X POST "https://api.vercel.com/v13/deployments" \ | |
| -H "Authorization: Bearer ${{ secrets.VERCEL_TOKEN }}" \ | |
| -H "Content-Type: application/json" \ | |
| -d "{ | |
| \"name\": \"$PROJECT_NAME\", | |
| \"project\": \"$PROJECT_ID\", | |
| \"target\": \"preview\", | |
| \"gitSource\": { | |
| \"type\": \"github\", | |
| \"ref\": \"${{ github.head_ref }}\", | |
| \"sha\": \"${{ github.sha }}\", | |
| \"repoId\": ${{ github.event.repository.id }} | |
| }, | |
| \"meta\": { | |
| \"githubCommitSha\": \"${{ github.sha }}\", | |
| \"githubCommitAuthorName\": \"${{ github.actor }}\", | |
| \"githubCommitMessage\": \"${{ github.event.pull_request.title }}\", | |
| \"packageVersion\": \"${{ needs.publish-preview.outputs.version }}\", | |
| \"githubPR\": \"${{ github.event.pull_request.number }}\", | |
| \"triggeredBy\": \"github-actions\" | |
| } | |
| }") | |
| # Extract deployment URL and ID | |
| DEPLOYMENT_URL=$(echo "$RESPONSE" | jq -r '.url // empty') | |
| DEPLOYMENT_ID=$(echo "$RESPONSE" | jq -r '.id // empty') | |
| ERROR_MESSAGE=$(echo "$RESPONSE" | jq -r '.error.message // empty') | |
| if [ -n "$DEPLOYMENT_URL" ] && [ -n "$DEPLOYMENT_ID" ]; then | |
| echo "✅ Successfully triggered deployment for $PROJECT_NAME" | |
| echo "📍 Preview URL: https://$DEPLOYMENT_URL" | |
| DEPLOYED_PROJECTS="$DEPLOYED_PROJECTS $PROJECT_NAME" | |
| DEPLOYMENT_URLS="$DEPLOYMENT_URLS\\n- **$PROJECT_NAME**: https://$DEPLOYMENT_URL" | |
| else | |
| echo "❌ Failed to deploy $PROJECT_NAME" | |
| if [ -n "$ERROR_MESSAGE" ]; then | |
| echo "Error: $ERROR_MESSAGE" | |
| fi | |
| echo "Response: $RESPONSE" | |
| fi | |
| done | |
| # Save results for next step | |
| echo "deployed_projects=$DEPLOYED_PROJECTS" >> $GITHUB_OUTPUT | |
| echo -e "deployment_urls=$DEPLOYMENT_URLS" >> $GITHUB_OUTPUT | |
| - name: Update PR with Vercel deployment info | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const deploymentUrls = `${{ steps.deploy.outputs.deployment_urls }}`; | |
| const deployedProjects = `${{ steps.deploy.outputs.deployed_projects }}`.trim(); | |
| let comment = `🚀 **Vercel preview deployments triggered!** | |
| **Package Version:** \`${{ needs.publish-preview.outputs.version }}\` | |
| `; | |
| if (deployedProjects) { | |
| comment += `**Deployed Projects:**${deploymentUrls} | |
| ⏱️ Deployments are in progress. Check your Vercel dashboard for detailed status.`; | |
| } else { | |
| comment += `⚠️ No deployments were triggered. Please check the workflow logs.`; | |
| } | |
| github.rest.issues.createComment({ | |
| issue_number: context.issue.number, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: comment | |
| }); |