|
1 | 1 | #!/bin/bash |
| 2 | +# Import actual Linux snapshots from one GitHub Actions run. |
| 3 | +set -euo pipefail |
2 | 4 |
|
3 | | -# Script to update Linux snapshots from CI artifacts |
4 | | -# Usage: ./scripts/update-snapshots-from-ci.sh <run-id> |
5 | | - |
6 | | -set -e |
7 | | - |
8 | | -if [ -z "$1" ]; then |
9 | | - echo "❌ Error: Please provide a GitHub Actions run ID" |
10 | | - echo "Usage: $0 <run-id>" |
11 | | - echo "" |
12 | | - echo "Example: $0 18528005182" |
13 | | - echo "" |
14 | | - echo "Find the run ID in the GitHub Actions URL:" |
15 | | - echo "https://github.com/gre/react-native-view-shot/actions/runs/<RUN_ID>" |
| 5 | +if [ "$#" -ne 1 ] || [[ ! $1 =~ ^[0-9]+$ ]]; then |
| 6 | + echo "Usage: $0 <numeric-run-id>" >&2 |
16 | 7 | exit 1 |
17 | 8 | fi |
18 | 9 |
|
19 | 10 | RUN_ID=$1 |
20 | 11 | ARTIFACT_NAME="web-snapshots-actual" |
21 | 12 | SNAPSHOT_DIR="e2e/snapshots/reference/viewshot.spec.ts-snapshots" |
| 13 | +DOWNLOAD_DIR=$(mktemp -d "${TMPDIR:-/tmp}/viewshot-snapshots.XXXXXX") |
| 14 | +trap 'rm -rf -- "$DOWNLOAD_DIR"' EXIT |
22 | 15 |
|
23 | | -echo "🔍 Fetching artifacts from run $RUN_ID..." |
24 | | - |
25 | | -# Download the artifact |
26 | 16 | cd "$(dirname "$0")/.." |
27 | | -gh run download "$RUN_ID" -n "$ARTIFACT_NAME" -D /tmp/playwright-snapshots || { |
28 | | - echo "❌ Failed to download artifact '$ARTIFACT_NAME'" |
29 | | - echo "" |
30 | | - echo "💡 Make sure:" |
31 | | - echo " 1. The run ID is correct" |
32 | | - echo " 2. The artifact exists (may take a few minutes after test completion)" |
33 | | - echo " 3. You have GitHub CLI (gh) installed and authenticated" |
34 | | - exit 1 |
35 | | -} |
36 | | - |
37 | | -echo "✅ Artifact downloaded to /tmp/playwright-snapshots" |
38 | | - |
39 | | -# Create snapshot directory if it doesn't exist |
40 | | -mkdir -p "$SNAPSHOT_DIR" |
41 | | - |
42 | | -# Copy actual snapshots to reference directory |
43 | | -if [ -d "/tmp/playwright-snapshots" ]; then |
44 | | - echo "📂 Copying snapshots to $SNAPSHOT_DIR..." |
45 | | - |
46 | | - # Find all PNG files and copy them |
47 | | - find /tmp/playwright-snapshots -name "*.png" | while read -r file; do |
48 | | - filename=$(basename "$file") |
49 | | - |
50 | | - # If it's an -actual.png file, rename it to the standard name |
51 | | - if [[ $filename == *"-actual.png" ]]; then |
52 | | - newname="${filename%-actual.png}.png" |
53 | | - echo " → $newname" |
54 | | - cp "$file" "$SNAPSHOT_DIR/$newname" |
55 | | - else |
56 | | - echo " → $filename" |
57 | | - cp "$file" "$SNAPSHOT_DIR/$filename" |
58 | | - fi |
59 | | - done |
60 | | - |
61 | | - echo "" |
62 | | - echo "✅ Snapshots updated successfully!" |
63 | | - echo "" |
64 | | - echo "📋 Next steps:" |
65 | | - echo " 1. Review the changes: git diff $SNAPSHOT_DIR" |
66 | | - echo " 2. Commit the new snapshots: git add $SNAPSHOT_DIR && git commit -m 'Update Linux snapshots from CI'" |
67 | | - echo " 3. Push: git push" |
68 | | - |
69 | | - # Clean up |
70 | | - rm -rf /tmp/playwright-snapshots |
71 | | -else |
72 | | - echo "❌ No snapshots found in artifact" |
| 17 | +echo "Fetching $ARTIFACT_NAME from run $RUN_ID..." |
| 18 | +gh run download "$RUN_ID" -n "$ARTIFACT_NAME" -D "$DOWNLOAD_DIR/artifact" |
| 19 | + |
| 20 | +# Validate all destinations before changing any checked-in reference image. |
| 21 | +mkdir "$DOWNLOAD_DIR/staged" |
| 22 | +count=0 |
| 23 | +while IFS= read -r -d '' file; do |
| 24 | + filename=${file##*/} |
| 25 | + destination="$DOWNLOAD_DIR/staged/${filename%-actual.png}.png" |
| 26 | + if [ -e "$destination" ]; then |
| 27 | + echo "Duplicate snapshot destination: ${filename%-actual.png}.png" >&2 |
| 28 | + exit 1 |
| 29 | + fi |
| 30 | + cp "$file" "$destination" |
| 31 | + count=$((count + 1)) |
| 32 | +done < <(find "$DOWNLOAD_DIR/artifact" -type f -name '*-actual.png' -print0) |
| 33 | + |
| 34 | +if [ "$count" -eq 0 ]; then |
| 35 | + echo "No actual snapshots found in artifact." >&2 |
73 | 36 | exit 1 |
74 | 37 | fi |
75 | 38 |
|
| 39 | +mkdir -p "$SNAPSHOT_DIR" |
| 40 | +cp "$DOWNLOAD_DIR/staged/"*.png "$SNAPSHOT_DIR/" |
| 41 | +echo "Updated $count snapshots. Review git diff -- $SNAPSHOT_DIR before committing." |
0 commit comments