.drone.jsonnet 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // generate .drone.yaml, run:
  2. // drone jsonnet --format --stream
  3. local CreateRelease() = {
  4. name: 'create release',
  5. image: 'plugins/gitea-release',
  6. settings: {
  7. api_key: { from_secret: 'GITEA_API_KEY' },
  8. base_url: 'https://git.unlock-music.dev',
  9. files: [
  10. 'um-*.tar.gz',
  11. 'um-*.zip',
  12. ],
  13. checksum: 'sha256',
  14. draft: true,
  15. title: '${DRONE_TAG}',
  16. },
  17. };
  18. local StepGoBuild(GOOS, GOARCH) = {
  19. local windows = GOOS == 'windows',
  20. local archiveExt = if windows then 'zip' else 'tar.gz',
  21. local filepath = 'dist/um-%s-%s-%s.%s' % [GOOS, GOARCH, '$(git describe --tags --always)', archiveExt],
  22. local archive = if windows then [
  23. // Ensure zip is installed
  24. 'command -v zip >/dev/null || (apt update && apt install -y zip)',
  25. 'zip -9 -j -r "%s" $DIST_DIR' % filepath,
  26. ] else [
  27. 'tar -c -C $DIST_DIR um | gzip -9 > "%s"' % filepath,
  28. ],
  29. name: 'go build %s/%s' % [GOOS, GOARCH],
  30. image: 'golang:1.23',
  31. environment: {
  32. GOOS: GOOS,
  33. GOARCH: GOARCH,
  34. GOPROXY: 'https://goproxy.io,direct',
  35. },
  36. commands: [
  37. 'DIST_DIR=$(mktemp -d)',
  38. 'go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags --always)" -o $DIST_DIR ./cmd/um',
  39. 'mkdir -p dist',
  40. ] + archive,
  41. };
  42. local StepUploadArtifact(GOOS, GOARCH) = {
  43. local windows = GOOS == 'windows',
  44. local archiveExt = if windows then 'zip' else 'tar.gz',
  45. local filename = 'um-%s-%s-%s.%s' % [GOOS, GOARCH, '$(git describe --tags --always)', archiveExt],
  46. local filepath = 'dist/%s' % filename,
  47. local pkgname = '${DRONE_REPO_NAME}-build',
  48. name: 'upload artifact',
  49. image: 'golang:1.23', // reuse golang:1.19 for curl
  50. environment: {
  51. DRONE_GITEA_SERVER: 'https://git.unlock-music.dev',
  52. GITEA_API_KEY: { from_secret: 'GITEA_API_KEY' },
  53. },
  54. commands: [
  55. 'curl --fail --include --user "um-release-bot:$GITEA_API_KEY" ' +
  56. '--upload-file "%s" ' % filepath +
  57. '"$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/%s/${DRONE_BUILD_NUMBER}/%s"' % [pkgname, filename],
  58. 'sha256sum %s' % filepath,
  59. 'echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/%s/${DRONE_BUILD_NUMBER}' % pkgname,
  60. ],
  61. };
  62. local PipelineBuild(GOOS, GOARCH, RUN_TEST) = {
  63. name: 'build %s/%s' % [GOOS, GOARCH],
  64. kind: 'pipeline',
  65. type: 'docker',
  66. steps: [
  67. {
  68. name: 'fetch tags',
  69. image: 'alpine/git',
  70. commands: ['git fetch --tags'],
  71. },
  72. ] +
  73. (
  74. if RUN_TEST then [{
  75. name: 'go test',
  76. image: 'golang:1.23',
  77. environment: {
  78. GOPROXY: 'https://goproxy.io,direct',
  79. },
  80. commands: ['go test -v ./...'],
  81. }] else []
  82. )
  83. +
  84. [
  85. StepGoBuild(GOOS, GOARCH),
  86. StepUploadArtifact(GOOS, GOARCH),
  87. ],
  88. trigger: {
  89. event: ['push', 'pull_request'],
  90. },
  91. };
  92. local PipelineRelease() = {
  93. name: 'release',
  94. kind: 'pipeline',
  95. type: 'docker',
  96. steps: [
  97. {
  98. name: 'fetch tags',
  99. image: 'alpine/git',
  100. commands: ['git fetch --tags'],
  101. },
  102. {
  103. name: 'go test',
  104. image: 'golang:1.23',
  105. environment: {
  106. GOPROXY: 'https://goproxy.io,direct',
  107. },
  108. commands: ['go test -v ./...'],
  109. },
  110. StepGoBuild('linux', 'amd64'),
  111. StepGoBuild('linux', 'arm64'),
  112. StepGoBuild('linux', '386'),
  113. StepGoBuild('windows', 'amd64'),
  114. StepGoBuild('windows', 'arm64'),
  115. StepGoBuild('windows', '386'),
  116. StepGoBuild('darwin', 'amd64'),
  117. StepGoBuild('darwin', 'arm64'),
  118. {
  119. name: 'prepare root',
  120. image: 'golang:1.23',
  121. commands: [
  122. 'mv dist/*.tar.gz dist/*.zip ./',
  123. ],
  124. },
  125. CreateRelease(),
  126. ],
  127. trigger: {
  128. event: ['tag'],
  129. },
  130. };
  131. [
  132. PipelineBuild('linux', 'amd64', true),
  133. PipelineBuild('windows', 'amd64', false),
  134. PipelineBuild('darwin', 'amd64', false),
  135. PipelineRelease(),
  136. ]