|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Handle Ctrl+C |
| 5 | +trap 'echo -e "\nInterrupted. Exiting..."; exit 130' INT TERM |
| 6 | + |
| 7 | +# Docker images to check for each semver tag |
| 8 | +DOCKER_IMAGES=( |
| 9 | + activemq |
| 10 | + alpaca |
| 11 | + blazegraph |
| 12 | + cantaloupe |
| 13 | + crayfish |
| 14 | + crayfits |
| 15 | + drupal |
| 16 | + fcrepo6 |
| 17 | + fits |
| 18 | + handle |
| 19 | + homarus |
| 20 | + houdini |
| 21 | + hypercube |
| 22 | + imagemagick |
| 23 | + java |
| 24 | + mariadb |
| 25 | + milliner |
| 26 | + nginx |
| 27 | + postgresql |
| 28 | + riprap |
| 29 | + solr |
| 30 | + tomcat |
| 31 | +) |
| 32 | + |
| 33 | +DOCKER_REPOSITORY="${DOCKER_REPOSITORY:-islandora}" |
| 34 | +WORKFLOW_FILE="${WORKFLOW_FILE:-push.yml}" |
| 35 | +WORKFLOW_REF="${WORKFLOW_REF:-main}" |
| 36 | +LOG_FILE="${LOG_FILE:-backfill-tags.log}" |
| 37 | +DRY_RUN=true |
| 38 | +SINGLE_TAG="" |
| 39 | + |
| 40 | +# Parse arguments |
| 41 | +while [[ $# -gt 0 ]]; do |
| 42 | + case $1 in |
| 43 | + --yolo) |
| 44 | + DRY_RUN=false |
| 45 | + shift |
| 46 | + ;; |
| 47 | + --tag) |
| 48 | + SINGLE_TAG="$2" |
| 49 | + shift 2 |
| 50 | + ;; |
| 51 | + *) |
| 52 | + shift |
| 53 | + ;; |
| 54 | + esac |
| 55 | +done |
| 56 | + |
| 57 | +# Get all semver tags from the repo (3.x and above), sorted |
| 58 | +get_semver_tags() { |
| 59 | + git tag --list | grep -E '^v?[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | while read -r tag; do |
| 60 | + # Extract major version (strip leading 'v' if present) |
| 61 | + local version="${tag#v}" |
| 62 | + local major="${version%%.*}" |
| 63 | + if [[ "$major" -ge 3 ]]; then |
| 64 | + echo "$tag" |
| 65 | + fi |
| 66 | + done |
| 67 | +} |
| 68 | + |
| 69 | +# Check if a Docker image exists |
| 70 | +image_exists() { |
| 71 | + local image="$1" |
| 72 | + local tag="$2" |
| 73 | + local full_image="${DOCKER_REPOSITORY}/${image}:${tag}" |
| 74 | + |
| 75 | + # Use docker manifest inspect to check if image exists (doesn't pull the image) |
| 76 | + if docker manifest inspect "$full_image" &>/dev/null; then |
| 77 | + return 0 |
| 78 | + else |
| 79 | + return 1 |
| 80 | + fi |
| 81 | +} |
| 82 | + |
| 83 | +# Log a workflow run |
| 84 | +log_workflow() { |
| 85 | + local tag="$1" |
| 86 | + local run_id="$2" |
| 87 | + local url="$3" |
| 88 | + local timestamp |
| 89 | + timestamp=$(date -Iseconds) |
| 90 | + |
| 91 | + echo "${timestamp} | ${tag} | ${run_id} | ${url}" >> "$LOG_FILE" |
| 92 | + echo " Logged to $LOG_FILE: $url" |
| 93 | +} |
| 94 | + |
| 95 | +# Run the workflow for a given tag |
| 96 | +run_workflow() { |
| 97 | + local tag="$1" |
| 98 | + |
| 99 | + echo "Running workflow for tag: $tag" |
| 100 | + |
| 101 | + if [[ "$DRY_RUN" == "true" ]]; then |
| 102 | + echo "[DRY-RUN] Would run: gh workflow run $WORKFLOW_FILE --ref $WORKFLOW_REF -f tag=$tag -f strip-apk-pinning=true" |
| 103 | + return 0 |
| 104 | + fi |
| 105 | + |
| 106 | + # Run the workflow and wait for completion |
| 107 | + gh workflow run "$WORKFLOW_FILE" \ |
| 108 | + --ref "$WORKFLOW_REF" \ |
| 109 | + -f "tag=$tag" \ |
| 110 | + -f "strip-apk-pinning=true" |
| 111 | + |
| 112 | + # Give GitHub a moment to register the run |
| 113 | + sleep 5 |
| 114 | + |
| 115 | + # Get the run ID and URL of the workflow we just triggered |
| 116 | + local run_info run_id run_url |
| 117 | + run_info=$(gh run list --workflow="$WORKFLOW_FILE" --limit=1 --json databaseId,url --jq '.[0] | "\(.databaseId) \(.url)"') |
| 118 | + run_id=$(echo "$run_info" | cut -d' ' -f1) |
| 119 | + run_url=$(echo "$run_info" | cut -d' ' -f2) |
| 120 | + |
| 121 | + # Log the tag and workflow URL |
| 122 | + log_workflow "$tag" "$run_id" "$run_url" |
| 123 | + |
| 124 | + echo "Waiting for workflow run $run_id to complete..." |
| 125 | + while true; do |
| 126 | + status=$(gh run view "$run_id" --json status,conclusion --jq '.status') |
| 127 | + if [[ "$status" == "completed" ]]; then |
| 128 | + conclusion=$(gh run view "$run_id" --json conclusion --jq '.conclusion') |
| 129 | + if [[ "$conclusion" != "success" ]]; then |
| 130 | + echo " Workflow failed with conclusion: $conclusion" |
| 131 | + exit 1 |
| 132 | + fi |
| 133 | + break |
| 134 | + fi |
| 135 | + printf "." |
| 136 | + sleep 30 |
| 137 | + done |
| 138 | + echo "" |
| 139 | + |
| 140 | + echo "Workflow completed for tag: $tag" |
| 141 | +} |
| 142 | + |
| 143 | +# Main logic |
| 144 | +main() { |
| 145 | + if [[ "$DRY_RUN" == "true" ]]; then |
| 146 | + echo "=== DRY-RUN MODE (pass --yolo to execute) ===" |
| 147 | + echo "" |
| 148 | + fi |
| 149 | + |
| 150 | + local tags |
| 151 | + if [[ -n "$SINGLE_TAG" ]]; then |
| 152 | + tags="$SINGLE_TAG" |
| 153 | + echo "Processing single tag: $tags" |
| 154 | + else |
| 155 | + tags=$(get_semver_tags) |
| 156 | + |
| 157 | + if [[ -z "$tags" ]]; then |
| 158 | + echo "No semver tags >= 3.0.0 found in repository" |
| 159 | + exit 0 |
| 160 | + fi |
| 161 | + |
| 162 | + echo "Found semver tags (>= 3.0.0):" |
| 163 | + echo "$tags" |
| 164 | + fi |
| 165 | + echo "" |
| 166 | + |
| 167 | + while IFS= read -r tag; do |
| 168 | + echo "Checking tag: $tag" |
| 169 | + |
| 170 | + local missing_images=() |
| 171 | + |
| 172 | + for image in "${DOCKER_IMAGES[@]}"; do |
| 173 | + if ! image_exists "$image" "$tag"; then |
| 174 | + missing_images+=("$image") |
| 175 | + fi |
| 176 | + done |
| 177 | + |
| 178 | + if [[ ${#missing_images[@]} -gt 0 ]]; then |
| 179 | + echo " Missing images for $tag: ${missing_images[*]}" |
| 180 | + run_workflow "$tag" |
| 181 | + else |
| 182 | + echo " All images exist for $tag, skipping" |
| 183 | + fi |
| 184 | + |
| 185 | + echo "" |
| 186 | + done <<< "$tags" |
| 187 | + |
| 188 | + echo "Done!" |
| 189 | +} |
| 190 | + |
| 191 | +main |
0 commit comments