Skip to content

Commit

Permalink
refactor: use built-in buildah --retry
Browse files Browse the repository at this point in the history
- Retries were achieved by writing a loop around the buildah command
- buildah actually has a `--retry` option
- Use the built-in option in order to simplify the code
  • Loading branch information
tnevrlka committed Dec 6, 2024
1 parent b670a9e commit 48f8f4b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 68 deletions.
26 changes: 9 additions & 17 deletions task/buildah-oci-ta/0.2/buildah-oci-ta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -559,41 +559,33 @@ spec:
#!/bin/bash
set -e
retry() {
status=-1
max_run=5
sleep_sec=10
for run in $(seq 1 $max_run); do
status=0
[ "$run" -gt 1 ] && sleep $sleep_sec
"$@" && break || status=$?
done
return $status
}
ca_bundle=/mnt/trusted-ca/ca-bundle.crt
if [ -f "$ca_bundle" ]; then
echo "INFO: Using mounted CA bundle: $ca_bundle"
cp -vf $ca_bundle /etc/pki/ca-trust/source/anchors
update-ca-trust
fi
retries=5
# Push to a unique tag based on the TaskRun name to avoid race conditions
echo "Pushing to ${IMAGE%:*}:${TASKRUN_NAME}"
if ! retry buildah push \
if ! buildah push \
--retry "$retries" \
--tls-verify="$TLSVERIFY" \
"$IMAGE" \
"docker://${IMAGE%:*}:$(context.taskRun.name)"; then
echo "Failed to push sbom image to ${IMAGE%:*}:$(context.taskRun.name) after ${max_run} tries"
echo "Failed to push sbom image to ${IMAGE%:*}:$(context.taskRun.name) after ${retries} tries"
exit 1
fi
# Push to a tag based on the git revision
echo "Pushing to ${IMAGE}"
if ! retry buildah push \
if ! buildah push \
--retry "$retries" \
--tls-verify="$TLSVERIFY" \
--digestfile "/var/workdir/image-digest" "$IMAGE" \
"docker://$IMAGE"; then
echo "Failed to push sbom image to $IMAGE after ${max_run} tries"
echo "Failed to push sbom image to $IMAGE after ${retries} tries"
exit 1
fi
Expand Down
26 changes: 9 additions & 17 deletions task/buildah-remote-oci-ta/0.2/buildah-remote-oci-ta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -659,41 +659,33 @@ spec:
export IMAGE
fi
retry() {
status=-1
max_run=5
sleep_sec=10
for run in $(seq 1 $max_run); do
status=0
[ "$run" -gt 1 ] && sleep $sleep_sec
"$@" && break || status=$?
done
return $status
}
ca_bundle=/mnt/trusted-ca/ca-bundle.crt
if [ -f "$ca_bundle" ]; then
echo "INFO: Using mounted CA bundle: $ca_bundle"
cp -vf $ca_bundle /etc/pki/ca-trust/source/anchors
update-ca-trust
fi
retries=5
# Push to a unique tag based on the TaskRun name to avoid race conditions
echo "Pushing to ${IMAGE%:*}:${TASKRUN_NAME}"
if ! retry buildah push \
if ! buildah push \
--retry "$retries" \
--tls-verify="$TLSVERIFY" \
"$IMAGE" \
"docker://${IMAGE%:*}:$(context.taskRun.name)"; then
echo "Failed to push sbom image to ${IMAGE%:*}:$(context.taskRun.name) after ${max_run} tries"
echo "Failed to push sbom image to ${IMAGE%:*}:$(context.taskRun.name) after ${retries} tries"
exit 1
fi
# Push to a tag based on the git revision
echo "Pushing to ${IMAGE}"
if ! retry buildah push \
if ! buildah push \
--retry "$retries" \
--tls-verify="$TLSVERIFY" \
--digestfile "/var/workdir/image-digest" "$IMAGE" \
"docker://$IMAGE"; then
echo "Failed to push sbom image to $IMAGE after ${max_run} tries"
echo "Failed to push sbom image to $IMAGE after ${retries} tries"
exit 1
fi
Expand Down
26 changes: 9 additions & 17 deletions task/buildah-remote/0.2/buildah-remote.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -637,43 +637,35 @@ spec:
export IMAGE
fi
retry () {
status=-1
max_run=5
sleep_sec=10
for run in $(seq 1 $max_run); do
status=0
[ "$run" -gt 1 ] && sleep $sleep_sec
"$@" && break || status=$?
done
return $status
}
ca_bundle=/mnt/trusted-ca/ca-bundle.crt
if [ -f "$ca_bundle" ]; then
echo "INFO: Using mounted CA bundle: $ca_bundle"
cp -vf $ca_bundle /etc/pki/ca-trust/source/anchors
update-ca-trust
fi
retries=5
# Push to a unique tag based on the TaskRun name to avoid race conditions
echo "Pushing to ${IMAGE%:*}:${TASKRUN_NAME}"
if ! retry buildah push \
if ! buildah push \
--retry "$retries" \
--tls-verify="$TLSVERIFY" \
"$IMAGE" \
"docker://${IMAGE%:*}:$(context.taskRun.name)";
then
echo "Failed to push sbom image to ${IMAGE%:*}:$(context.taskRun.name) after ${max_run} tries"
echo "Failed to push sbom image to ${IMAGE%:*}:$(context.taskRun.name) after ${retries} tries"
exit 1
fi
# Push to a tag based on the git revision
echo "Pushing to ${IMAGE}"
if ! retry buildah push \
if ! buildah push \
--retry "$retries" \
--tls-verify="$TLSVERIFY" \
--digestfile "$(workspaces.source.path)/image-digest" "$IMAGE" \
"docker://$IMAGE";
then
echo "Failed to push sbom image to $IMAGE after ${max_run} tries"
echo "Failed to push sbom image to $IMAGE after ${retries} tries"
exit 1
fi
Expand Down
26 changes: 9 additions & 17 deletions task/buildah/0.2/buildah.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -500,43 +500,35 @@ spec:
#!/bin/bash
set -e
retry () {
status=-1
max_run=5
sleep_sec=10
for run in $(seq 1 $max_run); do
status=0
[ "$run" -gt 1 ] && sleep $sleep_sec
"$@" && break || status=$?
done
return $status
}
ca_bundle=/mnt/trusted-ca/ca-bundle.crt
if [ -f "$ca_bundle" ]; then
echo "INFO: Using mounted CA bundle: $ca_bundle"
cp -vf $ca_bundle /etc/pki/ca-trust/source/anchors
update-ca-trust
fi
retries=5
# Push to a unique tag based on the TaskRun name to avoid race conditions
echo "Pushing to ${IMAGE%:*}:${TASKRUN_NAME}"
if ! retry buildah push \
if ! buildah push \
--retry "$retries" \
--tls-verify="$TLSVERIFY" \
"$IMAGE" \
"docker://${IMAGE%:*}:$(context.taskRun.name)";
then
echo "Failed to push sbom image to ${IMAGE%:*}:$(context.taskRun.name) after ${max_run} tries"
echo "Failed to push sbom image to ${IMAGE%:*}:$(context.taskRun.name) after ${retries} tries"
exit 1
fi
# Push to a tag based on the git revision
echo "Pushing to ${IMAGE}"
if ! retry buildah push \
if ! buildah push \
--retry "$retries" \
--tls-verify="$TLSVERIFY" \
--digestfile "$(workspaces.source.path)/image-digest" "$IMAGE" \
"docker://$IMAGE";
then
echo "Failed to push sbom image to $IMAGE after ${max_run} tries"
echo "Failed to push sbom image to $IMAGE after ${retries} tries"
exit 1
fi
Expand Down

0 comments on commit 48f8f4b

Please sign in to comment.