|
| 1 | +name: Branch Preview Comment |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: |
| 6 | + - opened |
| 7 | + push: |
| 8 | + branches: |
| 9 | + - '*' |
| 10 | + |
| 11 | +jobs: |
| 12 | + build: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + permissions: |
| 15 | + pull-requests: write |
| 16 | + steps: |
| 17 | + - name: Set up Node.js |
| 18 | + uses: actions/setup-node@v3 |
| 19 | + with: |
| 20 | + node-version: 18.18.0 |
| 21 | + - uses: actions/checkout@v4 |
| 22 | + - name: Cache node modules |
| 23 | + uses: actions/cache@v2 |
| 24 | + with: |
| 25 | + path: | |
| 26 | + **/node_modules |
| 27 | + key: ${{ runner.os }}-modules-${{ hashFiles('**/package-lock.json') }} |
| 28 | + restore-keys: | |
| 29 | + **/package-lock.json |
| 30 | + - name: Install dependencies |
| 31 | + run: npm install |
| 32 | + - name: Build & Deploy to Netlify |
| 33 | + id: netlify_deploy |
| 34 | + run: | |
| 35 | + NETLIFY_AUTH_TOKEN="${{ secrets.NETLIFY_AUTH_TOKEN }}" |
| 36 | + NETLIFY_SITE_ID="${{ secrets.NETLIFY_PREVIEW_SITE_ID }}" |
| 37 | + PR_NUMBER="${{ github.event.number }}" |
| 38 | +
|
| 39 | + # Run the Netlify deploy command |
| 40 | + DEPLOY_OUTPUT=$(netlify deploy --build --context deploy-preview --alias "pr-review-${PR_NUMBER}" --json) |
| 41 | +
|
| 42 | + # Extract the URLs from the JSON output using jq |
| 43 | + DEPLOY_URL=$(echo "$DEPLOY_OUTPUT" | jq -r '.deploy_url') |
| 44 | + LOGS_URL=$(echo "$DEPLOY_OUTPUT" | jq -r '.logs') |
| 45 | +
|
| 46 | + # Set outputs for later use |
| 47 | + echo "::set-output name=deploy_url::${DEPLOY_URL}" |
| 48 | + echo "::set-output name=logs_url::${LOGS_URL}" |
| 49 | + env: |
| 50 | + NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} |
| 51 | + NETLIFY_SITE_ID: ${{ secrets.NETLIFY_PREVIEW_SITE_ID }} |
| 52 | + - name: Comment on PR |
| 53 | + uses: mshick/add-pr-comment@v2 |
| 54 | + with: |
| 55 | + message: | |
| 56 | + This pull request is being automatically deployed to Netlify. |
| 57 | +
|
| 58 | + ✅ Preview: ${{ steps.netlify_deploy.outputs.deploy_url }} |
| 59 | + 🔖 Deployed version: ${{ github.sha }} |
| 60 | + env: |
| 61 | + GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} |
0 commit comments