Skip to content

Commit eaaf588

Browse files
committed
travis-ci: remove old branches, so things don't fill up
this will go through the files on the server, and remove old artifacts that aren't associated with an active branch or tag. Signed-off-by: Robin Getz <[email protected]>
1 parent f9abc28 commit eaaf588

File tree

2 files changed

+111
-22
lines changed

2 files changed

+111
-22
lines changed

CI/travis/deploy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ upload_file_to_swdownloads libiio ${RELEASE_PKG_FILE_DEB} ${TARGET_DEB} .deb
77
upload_file_to_swdownloads libiio ${RELEASE_PKG_FILE_RPM} ${TARGET_RPM} .rpm
88
upload_file_to_swdownloads libiio ${RELEASE_PKG_FILE_TGZ} ${TARGET_TGZ} .tar.gz
99
upload_file_to_swdownloads libiio ${RELEASE_PKG_FILE_PKG} ${TARGET_PKG} .pkg
10-
10+
remove_old_pkgs libiio

CI/travis/lib.sh

Lines changed: 110 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,27 @@ sftp_cmd_pipe() {
225225
sftp -o "StrictHostKeyChecking no" "${EXTRA_SSH}" "${SSHUSER}@${SSHHOST}"
226226
}
227227

228+
sftp_run_cmds() {
229+
local x=5
230+
while [ "${x}" -gt "0" ] ; do
231+
sftp -o "StrictHostKeyChecking no" "${EXTRA_SSH}" "${SSHUSER}@${SSHHOST}" 0< "$1" && break;
232+
echo_red "failed to ssh, trying again"
233+
x=$((x - 1))
234+
sleep 10
235+
done
236+
if [ "${x}" -eq "0" ] ; then
237+
echo_red "failed to upload files"
238+
return 1;
239+
fi
240+
return 0
241+
}
242+
228243
sftp_rm_artifact() {
229244
local artifact="$1"
230245
sftp_cmd_pipe <<-EOF
231246
cd ${DEPLOY_TO}
232247
rm ${artifact}
248+
bye
233249
EOF
234250
}
235251

@@ -297,33 +313,106 @@ upload_file_to_swdownloads() {
297313
echo and "${branch}_${LIBNAME}${LDIST}${EXT}"
298314
ssh -V
299315

300-
for rmf in ${TO} ${LATE} ; do
301-
sftp_rm_artifact "${rmf}" || \
302-
echo_blue "Could not delete ${rmf}"
303-
done
316+
local tmpfl=$(mktemp)
317+
echo "cd ${DEPLOY_TO}" > "${tmpfl}"
318+
echo "rm ${TO}" >> "${tmpfl}"
319+
echo "rm ${LATE}" >> "${tmpfl}"
320+
echo "put ${FROM} ${TO}" >> "${tmpfl}"
321+
echo "symlink ${TO} ${LATE}" >> "${tmpfl}"
322+
echo "ls -l ${TO}" >> "${tmpfl}"
323+
echo "ls -l ${LATE}" >> "${tmpfl}"
324+
echo "bye" >> "${tmpfl}"
304325

305-
sftp_upload "${FROM}" "${TO}" "${LATE}" || {
306-
echo_red "Failed to upload artifact from '${FROM}', to '${TO}', symlink '${LATE}'"
307-
return 1
308-
}
326+
sftp_run_cmds "${tmpfl}"
327+
rm "${tmpfl}"
328+
329+
return 0
330+
}
309331

332+
remove_old_pkgs() {
310333
# limit things to a few files, so things don't grow forever
311334
# we only do this on one build so simultaneous builds don't clobber each other
312-
if [ -n "${GH_DOC_TOKEN}" ] ; then
313-
for files in $(ssh -o "StrictHostKeyChecking no" "${EXTRA_SSH}" "${SSHUSER}@${SSHHOST}" \
335+
if [ -z "${GH_DOC_TOKEN}" ] ; then
336+
return 0
337+
fi
338+
339+
if [ -z "${TRAVIS_BUILD_DIR}" ] ; then
340+
echo "TRAVIS_BUILD_DIR not set"
341+
return 0
342+
fi
343+
344+
if [ ! -d "${TRAVIS_BUILD_DIR}/.git" ] ; then
345+
echo "No ${TRAVIS_BUILD_DIR}/.git to operate git on"
346+
return 0
347+
fi
348+
349+
local LIBNAME=$1
350+
local old=
351+
352+
echo "Remove old packages from ${LIBNAME}"
353+
354+
if [ -n "$TRAVIS_PULL_REQUEST_BRANCH" ] ; then
355+
local branch="$TRAVIS_PULL_REQUEST_BRANCH"
356+
else
357+
local branch="$TRAVIS_BRANCH"
358+
fi
359+
360+
local GLOB=${DEPLOY_TO}/${branch}_${LIBNAME}-*
361+
362+
# putting everything into a file, and connecting once decreases the chances
363+
# for ssh issues, connections happen once, not every single file
364+
local tmpfl=$(mktemp)
365+
echo "cd ${DEPLOY_TO}" > "${tmpfl}"
366+
for files in $(ssh -o "StrictHostKeyChecking no" "${EXTRA_SSH}" "${SSHUSER}@${SSHHOST}" \
314367
"ls -lt ${GLOB}" | tail -n +100 | awk '{print $NF}')
315-
do
316-
ssh -o "StrictHostKeyChecking no" "${EXTRA_SSH}" "${SSHUSER}@${SSHHOST}" \
317-
"rm ${DEPLOY_TO}/${files}" || \
318-
return 1
319-
done
320-
# provide an index so people can find files.
321-
ssh -o "StrictHostKeyChecking no" "${EXTRA_SSH}" "${SSHUSER}@${SSHHOST}" \
322-
"ls -lt ${DEPLOY_TO}" | grep ${LIBNAME} > ${LIBNAME}_index.html
323-
sftp_rm_artifact "${LIBNAME}_index.html" || \
324-
echo_blue "Could not delete ${LIBNAME}_index.html"
325-
sftp_upload "${LIBNAME}_index.html" "${LIBNAME}_index.html"
368+
do
369+
echo "rm ${files}" >> "${tmpfl}"
370+
done
371+
echo "bye" >> "${tmpfl}"
372+
# if it is only cd & bye, skip it
373+
if [ "$(wc -l "${tmpfl}" | awk '{print $1}')" -gt "2" ] ; then
374+
sftp_run_cmds "${tmpfl}"
326375
fi
376+
rm "${tmpfl}"
377+
# provide an index so people can find files.
378+
ssh -o "StrictHostKeyChecking no" "${EXTRA_SSH}" "${SSHUSER}@${SSHHOST}" \
379+
"ls -lt ${DEPLOY_TO}" | grep "${LIBNAME}" > "${LIBNAME}_index.html"
380+
echo "ls captured"
381+
382+
echo "cd ${DEPLOY_TO}" > "${tmpfl}"
383+
# prune old / removed branches, leave things are are tags/branches
384+
for old in $(sed 's/-> .*$//' libiio_index.html | \
385+
awk '{print $NF}' | grep -v master | sort | \
386+
sed "s/_libiio-0.[0-9][0-9].g[a-z0-9]*-/ %% /" | \
387+
grep "%%" | awk '{print $1}' | sort -u)
388+
do
389+
if [ "$(git --git-dir "${TRAVIS_BUILD_DIR}/.git" ls-remote --heads origin "${old}" | wc -l)" -ne "0" ] ; then
390+
echo "${old} is a branch"
391+
else
392+
if [ "$(git --git-dir "${TRAVIS_BUILD_DIR}/.git" ls-remote --tags origin "${old}" | wc -l)" -ne "0" ] ; then
393+
echo "${old} is a tag"
394+
else
395+
echo "${old} can be removed"
396+
echo "rm ${old}_${LIBNAME}-*" >> "${tmpfl}"
397+
echo "rm ${old}_latest_${LIBNAME}-*" >> "${tmpfl}"
398+
echo "rm ${old}_lastest_${LIBNAME}-*" >> "${tmpfl}"
399+
fi
400+
fi
401+
done
402+
# cap things at 15, so we don't exceed the time
403+
sed -i 16q "${tmpfl}"
404+
echo "bye" >> "${tmpfl}"
405+
# if it is only cd & bye, skip it
406+
if [ "$(wc -l "${tmpfl}" | awk '{print $1}')" -gt "2" ] ; then
407+
sftp_run_cmds "${tmpfl}"
408+
fi
409+
rm "${tmpfl}"
410+
411+
#Now that we removed things, do it again
412+
rm "${LIBNAME}_index.html"
413+
ssh -o "StrictHostKeyChecking no" "${EXTRA_SSH}" "${SSHUSER}@${SSHHOST}" \
414+
"ls -lt ${DEPLOY_TO}" | grep "${LIBNAME}" > "${LIBNAME}_index.html"
415+
sftp_upload "${LIBNAME}_index.html" "${LIBNAME}_index.html"
327416

328417
return 0
329418
}

0 commit comments

Comments
 (0)