fix: trim non-essential dirs before arch-docs to reduce zip size #4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: trunk-tagging | |
| on: | |
| push: | |
| branches: | |
| - main | |
| workflow_dispatch: | |
| inputs: | |
| commit_sha: | |
| description: 'Commit SHA to tag (leave empty for current HEAD)' | |
| required: false | |
| type: string | |
| concurrency: | |
| group: trunk-tagging-${{ github.event.inputs.commit_sha || github.sha }} | |
| cancel-in-progress: false | |
| permissions: | |
| contents: write | |
| jobs: | |
| tag-trunk-commit: | |
| name: Tag trunk commit | |
| runs-on: ubuntu-latest | |
| if: github.repository_owner == 'pytorch' | |
| steps: | |
| - name: Pre-checkout validation | |
| run: | | |
| # For workflow_dispatch, validate SHA format before checkout | |
| if [ -n "${{ github.event.inputs.commit_sha }}" ]; then | |
| COMMIT_SHA="${{ github.event.inputs.commit_sha }}" | |
| # Verify it's a well-formed SHA (40 hex characters) | |
| if ! echo "${COMMIT_SHA}" | grep -qE '^[a-f0-9]{40}$'; then | |
| echo "Error: Invalid commit SHA format. Expected 40 hexadecimal characters, got: ${COMMIT_SHA}" | |
| exit 1 | |
| fi | |
| echo "✅ Pre-checkout validation passed for: ${COMMIT_SHA}" | |
| else | |
| echo "✅ Using current commit SHA - no pre-checkout validation needed" | |
| fi | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| # Fetch full history to ensure we have all commits | |
| fetch-depth: 0 | |
| # For workflow_dispatch, checkout the specified commit | |
| ref: ${{ github.event.inputs.commit_sha || github.sha }} | |
| - name: Set commit SHA | |
| id: commit | |
| run: | | |
| if [ -n "${{ github.event.inputs.commit_sha }}" ]; then | |
| COMMIT_SHA="${{ github.event.inputs.commit_sha }}" | |
| else | |
| COMMIT_SHA="${{ github.sha }}" | |
| fi | |
| { | |
| echo "sha=${COMMIT_SHA}" | |
| echo "tag_name=trunk/${COMMIT_SHA}" | |
| } >> "${GITHUB_OUTPUT}" | |
| - name: Validate commit SHA | |
| run: | | |
| COMMIT_SHA="${{ steps.commit.outputs.sha }}" | |
| # Verify the commit exists and is valid | |
| if ! git cat-file -e "${COMMIT_SHA}"; then | |
| echo "Error: Commit SHA ${COMMIT_SHA} does not exist in repository" | |
| exit 1 | |
| fi | |
| # For workflow_dispatch, verify the commit exists on main branch | |
| if [ -n "${{ github.event.inputs.commit_sha }}" ]; then | |
| echo "Manual dispatch detected - validating commit is on main branch..." | |
| # Get all commits reachable from main branch | |
| if ! git merge-base --is-ancestor "${COMMIT_SHA}" origin/main; then | |
| echo "Error: Commit ${COMMIT_SHA} is not reachable from main branch" | |
| echo "Only commits that exist on the main branch can be tagged" | |
| exit 1 | |
| fi | |
| echo "✅ Commit ${COMMIT_SHA} is valid and exists on main branch" | |
| else | |
| echo "✅ Commit ${COMMIT_SHA} is valid (automatic push trigger)" | |
| fi | |
| - name: Create and push tag(s) with retry | |
| id: check_tag | |
| env: | |
| TAG_NAME: ${{ steps.commit.outputs.tag_name }} | |
| COMMIT_SHA: ${{ steps.commit.outputs.sha }} | |
| run: | | |
| set -e | |
| # Check if tag already exists | |
| check_tag_exists() { | |
| # Check if tag exists locally | |
| if git tag -l "${TAG_NAME}" | grep -q "${TAG_NAME}"; then | |
| echo "Tag ${TAG_NAME} already exists locally" | |
| return 0 | |
| fi | |
| # Check if tag exists on remote | |
| if git ls-remote --tags origin "${TAG_NAME}" | grep -q "${TAG_NAME}"; then | |
| echo "Tag ${TAG_NAME} already exists on remote" | |
| return 0 | |
| fi | |
| return 1 | |
| } | |
| # Counters for summary reporting | |
| created_count=0 | |
| skipped_count=0 | |
| failed_count=0 | |
| # Always write outputs once on exit | |
| finish() { | |
| set +e | |
| if [ -n "${GITHUB_OUTPUT:-}" ]; then | |
| { | |
| echo "created_count=${created_count}" | |
| echo "skipped_count=${skipped_count}" | |
| echo "failed_count=${failed_count}" | |
| } >> "${GITHUB_OUTPUT}" | |
| fi | |
| } | |
| trap finish EXIT | |
| # Retry configuration | |
| MAX_RETRIES=5 | |
| BASE_DELAY=2 | |
| BACKOFF_MULTIPLIER=4 | |
| MAX_DELAY=3600 | |
| # Common retry function with exponential backoff | |
| retry_with_backoff() { | |
| local command="${1}" | |
| local description="${2}" | |
| local retry_count=0 | |
| while [ "${retry_count}" -le "${MAX_RETRIES}" ]; do | |
| echo "Attempt $((retry_count + 1))/$((MAX_RETRIES + 1)): ${description}" | |
| if eval "${command}"; then | |
| echo "Success on attempt $((retry_count + 1))" | |
| return 0 | |
| fi | |
| retry_count=$((retry_count + 1)) | |
| if [ "${retry_count}" -le "${MAX_RETRIES}" ]; then | |
| # Calculate delay with exponential backoff | |
| local delay=$((BASE_DELAY * (BACKOFF_MULTIPLIER ** retry_count))) | |
| if [ "${delay}" -gt "${MAX_DELAY}" ]; then | |
| delay="${MAX_DELAY}" | |
| fi | |
| echo "Failed. Retrying in ${delay} seconds..." | |
| sleep "${delay}" | |
| fi | |
| done | |
| echo "All retry attempts exhausted" | |
| return 1 | |
| } | |
| # Function to create and push tag | |
| create_and_push_tag() { | |
| # Create the tag | |
| if ! git tag "${TAG_NAME}" "${COMMIT_SHA}"; then | |
| echo "Failed to create local tag" | |
| return 1 | |
| fi | |
| # Push the tag | |
| if git push origin "${TAG_NAME}"; then | |
| echo "Successfully created and pushed tag ${TAG_NAME}" | |
| return 0 | |
| else | |
| echo "Failed to push tag to remote" | |
| # Clean up local tag for retry | |
| git tag -d "${TAG_NAME}" 2>/dev/null || true | |
| return 1 | |
| fi | |
| } | |
| # Function to handle retries with race condition checks | |
| tag_with_retry() { | |
| # Check if tag exists before attempting creation | |
| if check_tag_exists; then | |
| echo "Tag ${TAG_NAME} was created by another process, exiting successfully" | |
| return 0 | |
| fi | |
| create_and_push_tag || { | |
| # Fetch latest state for next retry | |
| git fetch origin --tags | |
| return 1 | |
| } | |
| } | |
| # New behavior for push events: enumerate commits in the push and tag each one. | |
| # For workflow_dispatch, retain existing single-SHA behavior. | |
| # Always fetch tags once up front to improve idempotency in loops | |
| git fetch origin --tags --quiet || true | |
| if [ "${{ github.event_name }}" = "push" ]; then | |
| BEFORE_SHA="${{ github.event.before }}" | |
| AFTER_SHA="${{ github.sha }}" # same as event.after | |
| # List commits introduced by this push (old..new), oldest first for stable ordering | |
| commits_file="$(mktemp)" | |
| git rev-list --reverse "${BEFORE_SHA}..${AFTER_SHA}" > "${commits_file}" | |
| if [ ! -s "${commits_file}" ]; then | |
| echo "No new commits found between ${BEFORE_SHA}..${AFTER_SHA}; nothing to tag." | |
| rm -f "${commits_file}" | |
| exit 0 | |
| fi | |
| commit_count="$(wc -l < "${commits_file}" | tr -d ' ')" | |
| echo "Found ${commit_count} commit(s) to tag for push:" | |
| while IFS= read -r sha; do | |
| printf ' %s\n' "${sha}" | |
| done < "${commits_file}" | |
| while IFS= read -r sha; do | |
| TAG_NAME="trunk/${sha}" | |
| COMMIT_SHA="${sha}" | |
| # If tag already exists locally or remotely, skip (idempotent) | |
| if check_tag_exists; then | |
| echo "✅ Tag ${TAG_NAME} already exists - skipping" | |
| skipped_count=$((skipped_count + 1)) | |
| continue | |
| fi | |
| echo "Tag ${TAG_NAME} does not exist, proceeding with creation" | |
| if retry_with_backoff "tag_with_retry" "Creating tag ${TAG_NAME} for commit ${COMMIT_SHA}"; then | |
| created_count=$((created_count + 1)) | |
| else | |
| echo "Tag creation failed after all retry attempts for ${TAG_NAME}" | |
| failed_count=$((failed_count + 1)) | |
| fi | |
| done < "${commits_file}" | |
| rm -f "${commits_file}" | |
| if [ "${failed_count}" -gt 0 ]; then | |
| exit 1 | |
| fi | |
| exit 0 | |
| else | |
| # workflow_dispatch path (single SHA tagging preserved) | |
| # Exit early if tag already exists | |
| if check_tag_exists; then | |
| echo "✅ Tag already exists - no action needed" | |
| skipped_count=1 | |
| exit 0 | |
| fi | |
| echo "Tag ${TAG_NAME} does not exist, proceeding with creation" | |
| if retry_with_backoff "tag_with_retry" "Creating tag ${TAG_NAME} for commit ${COMMIT_SHA}"; then | |
| created_count=1 | |
| exit 0 | |
| else | |
| echo "Tag creation failed after all retry attempts" | |
| failed_count=1 | |
| exit 1 | |
| fi | |
| fi | |
| - name: Tag creation summary | |
| if: always() | |
| run: | | |
| if [ "${{ github.event_name }}" = "push" ]; then | |
| echo "Trigger: push on main" | |
| echo "Created: ${{ steps.check_tag.outputs.created_count }}" | |
| echo "Skipped (already existed): ${{ steps.check_tag.outputs.skipped_count }}" | |
| echo "Failed: ${{ steps.check_tag.outputs.failed_count }}" | |
| if [ "${{ steps.check_tag.outputs.failed_count }}" = "0" ]; then | |
| echo "✅ Completed tagging for push range ${{ github.event.before }}..${{ github.sha }}" | |
| else | |
| echo "❌ Some tags failed to create for push range ${{ github.event.before }}..${{ github.sha }}" | |
| fi | |
| else | |
| if [ "${{ steps.check_tag.outputs.failed_count }}" = "0" ]; then | |
| if [ "${{ steps.check_tag.outputs.created_count }}" = "0" ]; then | |
| echo "✅ Tag ${{ steps.commit.outputs.tag_name }} already existed - no action needed" | |
| else | |
| echo "✅ Successfully created tag ${{ steps.commit.outputs.tag_name }} for commit ${{ steps.commit.outputs.sha }}" | |
| fi | |
| else | |
| echo "❌ Failed to create tag ${{ steps.commit.outputs.tag_name }} for commit ${{ steps.commit.outputs.sha }}" | |
| fi | |
| echo "" | |
| echo "Tag details:" | |
| echo " Name: ${{ steps.commit.outputs.tag_name }}" | |
| echo " Commit: ${{ steps.commit.outputs.sha }}" | |
| echo " Trigger: ${{ github.event_name }}" | |
| if [ -n "${{ github.event.inputs.commit_sha }}" ]; then | |
| echo " Manual commit: ${{ github.event.inputs.commit_sha }}" | |
| fi | |
| fi |