Skip to content

Commit

Permalink
Add services for memcached and postgresql
Browse files Browse the repository at this point in the history
  • Loading branch information
buehlmann committed Nov 11, 2024
1 parent c494a78 commit e16d612
Showing 1 changed file with 58 additions and 10 deletions.
68 changes: 58 additions & 10 deletions ci/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,70 @@ package main
import (
"context"
"dagger/ci/internal/dagger"
"fmt"
)

type Ci struct{}

// Returns a Container built from the Dockerfile in the provided Directory
func (m *Ci) Build(ctx context.Context, directory *dagger.Directory) *dagger.Container {
return dag.Container().Build(directory)
func (m *Ci) Build(_ context.Context, dir *dagger.Directory) *dagger.Container {
return dag.Container().Build(dir)
}

// Returns the result of haml-lint run against the sources in the provided Directory
func (m *Ci) Lint(ctx context.Context, directory *dagger.Directory) (string, error) {
return dag.Container().
From("ruby:latest").
WithMountedDirectory("/mnt", directory).
WithWorkdir("/mnt").
WithExec([]string{"gem", "install", "haml-lint"}).
WithExec([]string{"haml-lint", "--reporter", "json", "."}).
Stdout(ctx)
func (m *Ci) Lint(ctx context.Context, dir *dagger.Directory) (string, error) {
return dag.Container().
From("ruby:latest").
WithMountedDirectory("/mnt", dir).
WithWorkdir("/mnt").
WithExec([]string{"gem", "install", "haml-lint"}).
WithExec([]string{"haml-lint", "--reporter", "json", "."}).
Stdout(ctx)
}

// Creates a PostgreSQL service for local testing based on the official image with the provided version. If no version is provided, 'latest' will be used.
func (m *Ci) Postgres(
_ context.Context,
// +optional
// +default="latest"
version string) *dagger.Service {

return dag.Container().
From(fmt.Sprintf("postgres:%s", version)).
WithEnvVariable("POSTGRES_PASSWORD", "postgres").
WithExposedPort(5432).
AsService()
}

// Creates a memcached service for local testing based on the official image with the provided version. If no version is provided, 'latest' will be used.
func (m *Ci) Memcached(
_ context.Context,
// +optional
// +default="latest"
version string) *dagger.Service {

return dag.Container().
From(fmt.Sprintf("postgres:%s", version)).
WithEnvVariable("POSTGRES_PASSWORD", "postgres").
WithExposedPort(5432).
AsService()
}

// Executes the test suite for the Rails application in the provided Directory
func (m *Ci) Test(ctx context.Context, dir *dagger.Directory) *dagger.Container {
return m.Build(ctx, dir).From("ruby:latest").
WithEnvVariable("RAILS_TEST_DB_NAME", "postgres").
WithEnvVariable("RAILS_TEST_DB_USERNAME", "postgres").
WithEnvVariable("RAILS_TEST_DB_PASSWORD", "postgres").
WithEnvVariable("RAILS_ENV", "test").
WithEnvVariable("CI", "true").
WithEnvVariable("PGDATESTYLE", "German").
WithExec([]string{"sudo", "apt-get", "-yqq", "update"}).
WithExec([]string{"sudo", "apt-get", "-yqq", "install", "libpq-dev", "libvips-dev"}).
WithExec([]string{"gem", "install", "bundler", "--version", "'~> 2'"}).
WithExec([]string{"bundle", "install", "--jobs", "4", "--retry", "3"}).
WithExec([]string{"bundle", "exec", "rails", "db:create"}).
WithExec([]string{"bundle", "exec", "rails", "db:migrate"}).
WithExec([]string{"bundle", "exec", "rails", "assets:precompile"}).
WithExec([]string{"bundle", "exec", "rails", "test"})
}

0 comments on commit e16d612

Please sign in to comment.