|
| 1 | +apiVersion: tekton.dev/v1 |
| 2 | +kind: Task |
| 3 | +metadata: |
| 4 | + name: push-snapshot |
| 5 | +spec: |
| 6 | + description: Push images referenced by the provided Snapshot to another registry |
| 7 | + params: |
| 8 | + - name: SNAPSHOT |
| 9 | + description: Information about the Snapshot to be pushed |
| 10 | + - name: event-type |
| 11 | + description: The type of event that triggered the pipeline |
| 12 | + - name: sync |
| 13 | + description: >- |
| 14 | + True if all components are built from the same git commit. False if components |
| 15 | + are built from different commits. |
| 16 | + - name: repo-prefix |
| 17 | + description: >- |
| 18 | + The prefix (usually hostname/orgname) of the destination repo to push the Snapshot components to. |
| 19 | + The name of the component will be appended to this prefix and the result will be used as the |
| 20 | + destination repo to push the component to. |
| 21 | + - name: skip-components |
| 22 | + description: >- |
| 23 | + A space-separated list of components that should not be pushed |
| 24 | + default: "" |
| 25 | + results: |
| 26 | + - name: TEST_OUTPUT |
| 27 | + description: Test results in JSON format |
| 28 | + steps: |
| 29 | + - name: push-to-registry |
| 30 | + image: registry.redhat.io/rhel10/skopeo:latest |
| 31 | + env: |
| 32 | + - name: SNAPSHOT |
| 33 | + value: $(params.SNAPSHOT) |
| 34 | + - name: EVENT_TYPE |
| 35 | + value: $(params.event-type) |
| 36 | + - name: SYNC |
| 37 | + value: $(params.sync) |
| 38 | + - name: REPO_PREFIX |
| 39 | + value: $(params.repo-prefix) |
| 40 | + - name: SKIP_COMPONENTS |
| 41 | + value: $(params.skip-components) |
| 42 | + - name: RESULTS_TEST_OUTPUT_PATH |
| 43 | + value: $(results.TEST_OUTPUT.path) |
| 44 | + - name: NAMESPACE |
| 45 | + value: $(context.taskRun.namespace) |
| 46 | + script: | |
| 47 | + #!/bin/bash -x |
| 48 | + dnf -y install jq |
| 49 | +
|
| 50 | + log() { |
| 51 | + echo "[$(date -uIns)]" $* |
| 52 | + } |
| 53 | +
|
| 54 | + in_args() { |
| 55 | + local item="$1" |
| 56 | + shift |
| 57 | + while [ "$#" -gt 0 ]; do |
| 58 | + if [ "$item" == "$1" ]; then |
| 59 | + return 0 |
| 60 | + fi |
| 61 | + shift |
| 62 | + done |
| 63 | + return 1 |
| 64 | + } |
| 65 | +
|
| 66 | + test_output() { |
| 67 | + local RESULT="$1" |
| 68 | + local SUCCESSES="$2" |
| 69 | + local FAILURES="$3" |
| 70 | + local WARNINGS="$4" |
| 71 | + local NOTE="$5" |
| 72 | +
|
| 73 | + jq -jnc \ |
| 74 | + --arg result "$RESULT" \ |
| 75 | + --arg note "$NOTE" \ |
| 76 | + --arg namespace "$NAMESPACE" \ |
| 77 | + --arg successes "$SUCCESSES" \ |
| 78 | + --arg failures "$FAILURES" \ |
| 79 | + --arg warnings "$WARNINGS" \ |
| 80 | + '{ |
| 81 | + result: $result, |
| 82 | + timestamp: now | todateiso8601[:-1] | "\(.)+00:00", |
| 83 | + note: $note, |
| 84 | + namespace: $namespace, |
| 85 | + successes: $successes | tonumber, |
| 86 | + failures: $failures | tonumber, |
| 87 | + warnings: $warnings | tonumber |
| 88 | + }' | tee "$RESULTS_TEST_OUTPUT_PATH" |
| 89 | + } |
| 90 | +
|
| 91 | + COMPONENTS="$(jq -r '.components | length' <<< "$SNAPSHOT")" |
| 92 | + SKIP=($SKIP_COMPONENTS) |
| 93 | +
|
| 94 | + if [ "$EVENT_TYPE" != "push" ]; then |
| 95 | + NOTE="Skipped push of $COMPONENTS components from \"$EVENT_TYPE\" event" |
| 96 | + test_output SKIPPED 0 0 0 "$NOTE" |
| 97 | + exit |
| 98 | + elif [ "$SYNC" != "true" ]; then |
| 99 | + NOTE="Skipped push of $COMPONENTS components due to out-of-sync components" |
| 100 | + test_output SKIPPED 0 0 0 "$NOTE" |
| 101 | + exit |
| 102 | + fi |
| 103 | +
|
| 104 | + SUCCESSES=0 |
| 105 | + SKIPS=0 |
| 106 | + FAILED_COMPONENTS=() |
| 107 | + while read NAME SRC DEST; do |
| 108 | + if in_args "$NAME" "${SKIP[@]}"; then |
| 109 | + log Skipping push of "$NAME" |
| 110 | + (( SKIPS+=1 )) |
| 111 | + continue |
| 112 | + fi |
| 113 | + log Copying "$NAME" from "$SRC" to "$DEST" |
| 114 | + if skopeo copy --all --multi-arch all --retry-times 5 "docker://$SRC" "docker://$DEST"; then |
| 115 | + (( SUCCESSES+=1 )) |
| 116 | + else |
| 117 | + FAILED_COMPONENTS+=("$NAME") |
| 118 | + fi |
| 119 | + done < <( |
| 120 | + jq -r --arg prefix "$REPO_PREFIX" \ |
| 121 | + '.components[] | "\(.name) \(.containerImage) \($prefix)/\(.name):\(.source.git.revision)"' \ |
| 122 | + <<< "$SNAPSHOT" |
| 123 | + ) |
| 124 | +
|
| 125 | + if [ "${#FAILED_COMPONENTS[*]}" -gt 0 ]; then |
| 126 | + NOTE="$SUCCESSES components successfully pushed, $SKIPS components skipped, ${#FAILED_COMPONENTS[*]} failed to push: ${FAILED_COMPONENTS[*]}" |
| 127 | + test_output FAILURE "$SUCCESSES" "${#FAILED_COMPONENTS[*]}" 0 "$NOTE" |
| 128 | + exit 1 |
| 129 | + else |
| 130 | + NOTE="$SUCCESSES components successfully pushed, $SKIPS components skipped" |
| 131 | + test_output SUCCESS "$SUCCESSES" 0 0 "$NOTE" |
| 132 | + fi |
0 commit comments