Skip to content
Merged
19 changes: 13 additions & 6 deletions docs/jobs/pre-build-job.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@

# Configuration
REPO="canonical/snapd"
# Ensure this matches the actual filename of your YAML workflow in the snapd repo
WORKFLOW="build-documentation.yaml"
ARTIFACT_NAME="openapi-spec" # Updated to match the new GitHub Action output
ARTIFACT_NAME="openapi-spec"
TARGET_DIR="${SOURCEDIR}/_html_extra/reference/api"
N_RUNS=100

mkdir -p "${TARGET_DIR}"

echo "Searching for the latest successful run that has artifact '${ARTIFACT_NAME}'..."
echo "Searching for the latest successful ${WORKFLOW} run that has artifact '${ARTIFACT_NAME}'..."

RUN_IDS=$(gh run list \
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i think this still fails currently for snap-docs PR builds as it requires authentication token (it works fine for master pushes without token)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

.readthedocs.yaml sets GITHUB_TOKEN, which the gh cli should respect. I don't see this script being called anywhere else, so I believe there is no need to add the token here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

yes the GITHUB_TOKEN env var is set for this script's execution (via .readthedocs.yaml) but AFAICT it will likely be empty as RTD_TOKEN is likely not available on PRs from forks.

You can see the build failing to authenticate gh at this step: https://app.readthedocs.com/projects/canonical-snap/builds/3870069/

-R "${REPO}" \
--workflow "${WORKFLOW}" \
--status success \
--limit 100 \
--branch master \
Comment thread
Mohit-Chachada marked this conversation as resolved.
--limit "${N_RUNS}" \
--json databaseId \
-q '.[].databaseId')

Comment thread
Mohit-Chachada marked this conversation as resolved.
if [ -z "$RUN_IDS" ]; then
echo "Warning: No successful runs found for '${WORKFLOW}' workflow from the master branch of '${REPO}' repo."
fi

DOWNLOAD_SUCCESS=false

for id in $RUN_IDS; do
Expand All @@ -37,8 +42,10 @@ for id in $RUN_IDS; do
done

if [ "$DOWNLOAD_SUCCESS" = false ]; then
echo "Error: Checked the last 100 successful runs, but none contained the artifact '${ARTIFACT_NAME}'."
exit 1
RUNS_FOUND=$(echo "$RUN_IDS" | wc -l)
echo "Warning: Checked the last ${RUNS_FOUND} successful runs, but none contained the artifact '${ARTIFACT_NAME}'."
mv ${SOURCEDIR}/_html_extra/reference/development/snapd-rest-api/openapi.json ${TARGET_DIR}/openapi.json
echo "Using local copy of openapi.json instead."
fi

echo "OpenAPI spec successfully downloaded to ${TARGET_DIR}/openapi.json"
Loading