Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions .github/actions/deploy-vercel-preview/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Deploy Vercel preview

description: Build and deploy a preview to Vercel

inputs:
github-token:
description: GitHub token used by Vercel action to comment on PRs
required: true
vercel-token:
description: Vercel token
required: true
vercel-org-id:
description: Vercel organization id
required: true
vercel-project-id:
description: Vercel project id
required: true
vercel-project-name:
description: Vercel project name
required: true
working-directory:
description: Working directory for deployment
required: false
default: ${{ github.workspace }}

runs:
using: composite
steps:
- name: Prepare build command for preview
working-directory: ${{ inputs.working-directory }}
run: |
sed -i 's/yarn vercel-build/yarn vercel-preview-build/' vercel.json
shell: bash
Comment thread
Yermanaco marked this conversation as resolved.

- name: Deploy to Vercel
uses: amondnet/vercel-action@v42.3.0
id: vercel-deploy
with:
github-token: ${{ inputs.github-token }}
vercel-token: ${{ inputs.vercel-token }}
vercel-org-id: ${{ inputs.vercel-org-id }}
vercel-project-id: ${{ inputs.vercel-project-id }}
vercel-project-name: ${{ inputs.vercel-project-name }}
working-directory: ${{ inputs.working-directory }}
65 changes: 65 additions & 0 deletions .github/workflows/deploy-fork-pr-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: deploy-fork-pr-preview
on:
workflow_dispatch:
inputs:
prNumber:
description: 'Pull request number to deploy preview for'
required: true

permissions:
contents: read
pull-requests: write

jobs:
deploy:
name: Deploy fork PR preview
runs-on: ubuntu-latest
# Manual approval gate before exposing deployment secrets to reviewed PR code
environment: production
steps:
- name: Validate PR and extract refs
id: pr
uses: actions/github-script@v7
with:
script: |
const REQUIRED_LABEL = 'safe-to-deploy';
const prNumber = Number('${{ github.event.inputs.prNumber }}');
if (!Number.isInteger(prNumber) || prNumber <= 0) {
core.setFailed('Invalid prNumber input');
return;
}

const {owner, repo} = context.repo;
const {data: pr} = await github.rest.pulls.get({owner, repo, pull_number: prNumber});

if (pr.state !== 'open') {
core.setFailed(`PR #${prNumber} is not open`);
return;
}

if (!pr.head.repo.fork) {
core.setFailed(`PR #${prNumber} is not from a fork. Use deploy-pull-requests workflow for internal PRs.`);
return;
}

const labels = (pr.labels || []).map((label) => label.name);
if (!labels.includes(REQUIRED_LABEL)) {
core.setFailed(`PR #${prNumber} is missing required label: ${REQUIRED_LABEL}`);
return;
}

core.setOutput('merge_ref', `refs/pull/${prNumber}/merge`);
core.setOutput('pr_number', String(prNumber));
- uses: actions/checkout@v6
with:
Comment thread
Yermanaco marked this conversation as resolved.
ref: ${{ steps.pr.outputs.merge_ref }}
persist-credentials: false

- uses: ./.github/actions/deploy-vercel-preview
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
vercel-token: ${{ secrets.VERCEL_TOKEN }}
vercel-org-id: ${{ secrets.MISTICA_WEB_VERCEL_ORG_ID }}
vercel-project-id: ${{ secrets.MISTICA_WEB_VERCEL_PROJECT_ID }}
vercel-project-name: mistica-web
working-directory: ${{ github.workspace }}
2 changes: 1 addition & 1 deletion .github/workflows/deploy-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
with:
persist-credentials: false

- uses: amondnet/vercel-action@master
- uses: amondnet/vercel-action@v42.3.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }} # needed to allow comments on prs
vercel-token: ${{ secrets.VERCEL_TOKEN }} # Required
Expand Down
8 changes: 1 addition & 7 deletions .github/workflows/deploy-pull-requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ jobs:
- uses: actions/checkout@v6
with:
persist-credentials: false

# we need to run a different buildCommand for PR previews (see package.json scripts)
- run: sed -i 's/yarn vercel-build/yarn vercel-preview-build/' vercel.json
shell: bash

- uses: amondnet/vercel-action@master
id: vercel-deploy # identifier to reference this step
- uses: ./.github/actions/deploy-vercel-preview
with:
github-token: ${{ secrets.GITHUB_TOKEN }} # needed to allow comments on prs
vercel-token: ${{ secrets.VERCEL_TOKEN }} # required
Expand Down
55 changes: 55 additions & 0 deletions .github/workflows/label-trigger-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Auto-dispatch deploy on label

on:
pull_request_target:
types: [labeled]

permissions:
actions: write
contents: read
Comment thread
Yermanaco marked this conversation as resolved.
issues: write
pull-requests: read

jobs:
dispatch:
runs-on: ubuntu-latest
steps:
- name: Dispatch deploy workflow when `safe-to-deploy` label is added
uses: actions/github-script@v7
with:
script: |
const TARGET_LABEL = 'safe-to-deploy';
const label = context.payload.label && context.payload.label.name;
if (label !== TARGET_LABEL) {
core.info(`Label '${label}' is not '${TARGET_LABEL}', skipping dispatch.`);
return;
}

const prNumber = context.payload.pull_request && context.payload.pull_request.number;
if (!prNumber) {
core.setFailed('Could not find pull request number in event payload.');
return;
}

const { owner, repo } = context.repo;
const { data: pr } = await github.rest.pulls.get({ owner, repo, pull_number: prNumber });

// Only dispatch the fork-preview workflow for PRs coming from forks
if (!pr.head || !pr.head.repo || !pr.head.repo.fork) {
core.info('PR is not from a fork; skipping fork preview dispatch.');
return;
}

await github.rest.actions.createWorkflowDispatch({
owner,
repo,
workflow_id: 'deploy-fork-pr-preview.yml',
ref: 'master',
inputs: { prNumber: String(prNumber) },
});

core.info(`Dispatched deploy-fork-pr-preview for PR #${prNumber}`);

// post an audit comment on the PR
const commentBody = `Label '${TARGET_LABEL}' added — dispatching fork preview workflow. Awaiting environment approval to expose deploy secrets.`;
await github.rest.issues.createComment({ owner, repo, issue_number: prNumber, body: commentBody });
70 changes: 70 additions & 0 deletions .github/workflows/size-stats-comment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Size stats comment

on:
workflow_run:
workflows: ['Size stats']
types: [completed]

permissions:
actions: read
contents: read
pull-requests: write

jobs:
comment:
runs-on: ubuntu-latest
if: github.event.workflow_run.conclusion == 'success'
steps:
- name: Download size stats artifact
uses: actions/download-artifact@v4
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ github.event.workflow_run.id }}
name: size-stats-results
path: size-stats-results

- name: Upsert PR comment
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');

const marker = '<!-- size-stats-comment -->';
const title = '**Size stats**';
const workflowRunPrs = context.payload.workflow_run?.pull_requests || [];
const prNumber = workflowRunPrs.length === 1 ? workflowRunPrs[0].number : NaN;
const message = fs.readFileSync('size-stats-results/message.md', 'utf8').trim();
const body = `${marker}\n${title}\n\n${message}`;

if (!Number.isInteger(prNumber) || prNumber <= 0) {
core.setFailed('Could not determine a unique PR number from workflow_run payload');
return;
}

const {owner, repo} = context.repo;
const {data: comments} = await github.rest.issues.listComments({
owner,
repo,
issue_number: prNumber,
per_page: 100,
});

const previous = comments.find((comment) =>
comment.user?.type === 'Bot' && comment.body?.includes(marker),
);

if (previous) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: previous.id,
body,
});
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number: prNumber,
body,
});
}
30 changes: 14 additions & 16 deletions .github/workflows/size-stats.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:

permissions:
contents: read
pull-requests: write
pull-requests: read

concurrency:
group: size-stats-${{ github.ref }}
Expand Down Expand Up @@ -56,20 +56,12 @@ jobs:
- id: stats
uses: './.github/actions/size-stats'

show-results:
store-results:
runs-on: ubuntu-latest
needs: [master-size-stats, branch-size-stats]
steps:
- uses: actions/checkout@v6
with:
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if I do this, this PR would fail

ref: master
persist-credentials: false

- uses: actions/checkout@v6
with:
repository: Telefonica/github-actions
token: '${{ secrets.GH_TOKEN_ACTIONS }}'
path: .github/shared-actions
persist-credentials: false

- run: yarn install --immutable --immutable-cache
Expand All @@ -87,10 +79,16 @@ jobs:
pr-lib-overhead: ${{ needs.branch-size-stats.outputs.lib-overhead }}
pr-lib-overhead-gzip: ${{ needs.branch-size-stats.outputs.lib-overhead-gzip }}

- name: Comment on PR
uses: ./.github/shared-actions/novum/comment-pr
- name: Prepare artifact payload
run: |
mkdir -p size-stats-results
cat > size-stats-results/message.md <<'EOF'
${{ steps.message.outputs.message }}
EOF

- name: Upload size stats results
uses: actions/upload-artifact@v4
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
title: '**Size stats**'
message: ${{ steps.message.outputs.message }}
update-if-present: 'true'
name: size-stats-results
path: size-stats-results
if-no-files-found: error
Loading