Skip to content

Add build plugin artifact to releases branch #3232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: releases
Choose a base branch
from
Open
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
142 changes: 142 additions & 0 deletions .github/workflows/build-plugin-artifact.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
name: Build Plugin Artifact

on:
pull_request:
types: [opened, synchronize, reopened]

jobs:
build-and-upload:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write

steps:
- uses: actions/checkout@v1

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
tools: composer

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
cache-dependency-path: './package-lock.json'
cache: 'npm'

- name: Install dependencies
run: |
npm ci || npm install
composer install

- name: Build plugin
run: npm run build

- name: Store built plugin
uses: actions/upload-artifact@master
continue-on-error: true
with:
name: facebook-for-woocommerce
path: facebook-for-woocommerce.zip

- name: Check if build succeeded
id: check_build
run: |
if [ -f "facebook-for-woocommerce.zip" ]; then
echo "build_success=true" >> $GITHUB_OUTPUT
echo "filesize=$(du -h facebook-for-woocommerce.zip | cut -f1)" >> $GITHUB_OUTPUT
else
echo "build_success=false" >> $GITHUB_OUTPUT
fi

- name: Create simple summary
run: echo "⬇️ Scroll to the bottom of this page and download the 'facebook-for-woocommerce' artifact to get the latest plugin build." > $GITHUB_STEP_SUMMARY

- name: Post build info comment on PR
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// Get PR number from pull_request event
const prNumber = context.payload.pull_request.number;
console.log(`Preparing build info for PR #${prNumber}`);

const runUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
const timestamp = new Date().toISOString();
const commitSha = context.sha.substring(0, 7);

// Create build info comment
let commentBody = '';
if ("${{ steps.check_build.outputs.build_success }}" === "true") {
commentBody = "## 📦 Latest Plugin Build\n\n" +
`**Built at:** ${timestamp}\n` +
`**Commit:** ${commitSha}\n` +
`**Size:** ${{ steps.check_build.outputs.filesize }}\n\n` +
`**Download:** [Click here to download the plugin](${runUrl})\n\n` +
`_To download: Click the link above → Scroll to bottom → Download "facebook-for-woocommerce" artifact_`;
} else {
commentBody = "## ❌ Plugin Build Failed\n\n" +
`**Attempted at:** ${timestamp}\n` +
`**Commit:** ${commitSha}\n\n` +
`Please check the [workflow logs](${runUrl}) for more information.`;
}

// Add build info to workflow summary regardless
core.summary
.addHeading(("${{ steps.check_build.outputs.build_success }}" === "true") ?
"📦 Latest Plugin Build" : "❌ Plugin Build Failed")
.addRaw(commentBody)
.write();

// Look for an existing comment from this workflow
const commentMarker = '## 📦 Latest Plugin Build';
const failMarker = '## ❌ Plugin Build Failed';

let existingCommentId = null;

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

// Look for comments that contain our markers
for (const comment of comments.data) {
if (comment.body.includes(commentMarker) || comment.body.includes(failMarker)) {
existingCommentId = comment.id;
break;
}
}
} catch (error) {
console.log(`Error listing comments: ${error.message}`);
}

// Update or create comment
try {
if (existingCommentId) {
// Update existing comment
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingCommentId,
body: commentBody
});
console.log(`Updated existing comment ID: ${existingCommentId}`);
} else {
// Create new comment if no existing one found
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: commentBody
});
console.log("Created new comment");
}
} catch (error) {
console.log("Unable to update or create comment. Build information is still available in the workflow summary.");
console.log(`Error details: ${error.message}`);
}