|
| 1 | +name: Deploy to Netlify |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + prod: |
| 7 | + required: true |
| 8 | + type: boolean |
| 9 | + secrets: |
| 10 | + NETLIFY_AUTH_TOKEN: |
| 11 | + required: true |
| 12 | + NETLIFY_SITE_ID: |
| 13 | + required: true |
| 14 | + |
| 15 | +jobs: |
| 16 | + deploy: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + permissions: |
| 19 | + contents: read |
| 20 | + |
| 21 | + environment: |
| 22 | + name: ${{ github.event_name == 'pull_request' && format('preview-{0}', github.event.pull_request.number) || 'production' }} |
| 23 | + url: ${{ steps.netlify_deploy.outputs.deploy_url }} |
| 24 | + |
| 25 | + steps: |
| 26 | + - name: Checkout repository |
| 27 | + uses: actions/checkout@v4 |
| 28 | + |
| 29 | + - name: Setup PNPM |
| 30 | + uses: pnpm/action-setup@v4 |
| 31 | + with: |
| 32 | + run_install: | |
| 33 | + - args: [--global, netlify-cli] |
| 34 | +
|
| 35 | + - name: Setup Node.js |
| 36 | + uses: actions/setup-node@v4 |
| 37 | + with: |
| 38 | + node-version: 22 |
| 39 | + cache: 'pnpm' |
| 40 | + |
| 41 | + - name: Install dependencies |
| 42 | + run: pnpm install --frozen-lockfile |
| 43 | + |
| 44 | + - name: Deploy to Netlify |
| 45 | + id: netlify_deploy |
| 46 | + run: | |
| 47 | + if [ "${{ inputs.prod }}" == "true" ]; then |
| 48 | + echo "🚀 Production Deploy" |
| 49 | + OUTPUT=$(netlify deploy \ |
| 50 | + --prod \ |
| 51 | + --site="$NETLIFY_SITE_ID" \ |
| 52 | + --auth="$NETLIFY_AUTH_TOKEN" \ |
| 53 | + --dir=dist \ |
| 54 | + --functions=.netlify/functions \ |
| 55 | + 2>&1 | tee /dev/stderr) |
| 56 | + URL=$(echo "$OUTPUT" | sed -nE 's/.*Deployed to production URL:[[:space:]]*([^[:space:]]*).*/\1/p') |
| 57 | + else |
| 58 | + echo "🧪 Preview Deploy" |
| 59 | + OUTPUT=$(netlify deploy \ |
| 60 | + --site="$NETLIFY_SITE_ID" \ |
| 61 | + --auth="$NETLIFY_AUTH_TOKEN" \ |
| 62 | + --dir=dist \ |
| 63 | + --functions=.netlify/functions \ |
| 64 | + 2>&1 | tee /dev/stderr) |
| 65 | + URL=$(echo "$OUTPUT" | sed -nE 's/.*Unique deploy URL:[[:space:]]*([^[:space:]]*).*/\1/p') |
| 66 | + fi |
| 67 | + if [ -z "$URL" ]; then |
| 68 | + echo "❌ Deploy URL not found, failing workflow." |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | + echo "deploy_url=$URL" >> $GITHUB_OUTPUT |
| 72 | + echo "Deployed to $URL" |
| 73 | + env: |
| 74 | + NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }} |
| 75 | + NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }} |
0 commit comments