justfile 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. commonenv := "CGO_ENABLED=0"
  2. version := `./tools/image-tag`
  3. commit := `git rev-parse --short HEAD`
  4. default:
  5. just --list
  6. # format all Go code
  7. fmt:
  8. go fmt ./...
  9. cd ./core && go fmt ./...
  10. cd ./modules/collector-source && go fmt ./...
  11. cd ./modules/prometheus-source && go fmt ./...
  12. cd ./modules/pricing/basic && go fmt ./...
  13. cd ./modules/pricing/public && go fmt ./...
  14. # check if code is formatted
  15. fmt-check:
  16. #!/bin/sh
  17. echo "Checking code formatting..."
  18. unformatted="$(gofmt -l .)"
  19. if [ -n "$unformatted" ]; then \
  20. echo "The following files are not formatted:"; \
  21. echo "$unformatted"; \
  22. echo ""; \
  23. echo "Run 'just fmt' to format your code"; \
  24. exit 1; \
  25. fi
  26. echo "All files are properly formatted"
  27. # run core unit tests
  28. test-core:
  29. {{commonenv}} cd ./core && go test ./... -coverprofile=coverage.out
  30. {{commonenv}} cd ./core && go vet ./...
  31. # run prometheus-source unit tests
  32. test-prometheus-source:
  33. {{commonenv}} cd ./modules/prometheus-source && go test ./... -coverprofile=coverage.out
  34. {{commonenv}} cd ./modules/prometheus-source && go vet ./...
  35. # run collector-source unit tests
  36. test-collector-source:
  37. {{commonenv}} cd ./modules/collector-source && go test ./... -coverprofile=coverage.out
  38. {{commonenv}} cd ./modules/collector-source && go vet ./...
  39. # run the opencost unit tests
  40. test-opencost:
  41. {{commonenv}} go test ./... -coverprofile=coverage.out
  42. {{commonenv}} go tool cover -html=coverage.out -o coverage.html
  43. {{commonenv}} go vet ./...
  44. # Run unit tests, merge coverage reports, remove old reports
  45. test: test-core test-prometheus-source test-collector-source test-opencost
  46. find . -name "coverage.out" -print0 | xargs -0 cat > coverage.new
  47. find . -name "coverage.out" -delete
  48. mv coverage.new coverage.out
  49. # Run unit tests and integration tests
  50. test-integration:
  51. {{commonenv}} INTEGRATION=true go test ./... -coverprofile=coverage.out
  52. # Compile a local binary
  53. build-local:
  54. cd ./cmd/costmodel && \
  55. {{commonenv}} go build \
  56. -ldflags \
  57. "-X github.com/opencost/opencost/core/pkg/version.Version={{version}} \
  58. -X github.com/opencost/opencost/core/pkg/version.GitCommit={{commit}}" \
  59. -o ./costmodel
  60. # Build multiarch binaries
  61. build-binary VERSION=version:
  62. cd ./cmd/costmodel && \
  63. {{commonenv}} GOOS=linux GOARCH=amd64 go build \
  64. -ldflags \
  65. "-X github.com/opencost/opencost/core/pkg/version.Version={{VERSION}} \
  66. -X github.com/opencost/opencost/core/pkg/version.GitCommit={{commit}}" \
  67. -o ./costmodel-amd64
  68. cd ./cmd/costmodel && \
  69. {{commonenv}} GOOS=linux GOARCH=arm64 go build \
  70. -ldflags \
  71. "-X github.com/opencost/opencost/core/pkg/version.Version={{VERSION}} \
  72. -X github.com/opencost/opencost/core/pkg/version.GitCommit={{commit}}" \
  73. -o ./costmodel-arm64
  74. # Build and push a multi-arch image using Docker
  75. build IMAGE_TAG RELEASE_VERSION: (build-binary RELEASE_VERSION)
  76. docker buildx build \
  77. --rm \
  78. --platform "linux/amd64" \
  79. -f 'Dockerfile.cross' \
  80. --build-arg binarypath=./cmd/costmodel/costmodel-amd64 \
  81. --build-arg version={{RELEASE_VERSION}} \
  82. --build-arg commit={{commit}} \
  83. --provenance=false \
  84. -t {{IMAGE_TAG}}-amd64 \
  85. --push \
  86. .
  87. docker buildx build \
  88. --rm \
  89. --platform "linux/arm64" \
  90. -f 'Dockerfile.cross' \
  91. --build-arg binarypath=./cmd/costmodel/costmodel-arm64 \
  92. --build-arg version={{RELEASE_VERSION}} \
  93. --build-arg commit={{commit}} \
  94. --provenance=false \
  95. -t {{IMAGE_TAG}}-arm64 \
  96. --push \
  97. .
  98. manifest-tool push from-args \
  99. --platforms "linux/amd64,linux/arm64" \
  100. --template {{IMAGE_TAG}}-ARCH \
  101. --target {{IMAGE_TAG}}
  102. # Build and push a multi-arch image using Podman
  103. build-podman IMAGE_TAG RELEASE_VERSION: (build-binary RELEASE_VERSION)
  104. podman build \
  105. --rm \
  106. --platform "linux/amd64" \
  107. -f 'Dockerfile.cross' \
  108. --build-arg binarypath=./cmd/costmodel/costmodel-amd64 \
  109. --build-arg version={{RELEASE_VERSION}} \
  110. --build-arg commit={{commit}} \
  111. -t {{IMAGE_TAG}}-amd64 \
  112. .
  113. podman push {{IMAGE_TAG}}-amd64
  114. podman build \
  115. --rm \
  116. --platform "linux/arm64" \
  117. -f 'Dockerfile.cross' \
  118. --build-arg binarypath=./cmd/costmodel/costmodel-arm64 \
  119. --build-arg version={{RELEASE_VERSION}} \
  120. --build-arg commit={{commit}} \
  121. -t {{IMAGE_TAG}}-arm64 \
  122. .
  123. podman push {{IMAGE_TAG}}-arm64
  124. podman manifest create {{IMAGE_TAG}} \
  125. {{IMAGE_TAG}}-amd64 \
  126. {{IMAGE_TAG}}-arm64
  127. podman manifest push --all {{IMAGE_TAG}}
  128. podman manifest rm {{IMAGE_TAG}}
  129. validate-protobuf:
  130. ./generate.sh
  131. git diff --exit-code