-
Notifications
You must be signed in to change notification settings - Fork 0
149 lines (126 loc) · 6.61 KB
/
create-demo-attachments.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
name: Create Demo and Attachment Files
on:
pull_request:
# Cancels all previous workflow runs for pull requests that have not completed.
concurrency:
# The concurrency group contains the workflow name and the branch name for pull requests
# or the commit hash for any other events.
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }}
cancel-in-progress: true
jobs:
build:
name: Create Demo and Attachments
runs-on: ubuntu-latest
if: |
always() && (
github.event_name == 'pull_request' ||
github.event_name == 'workflow_dispatch' ||
github.repository == 'blockeraai/blockera'
)
steps:
- name: Checkout code
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- name: Setup Node.js and install dependencies
uses: ./.github/setup-node
- name: Setup PHP Composer install dependencies
uses: ./.github/setup-php
with:
composer-options: '--no-dev -o --apcu-autoloader -a'
- name: Build Plugin Zip File
uses: ./.github/build-plugin-zip
with:
main-file-suffix: '-${{ github.event.pull_request.number }}'
- name: Upload ZIP to FTP server
uses: nerdoza/action-simple-file-upload@v2
with:
host: ${{ secrets.FTP_HOST }}
user: ${{ secrets.FTP_USERNAME }}
password: ${{ secrets.FTP_PASSWORD }}
src: blockera.zip
dest: /domains/cd.blockera.ai/public_html/blockera-${{ github.event.pull_request.number }}.zip
- name: Echo uploaded file link
run: |
echo "uploaded_zip_file=https://${{ secrets.FTP_HOST }}/blockera-${{ github.event.pull_request.number }}.zip" >> $GITHUB_ENV
- name: Find existing bot comment
id: find_comment
run: |
PR_NUMBER=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")
COMMENTS_URL="https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/comments"
# Fetch all comments and find if one from blockerabot exists
curl -H "Authorization: token ${{ secrets.BLOCKERABOT_PAT }}" \
-H "Accept: application/vnd.github.v3+json" \
$COMMENTS_URL | jq -r '.[] | select(.user.login == "blockerabot" and (.body | test("Branch Links"; "i"))) | .id' > comment_id.txt
# If comment ID is found, set it to environment variable
if [[ -s comment_id.txt ]]; then
COMMENT_ID=$(cat comment_id.txt)
echo "comment_id=${COMMENT_ID}" >> $GITHUB_ENV
fi
# Step to encode JSON with Base64
- name: Encode JSON with Base64
id: encode_json
run: |
# Define the original JSON
JSON_STRING='{
"meta": {
"title": "Install Blockera plugin from github artifact",
"author": "blockera.ai",
"description": "Install and activate a WordPress Blockera plugin from a .php file stored in a github artifact.",
"categories": ["plugins"]
},
"landingPage": "/wp-admin/plugins.php",
"preferredVersions": {
"wp": "beta",
"php": "8.0"
},
"steps": [
{
"step": "login"
},
{
"step": "installPlugin",
"pluginZipFile": {
"resource": "url",
"url": "URL_PLACEMENT"
}
}
]
}'
# Replace URL_PLACEMENT with the newly generated URL
ENCODED_JSON=$(echo "$JSON_STRING" | jq --arg new_url "${{ env.uploaded_zip_file }}" '.steps[1].pluginZipFile.url = "${{ env.uploaded_zip_file }}"')
# Base64 encode the updated JSON
BASE64_ENCODED_JSON=$(echo "$ENCODED_JSON" | base64 -w 0)
# Generate the final URL with encoded JSON appended
DEMO_URL="https://playground.wordpress.net/#${BASE64_ENCODED_JSON}"
# Save the URL as an output for later steps if needed
echo "demo_url=$DEMO_URL" >> $GITHUB_ENV
# Print the URL to the GitHub workflow output
echo "Encoded URL: $DEMO_URL"
# Optional: Output the final URL as a GitHub Action output
- name: Set output
run: |
echo "demo_url=${{ env.demo_url }}" >> $GITHUB_OUTPUT
- name: Post or update comment with download link
run: |
PR_NUMBER=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")
REPO_NAME="${{ github.repository }}"
RUN_ID="${{ github.run_id }}"
# Define the comment body following the desired structure
COMMENT_BODY="**Branch Links**\n\n- [**🔗 Playground Demo**](${{ env.demo_url }})\n\n- [**📦 Download Zip** (Build)](${{ env.uploaded_zip_file }})"
if [ -z "${{ env.comment_id }}" ]; then
# No existing comment found, create a new one
curl -X POST \
-H "Authorization: token ${{ secrets.BLOCKERABOT_PAT }}" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/issues/$PR_NUMBER/comments \
-d "{\"body\":\"${COMMENT_BODY}\"}"
else
# Existing comment found, update it
COMMENT_ID=${{ env.comment_id }}
COMMENT_URL="https://api.github.com/repos/${{ github.repository }}/issues/comments/$COMMENT_ID"
# Update the existing comment with the new content
curl -X PATCH \
-H "Authorization: token ${{ secrets.BLOCKERABOT_PAT }}" \
-H "Accept: application/vnd.github.v3+json" \
$COMMENT_URL \
-d "{\"body\":\"${COMMENT_BODY}\"}"
fi