Skip to content

Commit

Permalink
ci(dagger): simplify module (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
lerenn authored Jul 25, 2024
1 parent 553134b commit 4a71edd
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 58 deletions.
13 changes: 4 additions & 9 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ jobs:
- uses: dagger/dagger-for-github@v5
with:
verb: call
args: -m build/ci tests --src-dir .
version: "0.11.0"
args: -m build/ci test --src-dir .

examples:
name: Run examples
Expand All @@ -36,8 +35,7 @@ jobs:
- uses: dagger/dagger-for-github@v5
with:
verb: call
args: -m build/ci examples --src-dir .
version: "0.11.0"
args: -m build/ci examples --src-dir . stderr

lint:
name: Run linter
Expand All @@ -52,8 +50,7 @@ jobs:
- uses: dagger/dagger-for-github@v5
with:
verb: call
args: -m build/ci lint --src-dir .
version: "0.11.0"
args: -m build/ci lint --src-dir .

check-generation:
name: Check generation has been run
Expand All @@ -69,7 +66,6 @@ jobs:
with:
verb: call
args: -m build/ci check-generation --src-dir .
version: "0.11.0"

publish:
name: Publish a new version
Expand All @@ -95,5 +91,4 @@ jobs:
- uses: dagger/dagger-for-github@v5
with:
verb: call
args: -m build/ci publish --src-dir .
version: "0.11.0"
args: -m build/ci publish --src-dir .
24 changes: 12 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@ check-generation: ## Check files are generated locally
@sh ./scripts/check-generation.sh

.PHONY: clean
clean: down ## Clean the project locally
clean: local-env/stop ## Clean the project locally
@rm -rf ./tmp/certs

.PHONY: down
down: ## Stop the local environment
@docker-compose down

.PHONY: generate
generate: ## Generate files locally
@go generate ./...
Expand All @@ -23,15 +19,19 @@ generate: ## Generate files locally
lint: ## Lint the code locally
@go run github.com/golangci/golangci-lint/cmd/[email protected] run ./...

.PHONY: local-env/start
local-env/start: ## Start the local environment
@go run ./tools/generate-certs
@docker-compose local-env/start -d

.PHONY: local-env/stop
local-env/stop: ## Stop the local environment
@docker-compose local-env/stop

.PHONY: publish
publish: dagger/publish ## Publish with tag on git, docker hub, etc. locally
@git tag ${TAG} && git push origin ${TAG}

.PHONY: test
test: up ## Perform tests locally
@go test ./...

.PHONY: up
up: ## Start the local environment
@go run ./tools/generate-certs
@docker-compose up -d
test: local-env/start ## Perform tests locally
@go test ./...
1 change: 1 addition & 0 deletions build/ci/dagger/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/sosodev/duration v1.3.1 // indirect
go.opentelemetry.io/otel v1.28.0
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.0.0-20240518090000-14441aefdf88
Expand Down
10 changes: 10 additions & 0 deletions build/ci/dagger/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNg
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand All @@ -19,12 +20,17 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1YCS1PXdKYWi8FsN0=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
github.com/sosodev/duration v1.3.1 h1:qtHBDMQ6lvMQsL15g4aopM4HEfOaYuhWBw3NPTtlqq4=
github.com/sosodev/duration v1.3.1/go.mod h1:RQIBBX0+fMLc/D9+Jb/fwvVmo0eZvDDEERAikUR6SDg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/vektah/gqlparser/v2 v2.5.16 h1:1gcmLTvs3JLKXckwCwlUagVn/IlV2bwqle0vJ0vy5p8=
Expand Down Expand Up @@ -73,5 +79,9 @@ google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY=
google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
64 changes: 27 additions & 37 deletions build/ci/dagger/cmd.go → build/ci/dagger/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,67 +42,61 @@ func (ci *AsyncapiCodegenCi) cachedBrokers() map[string]*dagger.Service {
func (ci *AsyncapiCodegenCi) Check(
ctx context.Context,
srcDir *dagger.Directory,
) (string, error) {
if _, err := ci.CheckGeneration(ctx, srcDir); err != nil {
return "", err
}
) ([]*dagger.Container, error) {
containers := make([]*dagger.Container, 0)

if _, err := ci.Lint(ctx, srcDir); err != nil {
return "", err
}
containers = append(containers, ci.CheckGeneration(ctx, srcDir))
containers = append(containers, ci.Lint(ctx, srcDir))

if _, err := ci.Examples(ctx, srcDir); err != nil {
return "", err
examples, err := ci.Examples(ctx, srcDir)
if err != nil {
return nil, err
}
containers = append(containers, examples...)

if _, err := ci.Tests(ctx, srcDir); err != nil {
return "", err
}
containers = append(containers, ci.Test(ctx, srcDir))

return "", nil
return containers, nil
}

// CheckGeneration generate files from Golang generate command on AsyncAPI-Codegen
// source code and check that there is no change.
func (ci *AsyncapiCodegenCi) CheckGeneration(
ctx context.Context,
srcDir *dagger.Directory,
) (string, error) {
_, err := dag.Container().
) *dagger.Container {
return dag.Container().
From(golangImage).
With(sourceCodeAndGoCache(srcDir)).
WithExec([]string{"sh", "./scripts/check-generation.sh"}).
Stdout(ctx)

return "", err
WithExec([]string{"sh", "./scripts/check-generation.sh"})
}

// Lint AsyncAPI-Codegen source code.
func (ci *AsyncapiCodegenCi) Lint(
ctx context.Context,
srcDir *dagger.Directory,
) (string, error) {
) *dagger.Container {
return dag.Container().
From(linterImage).
With(sourceCodeAndGoCache(srcDir)).
WithMountedCache("/root/.cache/golangci-lint", dag.CacheVolume("golangci-lint")).
WithExec([]string{"golangci-lint", "run"}).
Stdout(ctx)
WithExec([]string{"golangci-lint", "run"})
}

// Run AsyncAPI-Codegen examples.
// Examples runs AsyncAPI-Codegen examples.
func (ci *AsyncapiCodegenCi) Examples(
ctx context.Context,
srcDir *dagger.Directory,
) (string, error) {
) ([]*dagger.Container, error) {
// Get examples subdirs
subdirs, err := directoriesAtSublevel(ctx, srcDir.Directory("examples"), 2, "./examples")
if err != nil {
return "", err
return nil, err
}

// Get examples containers
for _, p := range subdirs {
containers := make([]*dagger.Container, len(subdirs))
for i, p := range subdirs {
// Set app container
app := dag.Container().
From(golangImage).
Expand Down Expand Up @@ -130,21 +124,18 @@ func (ci *AsyncapiCodegenCi) Examples(
// Execute command
WithExec([]string{"go", "run", p + "/user"})

// Execute user container
stderr, err := user.Stderr(ctx)
if err != nil {
return stderr, err
}
// Add user container to list
containers[i] = user
}

return "", nil
return containers, nil
}

// Run tests from AsyncAPICodegen
func (ci *AsyncapiCodegenCi) Tests(
// Test run tests from AsyncAPICodegen
func (ci *AsyncapiCodegenCi) Test(
ctx context.Context,
srcDir *dagger.Directory,
) (string, error) {
) *dagger.Container {
return dag.Container().
// Add base image
From(golangImage).
Expand All @@ -153,8 +144,7 @@ func (ci *AsyncapiCodegenCi) Tests(
// Set brokers as dependencies of app and user
With(bindBrokers(ci.cachedBrokers())).
// Execute command
WithExec([]string{"go", "test", "./..."}).
Stdout(ctx)
WithExec([]string{"go", "test", "./..."})
}

// Publish tag on git repository and docker image(s) on Docker Hub
Expand Down

0 comments on commit 4a71edd

Please sign in to comment.