Skip to content

Commit d25367f

Browse files
committed
use only one input (PR number) for workflow_dispatch
1 parent bfd2c74 commit d25367f

1 file changed

Lines changed: 61 additions & 37 deletions

File tree

.github/workflows/upload-dev-build.yml

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ on:
1010
pr_number:
1111
description: 'PR Number to deploy'
1212
required: false
13-
run_id:
14-
description: 'The Run ID of the CI workflow that has the artifact'
15-
required: true
13+
14+
env:
15+
ARTIFACT_UPLOAD_WORKFLOW_NAME: "Publish Dist"
16+
UPLOAD_DIR_NAME: "upload"
1617

1718
jobs:
1819
upload:
@@ -25,35 +26,24 @@ jobs:
2526
github.event.workflow_run.conclusion == 'success'
2627
)
2728
steps:
28-
- name: Download build artifact
29-
id: download-artifact
30-
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
31-
with:
32-
name: dist # uploaded by publish-dist.yml > publish-dist
33-
run-id: ${{ github.event.workflow_run.id || inputs.run_id }}
34-
github-token: ${{ secrets.GITHUB_TOKEN }}
35-
path: temp-dist
36-
37-
- name: Setup metadata and prepare folders
38-
id: setup-metadata
29+
- name: Get required metadata (PR number, commit SHA, workflow run ID containing artifacts)
30+
id: get-metadata
3931
env:
4032
GH_TOKEN: ${{ github.token }}
41-
PR_NUM: ${{ github.event.workflow_run.pull_requests[0].number || inputs.pr_number }}
4233
GH_EVENT_NAME: ${{ github.event_name }}
43-
GH_HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
4434
GH_REPO: ${{ github.repository }}
35+
SHA: ${{ github.event.workflow_run.sha }}
36+
RUN_ID: ${{ github.event.workflow_run.id }}
37+
PR_NUM: ${{ github.event.workflow_run.pull_requests[0].number || inputs.pr_number }}
4538
run: |
46-
# Get SHA from triggering workflow, or from manual input
47-
if [ "${GH_EVENT_NAME}" == "workflow_run" ]; then
48-
SHA="${GH_HEAD_SHA}"
49-
else
50-
echo "Fetching latest SHA for PR #${PR_NUM}..."
39+
# Get SHA from manually-provided PR number if triggered by workflow_dispatch
40+
if [ "${GH_EVENT_NAME}" == "workflow_dispatch" ]; then
5141
if [ -n "${PR_NUM}" ]; then
5242
SHA=$(gh pr view "${PR_NUM}" --repo "${GH_REPO}" --json headRefOid --template '{{.headRefOid}}')
5343
fi
5444
fi
5545
56-
# Validate that we have a SHA
46+
# At this point, SHA should be defined. If not, fail the workflow
5747
if [ -z "${SHA}" ]; then
5848
echo "Failed to get commit SHA, exiting"
5949
exit 1
@@ -65,14 +55,50 @@ jobs:
6555
fi
6656
6757
# Validate that we have a valid PR number
68-
if [ -z "${PR_NUM}" ] || [[ "${PR_NUM}" =~ ^[1-9][0-9]{0,4}$ ]]; then
58+
if [ -z "${PR_NUM}" ] || [[ ! "${PR_NUM}" =~ ^[1-9][0-9]{0,4}$ ]]; then
6959
echo "Failed to get PR number, exiting (PR_NUM=${PR_NUM})"
7060
exit 1
7161
fi
7262
73-
SHORT_SHA=${SHA::7}
74-
UPLOAD_DIR_NAME="upload"
63+
# If RUN_ID is empty, use the gh CLI to get the most recent run ID for SHA
64+
if [ -z "${RUN_ID}" ]; then
65+
RUN_ID=$(gh run list \
66+
--workflow "${ARTIFACT_UPLOAD_WORKFLOW_NAME}" \
67+
--commit "${SHA}" \
68+
--limit 1 \
69+
--json databaseId \
70+
--jq '.[0].databaseId')
71+
fi
72+
73+
# At this point, RUN_ID should be defined. If not, fail the workflow
74+
if [ -z "${RUN_ID}" ]; then
75+
echo "Failed to get workflow run ID, exiting"
76+
exit 1
77+
fi
78+
79+
# Save PR number, commit SHA, short SHA, and run ID to output
80+
echo "PR_NUM=${PR_NUM}" >> $GITHUB_OUTPUT
81+
echo "SHA=${SHA}" >> $GITHUB_OUTPUT
82+
echo "SHORT_SHA=${SHA:0:7}" >> $GITHUB_OUTPUT
83+
echo "RUN_ID=${RUN_ID}" >> $GITHUB_OUTPUT
84+
85+
- name: Download build artifact
86+
id: download-artifact
87+
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
88+
with:
89+
name: dist # uploaded by publish-dist.yml > publish-dist
90+
run-id: ${{ steps.get-metadata.outputs.RUN_ID }}
91+
github-token: ${{ secrets.GITHUB_TOKEN }}
92+
path: temp-dist
7593

94+
- name: Prepare folders
95+
id: setup-metadata
96+
env:
97+
GH_TOKEN: ${{ github.token }}
98+
PR_NUM: ${{ steps.get-metadata.outputs.PR_NUM }}
99+
SHA: ${{ steps.get-metadata.outputs.SHA }}
100+
SHORT_SHA: ${{ steps.get-metadata.outputs.SHORT_SHA }}
101+
run: |
76102
echo "SHA: ${SHA}"
77103
echo "Short SHA: ${SHORT_SHA}"
78104
echo "PR number: ${PR_NUM}"
@@ -89,10 +115,6 @@ jobs:
89115
echo "Created directory ${UPLOAD_DIR_FULL_PATH} with the following contents:"
90116
echo "$(ls -lR ${UPLOAD_DIR_FULL_PATH})"
91117
92-
echo "PR_NUM=${PR_NUM}" >> $GITHUB_OUTPUT
93-
echo "SHA=${SHA}" >> $GITHUB_OUTPUT
94-
echo "SHORT_SHA=${SHORT_SHA}" >> $GITHUB_OUTPUT
95-
96118
- name: Generate GitHub App token
97119
id: generate-token
98120
uses: actions/create-github-app-token@1b10c78c7865c340bc4f6099eb2f838309f1e8c3 #v3.1.1
@@ -112,12 +134,11 @@ jobs:
112134
- name: Commit and push files
113135
id: commit-and-push
114136
env:
115-
PR_NUM: ${{ steps.setup-metadata.outputs.PR_NUM }}
116-
SHORT_SHA: ${{ steps.setup-metadata.outputs.SHORT_SHA }}
137+
PR_NUM: ${{ steps.get-metadata.outputs.PR_NUM }}
138+
SHORT_SHA: ${{ steps.get-metadata.outputs.SHORT_SHA }}
117139
run: |
118-
# Move 'upload/pr-NNNN/' directory into repo folder and cd into repo root
119-
TARGET_DIR="upload/pr-${PR_NUM}"
120-
mkdir -p plotly.js-dev-builds/${TARGET_DIR}
140+
# Move 'pr-NNNN/' directory into upload directory inside repo and cd into repo root
141+
TARGET_DIR="${UPLOAD_DIR_NAME}/pr-${PR_NUM}"
121142
cp -r ${TARGET_DIR} plotly.js-dev-builds/
122143
cd plotly.js-dev-builds
123144
@@ -143,10 +164,13 @@ jobs:
143164
fi
144165
145166
- name: Generate summary
167+
env:
168+
PR_NUM: ${{ steps.get-metadata.outputs.PR_NUM }}
169+
SHORT_SHA: ${{ steps.get-metadata.outputs.SHORT_SHA }}
146170
run: |
147-
BASE="https://plotly.github.io/plotly.js-dev-builds/upload/pr-${PR_NUM}"
171+
BASE_URL="https://plotly.github.io/plotly.js-dev-builds/${UPLOAD_DIR_NAME}/pr-${PR_NUM}"
148172
echo "### PR Build Uploaded" >> $GITHUB_STEP_SUMMARY
149173
echo "Builds for PR #${PR_NUM} can be accessed at:" >> $GITHUB_STEP_SUMMARY
150-
echo "- Latest build for this PR: [${BASE}/latest/plotly.min.js](${BASE}/latest/plotly.min.js)" >> $GITHUB_STEP_SUMMARY
151-
echo "- Build for this commit: [${BASE}/${SHORT_SHA}/plotly.min.js](${BASE}/${SHORT_SHA}/plotly.min.js)" >> $GITHUB_STEP_SUMMARY
174+
echo "- Latest build for this PR: [${BASE_URL}/latest/plotly.min.js](${BASE_URL}/latest/plotly.min.js)" >> $GITHUB_STEP_SUMMARY
175+
echo "- Build for this commit: [${BASE_URL}/${SHORT_SHA}/plotly.min.js](${BASE_URL}/${SHORT_SHA}/plotly.min.js)" >> $GITHUB_STEP_SUMMARY
152176
echo "The above links should start working a minute or two after this job completes." >> $GITHUB_STEP_SUMMARY

0 commit comments

Comments
 (0)