Add the Makefile and the release workflow.
This commit is contained in:
1 parent
15a34e1f84
commit
4593218996
6 files changed
+305
-1
No files matched your search
Executable
+214
@@ -0,0 +1,214 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Builds dibs for the v* git tag on HEAD and publishes the binaries as a Gitea release. The
|
||||
# targets are the Makefile's TARGETS. Each binary is attached twice: as
|
||||
# dibs-<version>-<os>-<arch>, and as dibs-<os>-<arch>, the stable name behind
|
||||
# <server>/<owner>/<repo>/releases/download/latest/dibs-<os>-<arch>. SHA256SUMS covers the
|
||||
# versioned names.
|
||||
#
|
||||
# A pre-release tag (containing "-", e.g. v0.2.0-rc.1), or a tag on a commit that is not on
|
||||
# master, is built and tested but not released.
|
||||
#
|
||||
# Usage:
|
||||
# ci/release.sh build runs the tests and the linter, and builds the binaries into dist/
|
||||
# ci/release.sh publish creates the release (or reuses the tag's release) and uploads dist/
|
||||
#
|
||||
# Environment:
|
||||
# GITEA_TOKEN token with write access to the repository's releases (publish)
|
||||
# GITHUB_API_URL Gitea API, e.g. https://git.security-command.org/api/v1 (publish; set by Gitea Actions)
|
||||
# GITHUB_REPOSITORY owner/repo (publish; set by Gitea Actions)
|
||||
|
||||
set -o errexit
|
||||
set -o nounset
|
||||
set -o pipefail
|
||||
shopt -s inherit_errexit
|
||||
|
||||
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
readonly REPO_ROOT
|
||||
|
||||
readonly RELEASE_BRANCH="master"
|
||||
|
||||
# Seconds publish waits for HEAD to arrive on the release branch. A push can deliver the tag
|
||||
# before the branch (git-flow pushes both), so the checkout may hold the branch from before.
|
||||
readonly RELEASE_BRANCH_WAIT=60
|
||||
readonly RELEASE_BRANCH_POLL=10
|
||||
|
||||
readonly DIST="${REPO_ROOT}/dist"
|
||||
|
||||
log() {
|
||||
echo "[release] $*" >&2
|
||||
}
|
||||
|
||||
fail() {
|
||||
log "$*"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Whether HEAD is on the release branch. Fetches the branch again rather than trusting the
|
||||
# checkout, and waits up to RELEASE_BRANCH_WAIT seconds for it to arrive.
|
||||
on_release_branch() {
|
||||
local waited=0
|
||||
|
||||
while true; do
|
||||
git -C "${REPO_ROOT}" fetch --quiet origin "+refs/heads/${RELEASE_BRANCH}:refs/remotes/origin/${RELEASE_BRANCH}" \
|
||||
|| log "Cannot fetch ${RELEASE_BRANCH}; checking against the one fetched before"
|
||||
|
||||
if git -C "${REPO_ROOT}" merge-base --is-ancestor HEAD "origin/${RELEASE_BRANCH}" 2> /dev/null; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
if (( waited >= RELEASE_BRANCH_WAIT )); then
|
||||
return 1
|
||||
fi
|
||||
|
||||
log "HEAD is not on ${RELEASE_BRANCH} (yet); fetching it again in ${RELEASE_BRANCH_POLL} seconds"
|
||||
sleep "${RELEASE_BRANCH_POLL}"
|
||||
waited=$(( waited + RELEASE_BRANCH_POLL ))
|
||||
done
|
||||
}
|
||||
|
||||
# Prints the v* tag on HEAD, or nothing.
|
||||
release_tag() {
|
||||
git -C "${REPO_ROOT}" describe --tags --exact-match --match 'v*' 2> /dev/null || true
|
||||
}
|
||||
|
||||
build() {
|
||||
local tag="$1"
|
||||
|
||||
# The tests that start Chrome are skipped; a runner rarely has one installed.
|
||||
log "Testing"
|
||||
make -C "${REPO_ROOT}" test GOTESTFLAGS=-short
|
||||
|
||||
log "Linting"
|
||||
make -C "${REPO_ROOT}" lint
|
||||
|
||||
log "Building ${tag}"
|
||||
make -C "${REPO_ROOT}" release VERSION="${tag}"
|
||||
}
|
||||
|
||||
# Calls the Gitea API; prints the response body. Extra arguments go to curl.
|
||||
api() {
|
||||
local method="$1"
|
||||
local path="$2"
|
||||
shift 2
|
||||
|
||||
curl --fail --silent --show-error \
|
||||
--request "${method}" \
|
||||
--header "Authorization: token ${GITEA_TOKEN}" \
|
||||
--header "Accept: application/json" \
|
||||
"$@" \
|
||||
"${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}${path}"
|
||||
}
|
||||
|
||||
# Prints the id of the tag's release, created when it does not exist yet.
|
||||
release_id() {
|
||||
local tag="$1"
|
||||
local status
|
||||
local body
|
||||
|
||||
# curl exits non-zero when the server cannot be reached; the status is then 000.
|
||||
status="$(curl --silent --output /dev/null --write-out '%{http_code}' \
|
||||
--header "Authorization: token ${GITEA_TOKEN}" \
|
||||
"${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/releases/tags/${tag}" || true)"
|
||||
|
||||
case "${status}" in
|
||||
200)
|
||||
log "Release '${tag}' exists; replacing its files"
|
||||
api GET "/releases/tags/${tag}" | jq --raw-output '.id'
|
||||
return
|
||||
;;
|
||||
404)
|
||||
;;
|
||||
*)
|
||||
fail "Cannot read the releases of '${GITHUB_REPOSITORY}' (HTTP ${status}); is GITEA_TOKEN valid for it?"
|
||||
;;
|
||||
esac
|
||||
|
||||
body="$(jq --null-input \
|
||||
--arg tag "${tag}" \
|
||||
--arg name "Release ${tag} - Stable, Automated" \
|
||||
--arg body "$(date --utc +%Y-%m-%d)"$'\n'"$(git -C "${REPO_ROOT}" rev-parse HEAD)" \
|
||||
'{tag_name: $tag, name: $name, body: $body, draft: false, prerelease: false}')"
|
||||
|
||||
log "Creating release '${tag}'"
|
||||
api POST "/releases" --header "Content-Type: application/json" --data "${body}" | jq --raw-output '.id'
|
||||
}
|
||||
|
||||
# Uploads ${file} as ${name}, replacing an attached file of that name.
|
||||
upload() {
|
||||
local id="$1"
|
||||
local file="$2"
|
||||
local name="$3"
|
||||
local existing
|
||||
|
||||
existing="$(api GET "/releases/${id}/assets" | jq --raw-output --arg name "${name}" '.[] | select(.name == $name) | .id')"
|
||||
if [[ -n "${existing}" ]]; then
|
||||
api DELETE "/releases/${id}/assets/${existing}" > /dev/null
|
||||
fi
|
||||
|
||||
api POST "/releases/${id}/assets?name=${name}" --form "attachment=@${file}" > /dev/null
|
||||
log "Attached '${name}'"
|
||||
}
|
||||
|
||||
publish() {
|
||||
local tag="$1"
|
||||
local version="${tag#v}"
|
||||
local id
|
||||
local file
|
||||
local name
|
||||
|
||||
if [[ "${version}" == *-* ]]; then
|
||||
log "${tag} is a pre-release tag; nothing is released."
|
||||
return
|
||||
fi
|
||||
|
||||
if ! on_release_branch; then
|
||||
log "${tag} is not on ${RELEASE_BRANCH}; nothing is released."
|
||||
return
|
||||
fi
|
||||
|
||||
: "${GITEA_TOKEN:?GITEA_TOKEN is not set}"
|
||||
: "${GITHUB_API_URL:?GITHUB_API_URL is not set}"
|
||||
: "${GITHUB_REPOSITORY:?GITHUB_REPOSITORY is not set}"
|
||||
|
||||
[[ -f "${DIST}/SHA256SUMS" ]] || fail "'${DIST}' has no release build; run 'ci/release.sh build' first."
|
||||
|
||||
id="$(release_id "${tag}")"
|
||||
|
||||
for file in "${DIST}/dibs-${version}-"*; do
|
||||
[[ -f "${file}" ]] || fail "'${DIST}' has no binaries for ${version}; run 'ci/release.sh build' first."
|
||||
|
||||
name="$(basename "${file}")"
|
||||
upload "${id}" "${file}" "${name}"
|
||||
upload "${id}" "${file}" "dibs-${name#dibs-"${version}"-}"
|
||||
done
|
||||
|
||||
upload "${id}" "${DIST}/SHA256SUMS" "SHA256SUMS"
|
||||
|
||||
log "Released ${tag}: ${GITHUB_SERVER_URL:-}/${GITHUB_REPOSITORY}/releases/tag/${tag}"
|
||||
}
|
||||
|
||||
main() {
|
||||
local step="${1:-}"
|
||||
local tag
|
||||
|
||||
tag="$(release_tag)"
|
||||
if [[ -z "${tag}" ]]; then
|
||||
fail "HEAD has no v* git tag (not a release commit, or a shallow clone without tags?); nothing to release."
|
||||
fi
|
||||
|
||||
case "${step}" in
|
||||
build)
|
||||
build "${tag}"
|
||||
;;
|
||||
publish)
|
||||
publish "${tag}"
|
||||
;;
|
||||
*)
|
||||
log "Usage: ci/release.sh build|publish"
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
main "$@"
|
||||
Reference in new issue
Block a user