beacon-node-builds: refactor to rebuild after failures

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2021-05-21 00:38:03 +02:00
parent ee9f8a74b7
commit 47f500e1ad
No known key found for this signature in database
GPG Key ID: 4EF064D0E6D63020
2 changed files with 74 additions and 40 deletions

View File

@ -8,7 +8,7 @@ RUN apt-get -qq update \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
{% for target in item.targets %} {% for target in item.targets %}
COPY repo/build/{{ target }} /usr/local/bin/ COPY repo/build/{{ target }}_${COMMIT} /usr/local/bin/{{ target }}
{% endfor %} {% endfor %}
STOPSIGNAL SIGINT STOPSIGNAL SIGINT

View File

@ -1,42 +1,25 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -e set -e
if [[ "${USER}" != "{{ beacon_node_builds_user }}" ]]; then function headIsDetached() {
echo "Incorrect user: ${USER}" >&2 [[ $(git rev-parse --abbrev-ref --symbolic-full-name HEAD) == "HEAD" ]];
echo "Expected: {{ beacon_node_builds_user }}" >&2 }
exit 1
fi
IMAGE="{{ beacon_node_builds_image_name }}" function dockerImageExists() {
TAG="{{ item.name }}" docker image inspect "${IMAGE}:${COMMIT}" 2>&1 1>/dev/null;
}
# Build the Beacon node binaries function binaryExists() {
pushd repo ls -l build/{{ item.targets | first }}_${COMMIT} 2>&1 1>/dev/null
}
COMMIT_BEFORE=$(git rev-parse --short=8 HEAD) function fetchChanges() {
SYMBOLIC_NAME=$(git rev-parse --abbrev-ref --symbolic-full-name HEAD)
function headIsDetached() { [[ "${SYMBOLIC_NAME}" == "HEAD" ]]; }
# Detached HEAD means we're probably on a tag
if headIsDetached; then
echo "Deatached HEAD, nothing to fetch."
else
# We cannot use "git pull" in here, because history may be changed upstream # We cannot use "git pull" in here, because history may be changed upstream
git fetch git fetch
git reset --hard origin/{{ item.version }} git reset --hard "origin/${BRANCH}"
fi }
COMMIT_AFTER=$(git rev-parse --short=8 HEAD)
if [[ "$1" == "--force" ]]; then
echo "Forcing rebuild!"
elif ! headIsDetached && [[ "${COMMIT_BEFORE}" == "${COMMIT_AFTER}" ]]; then
echo "Nothing new to build."
exit
fi
function buildBinaries() {
# Lower CPU and I/O priority so it doesn't affect the running beacon node # Lower CPU and I/O priority so it doesn't affect the running beacon node
NICE="nice -n 19 ionice -c2 -n7" NICE="nice -n 19 ionice -c2 -n7"
@ -44,18 +27,69 @@ ${NICE} make -j1 update
${NICE} make -j1 {{ item.targets | join(" ") }} \ ${NICE} make -j1 {{ item.targets | join(" ") }} \
LOG_LEVEL="TRACE" NIMFLAGS="-d:testnet_servers_image -d:noSignalHandler" LOG_LEVEL="TRACE" NIMFLAGS="-d:testnet_servers_image -d:noSignalHandler"
# Keep some copies of the resulting binaries, to be used for debugging in case of core dumps # Rename binaries to match commit the were built from.
cp -a build/nimbus_beacon_node "build/nimbus_beacon_node_{{ item.name }}_$(date +%F_%H-%M-%S)" {% for target in item.targets %}
mv "build/{{ target }}" "build/{{ target }}_${COMMIT}"
{% endfor %}
# Delete copies that are older than 7 days # Delete copies that are older than 7 days
find build -name 'nimbus_beacon_node_*' -mtime +7 -exec rm '{}' \+ find build -mtime +7 -exec rm '{}' \+
}
popd # Adds binary into a simple Alpine image
function buildDockerImage() {
docker build -t "${IMAGE}:${COMMIT}" \
--build-arg=COMMIT=${COMMIT} \
--label "commit=${COMMIT}" .
}
# Add binary into a simple Alpine image function pushImageTag() {
docker build -t "${IMAGE}:${COMMIT_AFTER}" \ docker tag "${IMAGE}:${COMMIT}" "${IMAGE}:${TAG}"
--label "commit=${COMMIT_AFTER}" \
--build-arg=COMMIT=${COMMIT_AFTER} .
docker tag "${IMAGE}:${COMMIT_AFTER}" "${IMAGE}:${TAG}"
docker push "${IMAGE}:${TAG}" docker push "${IMAGE}:${TAG}"
}
echo "SUCCESS - pushed: ${IMAGE}:${TAG}" #-------------------------------------------------------------------------------
BRANCH="{{ item.version }}"
IMAGE="{{ beacon_node_builds_image_name }}"
TAG="{{ item.name }}"
if [[ "${USER}" != "{{ beacon_node_builds_user }}" ]]; then
echo "Incorrect user: ${USER}" >&2
echo "Expected: {{ beacon_node_builds_user }}" >&2
exit 1
fi
# Build the Beacon node binaries
pushd repo >/dev/null
# Detached HEAD means we're probably on a tag
if headIsDetached; then
echo " >>> Deatached HEAD, nothing to fetch."
else
echo " >>> Fetching changes..."
fetchChanges
fi
COMMIT=$(git rev-parse --short=8 HEAD)
if binaryExists && [[ "$1" != "--force" ]]; then
echo " >>> Binary already built"
else
echo " >>> Building binaries..."
buildBinaries
fi
popd >/dev/null
if dockerImageExists && [[ "$1" != "--force" ]]; then
echo " >>> Image already built: ${IMAGE}:${COMMIT}"
else
echo " >>> Building Docker image..."
buildDockerImage
fi
echo " >>> Pushing Docker image..."
pushImageTag
echo " >>> SUCCESS - Pushed: ${IMAGE}:${TAG}"