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
@@ -0,0 +1,35 @@
|
||||
# Builds dibs for every v* tag and publishes it as a release with the binaries for Linux
|
||||
# (amd64), Windows (amd64) and macOS (arm64) attached (the Makefile's TARGETS), so
|
||||
# .../releases/download/latest/dibs-<os>-<arch> always serves the newest one.
|
||||
# Pre-release tags (containing "-") and tags on commits that are not on master are built and
|
||||
# tested but not released (see ci/release.sh).
|
||||
# Uses the repository secret RELEASE_TOKEN (a token with "repository: Read and Write") when it
|
||||
# exists, else the job's automatic token.
|
||||
name: release
|
||||
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- "v*"
|
||||
|
||||
jobs:
|
||||
release:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
|
||||
with:
|
||||
# release.sh reads the v* tag on HEAD and checks that HEAD is on origin/master
|
||||
fetch-depth: 0
|
||||
|
||||
- uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6.5.0
|
||||
with:
|
||||
go-version-file: go.mod
|
||||
|
||||
# The build runs without the token; only the publish step gets it.
|
||||
- name: Build
|
||||
run: ci/release.sh build
|
||||
|
||||
- name: Publish
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.RELEASE_TOKEN || secrets.GITEA_TOKEN }}
|
||||
run: ci/release.sh publish
|
||||
@@ -1,6 +1,7 @@
|
||||
.*
|
||||
!.git*
|
||||
!.dockerignore
|
||||
!.golangci.yml
|
||||
|
||||
*.md
|
||||
!README.md
|
||||
@@ -23,3 +24,8 @@ go.work.sum
|
||||
|
||||
# Real shop pages fetched by 'dibs fixtures'.
|
||||
testdata/*/pages/
|
||||
|
||||
# Build output.
|
||||
/dist/
|
||||
/dibs
|
||||
/dibs.exe
|
||||
@@ -0,0 +1,7 @@
|
||||
version: "2"
|
||||
|
||||
linters:
|
||||
default: standard
|
||||
|
||||
formatters:
|
||||
enable: []
|
||||
@@ -0,0 +1,42 @@
|
||||
# make build builds dibs for this machine
|
||||
# make test runs the tests; GOTESTFLAGS=-short skips those that start Chrome
|
||||
# make lint runs golangci-lint, built from source at a pinned version
|
||||
# make release cross-compiles TARGETS into dist/, with SHA256SUMS
|
||||
#
|
||||
# VERSION defaults to `git describe`, such as v0.3.0 or v0.3.0-4-gabc1234-dirty.
|
||||
|
||||
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo dev)
|
||||
TARGETS ?= linux/amd64 windows/amd64 darwin/arm64
|
||||
GOTESTFLAGS ?=
|
||||
|
||||
GOLANGCI_LINT ?= go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.14.0
|
||||
|
||||
# File names carry the version without its "v", like dibs-0.3.0-linux-amd64.
|
||||
FILE_VERSION := $(patsubst v%,%,$(VERSION))
|
||||
LDFLAGS := -s -w -X main.version=$(VERSION)
|
||||
|
||||
.PHONY: build test lint release clean
|
||||
|
||||
build:
|
||||
CGO_ENABLED=0 go build -trimpath -ldflags '$(LDFLAGS)' -o dibs$(shell go env GOEXE) ./cmd/dibs
|
||||
|
||||
test:
|
||||
go test $(GOTESTFLAGS) ./...
|
||||
|
||||
lint:
|
||||
$(GOLANGCI_LINT) run ./...
|
||||
|
||||
release:
|
||||
rm -rf dist
|
||||
mkdir -p dist
|
||||
set -e; for target in $(TARGETS); do \
|
||||
os=$${target%/*}; arch=$${target#*/}; ext=; \
|
||||
if [ "$$os" = windows ]; then ext=.exe; fi; \
|
||||
out=dist/dibs-$(FILE_VERSION)-$$os-$$arch$$ext; \
|
||||
echo "$$out"; \
|
||||
CGO_ENABLED=0 GOOS=$$os GOARCH=$$arch go build -trimpath -ldflags '$(LDFLAGS)' -o "$$out" ./cmd/dibs; \
|
||||
done
|
||||
cd dist && sha256sum dibs-* > SHA256SUMS
|
||||
|
||||
clean:
|
||||
rm -rf dist dibs dibs.exe
|
||||
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 "$@"
|
||||
@@ -207,7 +207,7 @@ func (m *Manager) BrowserLogin(ctx context.Context, site, loginURL string) error
|
||||
|
||||
n, err := m.chrome.SignIn(ctx, loginURL)
|
||||
if err != nil { return err }
|
||||
if n == 0 { return fmt.Errorf("Chrome had no cookies for %s", host) }
|
||||
if n == 0 { return fmt.Errorf("no cookies from Chrome for %s", host) }
|
||||
|
||||
m.update(func(r *Record) { r.Logins[host] = time.Now() })
|
||||
m.limiter.Success(host)
|
||||
|
||||
Reference in new issue
Block a user