-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathopendatahub-io-ci-image-mirror-commands.sh
executable file
·131 lines (115 loc) · 4.04 KB
/
opendatahub-io-ci-image-mirror-commands.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
export HOME=/tmp/home
export XDG_RUNTIME_DIR="${HOME}/run"
export REGISTRY_AUTH_PREFERENCE=podman # TODO: remove later, used for migrating oc from docker to podman
mkdir -p "${XDG_RUNTIME_DIR}/containers"
cd "$HOME" || exit 1
# log function
log_file="${ARTIFACT_DIR}/mirror.log"
log() {
local ts
ts=$(date --iso-8601=seconds)
echo "$ts" "$@" | tee -a "$log_file"
}
# Get current date
current_date=$(date +%F)
log "INFO Current date is $current_date"
# Get RELEASE_VERSION
log "INFO Z-stream version is $RELEASE_VERSION"
# Get IMAGE_REPO
log "INFO Image repo is $IMAGE_REPO"
# Get IMAGE_TAG if not provided
if [[ -z "$IMAGE_TAG" ]]; then
case "$JOB_TYPE" in
presubmit)
log "INFO Building default image tag for a $JOB_TYPE job"
IMAGE_TAG="pr-${PULL_NUMBER}"
if [[ -n "${RELEASE_VERSION-}" ]]; then
IMAGE_TAG="${RELEASE_VERSION}-${IMAGE_TAG}"
fi
;;
postsubmit)
log "INFO Building default image tag for a $JOB_TYPE job"
IMAGE_TAG="${RELEASE_VERSION}-${PULL_BASE_SHA:0:7}"
IMAGE_FLOATING_TAG="${RELEASE_VERSION}"
;;
periodic)
log "INFO Building default image tag for a $JOB_TYPE job"
IMAGE_TAG="${RELEASE_VERSION}-nightly-${current_date}"
;;
*)
log "ERROR Cannot publish an image from a $JOB_TYPE job"
exit 1
;;
esac
fi
# Get IMAGE_TAG if it's equal to YearIndex in YYYYMMDD format
if [[ "$IMAGE_TAG" == "YearIndex" ]]; then
YEAR_INDEX=$(echo "$(date +%Y%m%d)")
case "$JOB_TYPE" in
presubmit)
log "INFO Building YearIndex image tag for a $JOB_TYPE job"
IMAGE_TAG="pr-${PULL_NUMBER}"
if [[ -n "${RELEASE_VERSION-}" ]]; then
IMAGE_TAG="${RELEASE_VERSION}-${IMAGE_TAG}"
fi
;;
postsubmit)
log "INFO Building YearIndex image tag for a $JOB_TYPE job"
IMAGE_TAG="${RELEASE_VERSION}-${YEAR_INDEX}-${PULL_BASE_SHA:0:7}"
IMAGE_FLOATING_TAG="${RELEASE_VERSION}-${YEAR_INDEX}"
;;
periodic)
log "INFO Building weekly image tag for a $JOB_TYPE job"
IMAGE_TAG="${RELEASE_VERSION}-weekly"
;;
*)
log "ERROR Cannot publish an image from a $JOB_TYPE job"
exit 1
;;
esac
fi
log "INFO Image tag is $IMAGE_TAG"
# Setup registry credentials
REGISTRY_TOKEN_FILE="$SECRETS_PATH/$REGISTRY_SECRET/$REGISTRY_SECRET_FILE"
if [[ ! -r "$REGISTRY_TOKEN_FILE" ]]; then
log "ERROR Registry secret file not found: $REGISTRY_TOKEN_FILE"
exit 1
fi
config_file="${XDG_RUNTIME_DIR}/containers/auth.json"
cp $REGISTRY_TOKEN_FILE $config_file || {
log "ERROR Could not create registry secret file"
log " From: $REGISTRY_TOKEN_FILE"
log " To : $config_file"
exit 1
}
# Allow to pull images from internal Openshift CI registry
oc registry login || {
log "ERROR Unable to login to internal Openshift CI registry"
exit 1
}
# Check if running in openshift/release only in presubmit jobs because
# REPO_OWNER and REPO_NAME are not available for other types
dry=false
if [[ "$JOB_TYPE" == "presubmit" ]]; then
if [[ "$REPO_OWNER" == "openshift" && "$REPO_NAME" == "release" ]]; then
log "INFO Running in openshift/release, setting dry-run to true"
dry=true
fi
fi
# Build destination image reference
DESTINATION_REGISTRY_REPO="$REGISTRY_HOST/$REGISTRY_ORG/$IMAGE_REPO"
DESTINATION_IMAGE_REF="$DESTINATION_REGISTRY_REPO:$IMAGE_TAG"
if [[ -n "${IMAGE_FLOATING_TAG-}" ]]; then
FLOATING_IMAGE_REF="$DESTINATION_REGISTRY_REPO:$IMAGE_FLOATING_TAG"
DESTINATION_IMAGE_REF="$DESTINATION_IMAGE_REF $FLOATING_IMAGE_REF"
fi
log "INFO Mirroring Image"
log " From : $SOURCE_IMAGE_REF"
log " To : $DESTINATION_IMAGE_REF"
log " Dry Run: $dry"
oc image mirror $SOURCE_IMAGE_REF $DESTINATION_IMAGE_REF --dry-run=$dry || {
log "ERROR Unable to mirror image"
exit 1
}
log "INFO Mirroring complete."