Skip to content
This repository has been archived by the owner on Oct 21, 2023. It is now read-only.

Deploy To Netlify

Deploy To Netlify #170

Workflow file for this run

name: Deploy To Netlify
on:
workflow_run:
workflows: ["Run CI"]
types:
- completed
jobs:
deploy-target:
if: ${{ github.event.workflow_run.conclusion == 'success' }}
name: Generate Deployment Target
runs-on: ubuntu-latest
outputs:
environment: ${{ steps.data.outputs.environment }}
alias: ${{ steps.data.outputs.alias }}
pr-number: ${{ steps.data.outputs.pr-number }}
head-sha: ${{ steps.data.outputs.head-sha }}
steps:
- name: Prepare Data
id: data
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const workflowRun = await github.rest.actions.getWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});
if (workflowRun.data.event === "pull_request") {
core.setOutput("alias", `pull-${workflowRun.data.pull_requests[0].number}`);
core.setOutput("environment", "preview");
core.setOutput("pr-number", workflowRun.data.pull_requests[0].number);
core.setOutput("head-sha", workflowRun.data.pull_requests[0].head.sha);
} else {
core.setOutput("alias", workflowRun.data.head_sha);
core.setOutput("environment", workflowRun.data.event === "workflow_dispatch" ? "release" : "commit");
core.setOutput("pr-number", 0);
core.setOutput("head-sha", workflowRun.data.head_sha);
}
deploy-docs:
name: Deploy To Netlify
runs-on: ubuntu-latest
needs: [deploy-target]
environment: ${{ needs.deploy-target.outputs.environment }}
permissions:
contents: write
pull-requests: write
deployments: write
steps:
- name: Download Documentation Artifact
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});
const artifact = artifacts.data.artifacts.find(
(artifact) => artifact.name === "docs"
);
const response = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: artifact.id,
archive_format: "zip",
});
// Write the artifact to disk
const fs = require("fs");
fs.writeFileSync("docs.zip", Buffer.from(response.data));
await exec.exec("unzip", ["docs.zip", "-d", "build"]);
- name: Deploy to Netlify
id: deploy
uses: nwtgck/[email protected]
with:
publish-dir: "./build"
production-deploy: ${{ github.event.workflow_run.event == 'workflow_dispatch' }}
github-token: ${{ secrets.GITHUB_TOKEN }}
deploy-message: "Deploy ${{ needs.deploy-target.outputs.alias }} to Netlify"
enable-pull-request-comment: false
enable-commit-comment: false
alias: ${{ needs.deploy-target.outputs.alias }}
github-deployment-environment: ${{ needs.deploy-target.outputs.environment }}
env:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
- name: Add Sticky Comment to PR
if: ${{ github.event.workflow_run.event == 'pull_request' }}
uses: marocchino/sticky-pull-request-comment@v2
with:
number: ${{ needs.deploy-target.outputs.pr-number }}
message: |
:rocket: Preview is ready at Netlify: ${{ steps.deploy.outputs.deploy-url }}
Built with commit: ${{ needs.deploy-target.outputs.head-sha }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}