cli/.drone.jsonnet

149 lines
3.9 KiB
Plaintext
Raw Permalink Normal View History

2022-11-26 18:42:13 +00:00
// generate .drone.yaml, run:
// drone jsonnet --format --stream
2022-11-26 23:45:44 +00:00
local CreateRelease() = {
name: 'create release',
image: 'plugins/gitea-release',
settings: {
api_key: { from_secret: 'GITEA_API_KEY' },
base_url: 'https://git.unlock-music.dev',
files: [
'um-*.tar.gz',
'um-*.zip',
],
2022-11-26 23:45:44 +00:00
checksum: 'sha256',
draft: true,
title: '${DRONE_TAG}',
},
};
2022-11-26 18:42:13 +00:00
local StepGoBuild(GOOS, GOARCH) = {
2024-10-20 21:04:54 +00:00
local windows = GOOS == 'windows',
local archiveExt = if windows then 'zip' else 'tar.gz',
local filepath = 'dist/um-%s-%s-%s.%s' % [GOOS, GOARCH, '$(git describe --tags --always)', archiveExt],
2024-10-20 21:04:54 +00:00
local archive = if windows then [
// Ensure zip is installed
'command -v zip >/dev/null || (apt update && apt install -y zip)',
'zip -9 -j -r "%s" $DIST_DIR' % filepath,
] else [
'tar -c -C $DIST_DIR um | gzip -9 > "%s"' % filepath,
2024-10-20 21:04:54 +00:00
],
2022-11-26 18:42:13 +00:00
2022-11-26 23:47:42 +00:00
name: 'go build %s/%s' % [GOOS, GOARCH],
image: 'golang:1.23',
2022-11-26 18:42:13 +00:00
environment: {
GOOS: GOOS,
GOARCH: GOARCH,
2024-10-20 21:04:54 +00:00
GOPROXY: 'https://goproxy.io,direct',
2022-11-26 18:42:13 +00:00
},
commands: [
'DIST_DIR=$(mktemp -d)',
'go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags --always)" -o $DIST_DIR ./cmd/um',
2022-11-26 23:50:04 +00:00
'mkdir -p dist',
2024-10-20 21:04:54 +00:00
] + archive,
2022-11-26 18:42:13 +00:00
};
local StepUploadArtifact(GOOS, GOARCH) = {
2024-10-20 21:10:36 +00:00
local windows = GOOS == 'windows',
local archiveExt = if windows then 'zip' else 'tar.gz',
local filename = 'um-%s-%s-%s.%s' % [GOOS, GOARCH, '$(git describe --tags --always)', archiveExt],
2022-11-26 23:45:44 +00:00
local filepath = 'dist/%s' % filename,
2022-11-26 18:42:13 +00:00
local pkgname = '${DRONE_REPO_NAME}-build',
name: 'upload artifact',
image: 'golang:1.23', // reuse golang:1.19 for curl
2022-11-26 18:42:13 +00:00
environment: {
DRONE_GITEA_SERVER: 'https://git.unlock-music.dev',
GITEA_API_KEY: { from_secret: 'GITEA_API_KEY' },
},
commands: [
'curl --fail --include --user "um-release-bot:$GITEA_API_KEY" ' +
2022-11-26 23:45:44 +00:00
'--upload-file "%s" ' % filepath +
2022-11-26 18:42:13 +00:00
'"$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/%s/${DRONE_BUILD_NUMBER}/%s"' % [pkgname, filename],
2022-11-26 23:45:44 +00:00
'sha256sum %s' % filepath,
2022-11-26 18:42:13 +00:00
'echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/%s/${DRONE_BUILD_NUMBER}' % pkgname,
],
};
local PipelineBuild(GOOS, GOARCH, RUN_TEST) = {
name: 'build %s/%s' % [GOOS, GOARCH],
kind: 'pipeline',
type: 'docker',
steps: [
{
name: 'fetch tags',
image: 'alpine/git',
commands: ['git fetch --tags'],
},
] +
(
if RUN_TEST then [{
name: 'go test',
image: 'golang:1.23',
2024-04-24 08:13:46 +00:00
environment: {
2024-10-20 21:04:54 +00:00
GOPROXY: 'https://goproxy.io,direct',
2024-04-24 08:13:46 +00:00
},
2022-11-26 18:42:13 +00:00
commands: ['go test -v ./...'],
}] else []
)
+
[
StepGoBuild(GOOS, GOARCH),
StepUploadArtifact(GOOS, GOARCH),
],
trigger: {
event: ['push', 'pull_request'],
},
};
2022-11-26 23:45:44 +00:00
local PipelineRelease() = {
name: 'release',
kind: 'pipeline',
type: 'docker',
steps: [
{
name: 'fetch tags',
image: 'alpine/git',
commands: ['git fetch --tags'],
},
{
name: 'go test',
image: 'golang:1.23',
2024-04-24 08:13:46 +00:00
environment: {
2024-10-20 21:04:54 +00:00
GOPROXY: 'https://goproxy.io,direct',
2024-04-24 08:13:46 +00:00
},
2022-11-26 23:45:44 +00:00
commands: ['go test -v ./...'],
},
StepGoBuild('linux', 'amd64'),
StepGoBuild('linux', 'arm64'),
StepGoBuild('linux', '386'),
StepGoBuild('windows', 'amd64'),
2024-10-20 22:29:52 +00:00
StepGoBuild('windows', 'arm64'),
2022-11-26 23:45:44 +00:00
StepGoBuild('windows', '386'),
StepGoBuild('darwin', 'amd64'),
StepGoBuild('darwin', 'arm64'),
{
name: 'prepare root',
image: 'golang:1.23',
commands: [
'mv dist/*.tar.gz dist/*.zip ./',
],
},
2022-11-26 23:45:44 +00:00
CreateRelease(),
],
trigger: {
event: ['tag'],
},
};
2022-11-26 18:42:13 +00:00
[
PipelineBuild('linux', 'amd64', true),
PipelineBuild('windows', 'amd64', false),
PipelineBuild('darwin', 'amd64', false),
2022-11-26 23:45:44 +00:00
PipelineRelease(),
2022-11-26 18:42:13 +00:00
]