Skip to content

Publish preview

Publish preview #1010

Workflow file for this run

# Copyright 2022 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0s
name: Publish preview
on:
workflow_run:
workflows: ["Check pull request"]
types:
- completed
jobs:
preview:
runs-on: ubuntu-latest
permissions:
pull-requests: write
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
steps:
# Step 1: Check out your repository code so netlify.toml is found
- name: Checkout repository
uses: actions/checkout@v5
# Step 2: Download the built website files from the previous job
- name: Download pull request artifact
uses: actions/github-script@v7
with:
script: |
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{ github.event.workflow_run.id }},
});
const websiteArtifact = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == 'website'
})[0];
var download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: websiteArtifact.id,
archive_format: 'zip',
});
const fs = require('fs');
fs.writeFileSync('${{github.workspace}}/website.zip', Buffer.from(download.data));
# Step 3: Unzip the website files
- name: Unzip website artifact
run: unzip -q website.zip
# Step 4: Get the PR number for the deploy alias
- name: Setup pull request data
id: data
run: |
PR_NUMBER=$(head -1 pull_request/number)
PR_NUMBER=${PR_NUMBER//[^0-9]/}
echo "PR_NUMBER=${PR_NUMBER}" >> $GITHUB_OUTPUT
echo "PR_URL=https://github.com/${GITHUB_REPOSITORY}/pull/${PR_NUMBER}" >> $GITHUB_OUTPUT
# Step 5: Manually install the Netlify CLI tool
- name: Install Netlify CLI
run: npm install -g netlify-cli
# Step 6: Deploy to Netlify and capture the URL from the log
- name: Deploy to Netlify and Find URL
id: deploy
env: # <--- THIS IS THE CRITICAL BLOCK I FORGOT. IT IS NOW CORRECT.
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
run: |
# Run the deploy command and save its log to a file
netlify deploy --dir=public --alias="pr-${{ steps.data.outputs.PR_NUMBER }}" --message="Preview for ${{ steps.data.outputs.PR_URL}}" > netlify_output.log
# Print the log file for debugging
echo "--- CAPTURED NETLIFY LOG ---"
cat netlify_output.log
echo "----------------------------"
# Find the URL in the log file and set it as an output
URL=$(grep -o 'https://pr-.*\.netlify\.app' netlify_output.log)
echo "url=$URL" >> $GITHUB_OUTPUT
# Step 7: Post the comment with the URL we found
- name: Add comment
if: success() && steps.deploy.outputs.url
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
await github.rest.issues.createComment({
issue_number: Number(${{ steps.data.outputs.PR_NUMBER }}),
owner: context.repo.owner,
repo: context.repo.repo,
body: `🚀 Preview is available at: ${{ steps.deploy.outputs.url }}`
});