diff --git a/.github/workflows/ti_platforms_jenkins.yaml b/.github/workflows/ti_platforms_jenkins.yaml index 557fbbb71803..42e13b8f4856 100644 --- a/.github/workflows/ti_platforms_jenkins.yaml +++ b/.github/workflows/ti_platforms_jenkins.yaml @@ -1,9 +1,11 @@ name: Build TI platforms (Jenkins) -# Starts the zsod_utils_twister_build Jenkins job for each platform on the -# pull request and waits for it. Same build-only twister run as +# Starts the zsod_utils_twister_build_all Jenkins job for the pull request and +# waits for it. That master job builds every TI platform in parallel (one +# zsod_utils_twister_build worker per platform). Same build-only twister run as # ti_platforms_build.yaml, on the internal Jenkins nodes. The result comes back -# as one check per platform and one comment on the pull request. +# as one check and one comment on the pull request. Cancelling this run (a new +# push) stops the master, and the master stops its workers. # # The EPSW_JENKINS_* secrets are for jenkins-epsw.itg.ti.com, where this job # lives. The JENKINS_* secrets of zephyr-board-ci.yml are for another Jenkins. @@ -27,43 +29,28 @@ concurrency: cancel-in-progress: true env: - JENKINS_JOB: zsod_utils_twister_build + JENKINS_JOB: zsod_utils_twister_build_all jobs: build: - name: Jenkins build (${{ matrix.platform }}) + name: Jenkins builds of the TI platforms runs-on: [self-hosted, Linux, X64] # the self-hosted runner that can reach Jenkins timeout-minutes: 720 - strategy: - fail-fast: false - matrix: - platform: - - lp_am13e230 - - lp_mspm0g3507 - - lp_mspm0g3519 - - lp_mspm0l2228 - - lp_mspm33c321a - - am62l_evm/am62l3/a53 + permissions: + contents: read + pull-requests: write env: SECRET_URL: ${{ secrets.EPSW_JENKINS_URL }} JENKINS_USER: ${{ secrets.EPSW_JENKINS_USER }} JENKINS_TOKEN: ${{ secrets.EPSW_JENKINS_TOKEN }} - PLATFORM: ${{ matrix.platform }} PR_NUMBER: ${{ github.event.pull_request.number }} steps: - - name: Run the Jenkins build and wait for it + - name: Start the Jenkins master job and wait for it shell: bash run: | set -uo pipefail cd "$RUNNER_TEMP" - - # leave an outcome behind for the comment job, whatever happens - RESULT="ERROR" - BUILD_URL="" - finish() { - printf '%s\n%s\n' "$RESULT" "$BUILD_URL" > "$RUNNER_TEMP/result.txt" - } - trap finish EXIT + : > "$RUNNER_TEMP/build-url" [[ -n "${SECRET_URL:-}" ]] || { echo "ERROR: the EPSW_JENKINS_URL secret is not set"; exit 1; } [[ "$PR_NUMBER" =~ ^[0-9]+$ ]] || { echo "ERROR: invalid PR number"; exit 1; } @@ -74,132 +61,268 @@ jobs: # Jenkins is internal and the site proxy intercepts its TLS, so these # calls bypass the proxy. Credentials go in a file, not on the # command line (visible in the process list of the shared host). - JOPTS="--noproxy $H" NETRC=$(mktemp); chmod 600 "$NETRC" + trap 'rm -f "$NETRC"' EXIT printf 'machine %s login %s password %s\n' "$H" "$JENKINS_USER" "$JENKINS_TOKEN" > "$NETRC" - JOPTS="$JOPTS --netrc-file $NETRC" - trap 'rm -f "$NETRC"; finish' EXIT + JOPTS="--noproxy $H --netrc-file $NETRC" # the CSRF crumb is tied to the session, so crumb and build share a cookie jar - jar=$(mktemp) + jar=$(mktemp); hdrs=$(mktemp) json=$(curl -sf $JOPTS -c "$jar" "${URL}/crumbIssuer/api/json" || true) field=$(echo "$json" | jq -r '.crumbRequestField // empty' 2>/dev/null || true) crumb=$(echo "$json" | jq -r '.crumb // empty' 2>/dev/null || true) - hdrs=$(mktemp) code=$(curl -s $JOPTS -o /dev/null -D "$hdrs" -w '%{http_code}' -X POST -b "$jar" -c "$jar" \ - ${field:+-H "$field: $crumb"} --get \ - --data-urlencode "PLATFORM=${PLATFORM}" --data-urlencode "PR_NUMBER=${PR_NUMBER}" \ + ${field:+-H "$field: $crumb"} --get --data-urlencode "PR_NUMBER=${PR_NUMBER}" \ "${URL}/job/${JENKINS_JOB}/buildWithParameters") - rm -f "$jar" - if [[ "$code" != "201" ]]; then + QUEUE=$(grep -i '^location:' "$hdrs" | tr -d '\r' | awk '{print $2}') + rm -f "$jar" "$hdrs" + if [[ "$code" != "201" || -z "$QUEUE" ]]; then echo "ERROR: Jenkins refused the build request (HTTP $code). Check the credentials and Job/Build on ${JENKINS_JOB}." exit 1 fi - QUEUE=$(grep -i '^location:' "$hdrs" | tr -d '\r' | awk '{print $2}') echo "Queued: $QUEUE" + printf 'queue\t%s\n' "$QUEUE" > "$RUNNER_TEMP/build-url" # queue item -> build url + BUILD_URL="" for _ in $(seq 1 120); do BUILD_URL=$(curl -sf $JOPTS "${QUEUE}api/json" | jq -r '.executable.url // empty') || BUILD_URL="" [[ -n "$BUILD_URL" ]] && break sleep 15 done - [[ -n "$BUILD_URL" ]] || { RESULT="TIMEOUT"; echo "ERROR: the build did not start within 30 minutes"; exit 1; } - echo "Jenkins build: $BUILD_URL" - # lets the cancel step stop this build - printf '%s' "$BUILD_URL" > "$RUNNER_TEMP/build-url" - echo "[Jenkins build](${BUILD_URL}console)" >> "$GITHUB_STEP_SUMMARY" + [[ -n "$BUILD_URL" ]] || { echo "ERROR: the build did not start within 30 minutes"; exit 1; } + echo "Jenkins master build: $BUILD_URL" + printf 'build\t%s\n' "$BUILD_URL" > "$RUNNER_TEMP/build-url" + echo "[Jenkins master build](${BUILD_URL}console)" >> "$GITHUB_STEP_SUMMARY" # wait for it, 12 hours at most + RESULT="TIMEOUT" for _ in $(seq 1 1440); do b=$(curl -sf $JOPTS "${BUILD_URL}api/json?tree=building,result") || b="" if [[ -n "$b" && "$(echo "$b" | jq -r .building)" == "false" ]]; then RESULT=$(echo "$b" | jq -r '.result // "ERROR"') - echo "Jenkins result: $RESULT" - curl -sf $JOPTS "${BUILD_URL}consoleText" \ - | grep -E 'configurations passed|ERROR +Build failure' | tail -20 || true - [[ "$RESULT" == "SUCCESS" ]] - exit + break fi sleep 30 done - RESULT="TIMEOUT" - echo "ERROR: the Jenkins build did not finish in 12 hours" - exit 1 + echo "Jenkins master result: $RESULT" + + # one PLATFORM_RESULT line per platform, printed by the master + curl -sf $JOPTS "${BUILD_URL}consoleText" | grep -o 'PLATFORM_RESULT: .*' | sort -u > "$RUNNER_TEMP/platforms.txt" || true + cat "$RUNNER_TEMP/platforms.txt" + printf '%s\n' "$RESULT" > "$RUNNER_TEMP/result.txt" + { + echo "### Jenkins builds: $RESULT" + echo '```' + sed 's/^PLATFORM_RESULT: //' "$RUNNER_TEMP/platforms.txt" + echo '```' + } >> "$GITHUB_STEP_SUMMARY" + [[ "$RESULT" == "SUCCESS" ]] - - name: Stop the Jenkins build when this run is cancelled + - name: Stop the Jenkins master when this run is cancelled if: cancelled() shell: bash run: | [[ -s "$RUNNER_TEMP/build-url" ]] || exit 0 - BUILD_URL=$(cat "$RUNNER_TEMP/build-url") + KIND=$(cut -f1 "$RUNNER_TEMP/build-url"); TARGET=$(cut -f2 "$RUNNER_TEMP/build-url") URL="${SECRET_URL%/}"; H="${URL#*://}"; H="${H%%/*}"; H="${H%%:*}" NETRC=$(mktemp); chmod 600 "$NETRC" trap 'rm -f "$NETRC"' EXIT printf 'machine %s login %s password %s\n' "$H" "$JENKINS_USER" "$JENKINS_TOKEN" > "$NETRC" + JOPTS="--noproxy $H --netrc-file $NETRC" jar=$(mktemp) - json=$(curl -sf --noproxy "$H" --netrc-file "$NETRC" -c "$jar" "${URL}/crumbIssuer/api/json" || true) + json=$(curl -sf $JOPTS -c "$jar" "${URL}/crumbIssuer/api/json" || true) field=$(echo "$json" | jq -r '.crumbRequestField // empty' 2>/dev/null || true) crumb=$(echo "$json" | jq -r '.crumb // empty' 2>/dev/null || true) - curl -s --noproxy "$H" --netrc-file "$NETRC" -o /dev/null -X POST -b "$jar" \ - ${field:+-H "$field: $crumb"} "${BUILD_URL}stop" && echo "Stopped ${BUILD_URL}" + if [[ "$KIND" == "build" ]]; then + # the master pipeline aborts the workers it started + curl -s $JOPTS -o /dev/null -X POST -b "$jar" ${field:+-H "$field: $crumb"} "${TARGET}stop" \ + && echo "Stopped the master: $TARGET" + else + ID=$(basename "${TARGET%/}") + curl -s $JOPTS -o /dev/null -X POST -b "$jar" ${field:+-H "$field: $crumb"} \ + "${URL}/queue/cancelItem?id=${ID}" && echo "Removed the master from the queue" + fi rm -f "$jar" - - name: Save the result for the comment + - name: Collect the twister log of each platform + id: prep if: always() shell: bash + env: + ATTEMPT: ${{ github.run_attempt }} run: | - mkdir -p "$RUNNER_TEMP/out" - { echo "$PLATFORM"; cat "$RUNNER_TEMP/result.txt" 2>/dev/null || printf 'ERROR\n\n'; } \ - > "$RUNNER_TEMP/out/result.txt" - echo "ARTIFACT=result-${PLATFORM//\//_}" >> "$GITHUB_ENV" + set -uo pipefail + mkdir -p "$RUNNER_TEMP/twister-logs" + [[ -s "$RUNNER_TEMP/platforms.txt" && -s "$RUNNER_TEMP/build-url" ]] || exit 0 + [[ -n "${SECRET_URL:-}" ]] || exit 0 + URL="${SECRET_URL%/}"; H="${URL#*://}"; H="${H%%/*}"; H="${H%%:*}" + NETRC=$(mktemp); chmod 600 "$NETRC" + trap 'rm -f "$NETRC"' EXIT + printf 'machine %s login %s password %s\n' "$H" "$JENKINS_USER" "$JENKINS_TOKEN" > "$NETRC" + JOPTS="--noproxy $H --netrc-file $NETRC" - - name: Upload the result - if: always() - uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0 + MASTER=$(cut -f2 "$RUNNER_TEMP/build-url"); MASTER="${MASTER%/}" + MNUM="${MASTER##*/}" + n=0 + while read -r _ P R B; do + [[ -n "${P:-}" && -n "${B:-}" ]] || continue + B="${B%/}/" + # only take files of a build that this master started + cause=$(curl -sf $JOPTS "${B}api/json?tree=actions%5Bcauses%5BupstreamProject,upstreamBuild%5D%5D" \ + | jq -r '[.actions[]?.causes[]? | select(.upstreamProject != null) | "\(.upstreamProject)#\(.upstreamBuild)"] | first // empty') || cause="" + if [[ "$cause" != "${JENKINS_JOB}#${MNUM}" ]]; then + echo "Skip $P: the build was not started by this master ($cause)"; continue + fi + safe="${P//\//_}" + f="pr${PR_NUMBER}-${ATTEMPT}-${safe}-twister.log" + curl -sf $JOPTS -o "$RUNNER_TEMP/twister-logs/$f" "${B}artifact/artifacts/logs/twister_build.log" \ + || { echo "No twister log for $P"; rm -f "$RUNNER_TEMP/twister-logs/$f"; continue; } + n=$((n+1)) + if [[ $n -le 8 ]]; then + echo "board$n=$P" >> "$GITHUB_OUTPUT" + echo "file$n=$RUNNER_TEMP/twister-logs/$f" >> "$GITHUB_OUTPUT" + fi + done < "$RUNNER_TEMP/platforms.txt" + echo "Collected $n twister logs" + ls -l "$RUNNER_TEMP/twister-logs" + + - name: Twister logs of all platforms + id: logs + if: always() && steps.prep.outputs.file1 != '' + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: - name: ${{ env.ARTIFACT }} - path: ${{ runner.temp }}/out/result.txt - - comment: - name: Comment the Jenkins results on the pull request - needs: build - if: always() && needs.build.result != 'skipped' - runs-on: ubuntu-24.04 - permissions: - pull-requests: write - steps: - - name: Download the results - uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0 + name: twister-logs-pr${{ github.event.pull_request.number }}-${{ github.run_attempt }} + path: ${{ runner.temp }}/twister-logs + retention-days: 14 + + - name: Twister log 1 + id: log1 + if: always() && steps.prep.outputs.file1 != '' + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + path: ${{ steps.prep.outputs.file1 }} + archive: false + retention-days: 14 + + - name: Twister log 2 + id: log2 + if: always() && steps.prep.outputs.file2 != '' + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + path: ${{ steps.prep.outputs.file2 }} + archive: false + retention-days: 14 + + - name: Twister log 3 + id: log3 + if: always() && steps.prep.outputs.file3 != '' + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + path: ${{ steps.prep.outputs.file3 }} + archive: false + retention-days: 14 + + - name: Twister log 4 + id: log4 + if: always() && steps.prep.outputs.file4 != '' + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + path: ${{ steps.prep.outputs.file4 }} + archive: false + retention-days: 14 + + - name: Twister log 5 + id: log5 + if: always() && steps.prep.outputs.file5 != '' + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: - pattern: result-* - path: results + path: ${{ steps.prep.outputs.file5 }} + archive: false + retention-days: 14 - - name: Comment on the pull request + - name: Twister log 6 + id: log6 + if: always() && steps.prep.outputs.file6 != '' + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + path: ${{ steps.prep.outputs.file6 }} + archive: false + retention-days: 14 + + - name: Twister log 7 + id: log7 + if: always() && steps.prep.outputs.file7 != '' + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + path: ${{ steps.prep.outputs.file7 }} + archive: false + retention-days: 14 + + - name: Twister log 8 + id: log8 + if: always() && steps.prep.outputs.file8 != '' + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + path: ${{ steps.prep.outputs.file8 }} + archive: false + retention-days: 14 + + - name: Comment the results on the pull request + if: always() shell: bash env: GH_TOKEN: ${{ github.token }} REPO: ${{ github.repository }} - PR_NUMBER: ${{ github.event.pull_request.number }} PR_SHA: ${{ github.event.pull_request.head.sha }} RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} + LOGS_URL: ${{ steps.logs.outputs.artifact-url }} + LOG_BOARD_1: ${{ steps.prep.outputs.board1 }} + LOG_URL_1: ${{ steps.log1.outputs.artifact-url }} + LOG_BOARD_2: ${{ steps.prep.outputs.board2 }} + LOG_URL_2: ${{ steps.log2.outputs.artifact-url }} + LOG_BOARD_3: ${{ steps.prep.outputs.board3 }} + LOG_URL_3: ${{ steps.log3.outputs.artifact-url }} + LOG_BOARD_4: ${{ steps.prep.outputs.board4 }} + LOG_URL_4: ${{ steps.log4.outputs.artifact-url }} + LOG_BOARD_5: ${{ steps.prep.outputs.board5 }} + LOG_URL_5: ${{ steps.log5.outputs.artifact-url }} + LOG_BOARD_6: ${{ steps.prep.outputs.board6 }} + LOG_URL_6: ${{ steps.log6.outputs.artifact-url }} + LOG_BOARD_7: ${{ steps.prep.outputs.board7 }} + LOG_URL_7: ${{ steps.log7.outputs.artifact-url }} + LOG_BOARD_8: ${{ steps.prep.outputs.board8 }} + LOG_URL_8: ${{ steps.log8.outputs.artifact-url }} run: | set -uo pipefail MARKER='' API="https://api.github.com/repos/${REPO}" + declare -A LOGURL + for i in 1 2 3 4 5 6 7 8; do + b="LOG_BOARD_$i"; u="LOG_URL_$i" + [[ -n "${!b:-}" ]] && LOGURL["${!b}"]="${!u:-}" + done + ROWS="" - for f in results/*/result.txt; do - [[ -f "$f" ]] || continue - P=$(sed -n 1p "$f"); R=$(sed -n 2p "$f"); B=$(sed -n 3p "$f") + # missing when the run was cancelled before the master finished + touch "$RUNNER_TEMP/platforms.txt" + while read -r _ P R _; do + [[ -n "${P:-}" ]] || continue case "$R" in SUCCESS) I=":white_check_mark:" ;; FAILURE|UNSTABLE) I=":x:" ;; *) I=":warning:" ;; esac - L="-"; [[ -n "$B" ]] && L="[build](${B}console)" + L="-"; [[ -n "${LOGURL[$P]:-}" ]] && L="[log](${LOGURL[$P]})" ROWS+=$(printf '| %s | %s %s | %s |\n' "$P" "$I" "$R" "$L")$'\n' - done - [[ -n "$ROWS" ]] || ROWS=$'| - | no result | - |\n' + done < "$RUNNER_TEMP/platforms.txt" + if [[ -z "$ROWS" ]]; then + RES=$(cat "$RUNNER_TEMP/result.txt" 2>/dev/null || echo "not finished") + ROWS=$(printf '| all | :warning: %s | - |\n' "$RES")$'\n' + fi + + ALL="" + [[ -n "${LOGS_URL:-}" ]] && ALL=$(printf 'All twister logs in one download: [twister-logs](%s) (kept 14 days)\n\n' "$LOGS_URL") - BODY=$(printf '%s\n## Jenkins build of the TI platforms\n\nCommit `%s`\n\n| Platform | Result | Jenkins |\n| --- | --- | --- |\n%s\n[Workflow run](%s)\n' \ - "$MARKER" "${PR_SHA:0:12}" "$ROWS" "$RUN_URL") + BODY=$(printf '%s\n## Jenkins build of the TI platforms\n\nCommit `%s`\n\n| Platform | Result | Log |\n| --- | --- | --- |\n%s\n%s[Workflow run](%s)\n' \ + "$MARKER" "${PR_SHA:0:12}" "$ROWS" "$ALL" "$RUN_URL") # one comment per pull request, updated in place; only a comment of this bot ID=$(curl -sf -H "Authorization: Bearer $GH_TOKEN" \