152 lines
4.4 KiB
Go
152 lines
4.4 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"dagger.io/dagger"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
func main() {
|
|
if err := buildstaging(context.Background()); err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
}
|
|
|
|
func buildstaging(ctx context.Context) error {
|
|
fmt.Println("Inicializando deploy")
|
|
os.Chdir("./..")
|
|
|
|
fmt.Println("Levantar base de datos")
|
|
uniqueVolume := client.CacheVolume(fmt.Sprintf("pgdata-%d", time.Now().UnixNano()))
|
|
databaseDevelopment := client.Container().
|
|
From("postgres:16").
|
|
WithEnvVariable("POSTGRES_PASSWORD", "secret").
|
|
WithEnvVariable("POSTGRES_USER", "savne").
|
|
WithEnvVariable("POSTGRES_DB", "sav_net_test").
|
|
WithExposedPort(5432).
|
|
WithMountedCache("/var/lib/postgresql/data", uniqueVolume).
|
|
WithExec([]string{"postgres"}).
|
|
AsService()
|
|
|
|
client.Pipeline("Migraciones")
|
|
migrations, err := client.Container(dagger.ContainerOpts{Platform: "linux/amd64"}).
|
|
From("liquibase/liquibase").
|
|
WithServiceBinding("sav-net-db", databaseDevelopment).
|
|
WithDirectory("/project/migrations", client.Host().Directory("./dbmigrations/migrations")).
|
|
WithFile("/project/liquibase.properties", client.Host().File("liquibase.properties")).
|
|
WithWorkdir("/project").
|
|
WithExec([]string{"sleep", "20"}).
|
|
WithExec([]string{"liquibase", "--defaultsFile=/project/liquibase.properties", "update"}).
|
|
Stdout(ctx)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
log.Printf("Correr Migraciones: \n%s", migrations)
|
|
|
|
client.Pipeline("Test de cobertura")
|
|
hostDirectory := client.Host().Directory(".")
|
|
containerApp := hostDirectory.
|
|
DockerBuild(dagger.DirectoryDockerBuildOpts{
|
|
Dockerfile: "/devops/.devops/development/DockerfileTest",
|
|
}).
|
|
WithWorkdir("/var/www/app").
|
|
WithServiceBinding("sav-net-db", databaseDevelopment).
|
|
WithExec([]string{"composer", "install"}).
|
|
|
|
addr, err := containerApp.
|
|
WithExec([]string{"vendor/bin/phpunit", "--coverage-clover", ".qa/coverage/report.xml", "/var/www/app/src"}).
|
|
Directory("/var/www/app/.qa").
|
|
Export(ctx, "./code/.qa")
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println("Se ejecutaron los test:", addr)
|
|
|
|
fmt.Println("Ejecutar sonarScanner")
|
|
sonarScanner, err := client.Container(dagger.ContainerOpts{Platform: "linux/amd64"}).
|
|
From("sonarsource/sonar-scanner-cli").
|
|
WithEnvVariable("URL", os.Getenv("HOST")).
|
|
WithEnvVariable(
|
|
"SONAR_SCANNER_OPTS",
|
|
"-Dsonar.projectKey=Savnet "+
|
|
"-Dsonar.exclusions=public/**/*,vendor/**/*,storage/**/*,tests/**/*,.qa/**/*,.devops/**/* "+
|
|
"-Dsonar.php.coverage.reportPaths=.qa/coverage/report.xml "+
|
|
"-Dsonar.php.tests.reportPath=.qa/coverage/report-tests.xml -Dsonar.projectBaseDir=/var/www/app").
|
|
WithEnvVariable("TOKEN", os.Getenv("SAV_DEVELOP")).
|
|
WithDirectory("/var/www/app/app", client.Host().Directory("./code/app")).
|
|
WithWorkdir("/var/www/app").
|
|
Stdout(ctx)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
log.Printf("sonarScanner: \n%s", sonarScanner)
|
|
|
|
time.Sleep(420 * time.Second)
|
|
|
|
dataDevelopment := PayloadDevelopment{
|
|
NombreApp: "SavNet",
|
|
EntornoApp: "Development",
|
|
}
|
|
|
|
fmt.Println("Quality Gate failed")
|
|
url := os.Getenv("HOST") + "project_status?projectKey=Savnet"
|
|
req, _ := http.NewRequest("GET", url, nil)
|
|
req.Header.Add("Authorization", os.Getenv("SAV_DEVELOP"))
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
log.Fatalf("Error executing request: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
var result struct {
|
|
ProjectStatus struct {
|
|
Status string `json:"status"`
|
|
} `json:"projectStatus"`
|
|
}
|
|
json.NewDecoder(resp.Body).Decode(&result)
|
|
if result.ProjectStatus.Status != "OK" {
|
|
url_ := os.Getenv("TOKEN_FAILED")
|
|
sendNotificationDevelopment(url_, dataDevelopment)
|
|
log.Fatal("Quality Gate failed!")
|
|
os.Exit(1)
|
|
}
|
|
fmt.Printf("Estado: %s\n", result)
|
|
|
|
image := hostDirectory.
|
|
DockerBuild(dagger.DirectoryDockerBuildOpts{
|
|
Dockerfile: "Dockerfile",
|
|
}).
|
|
WithWorkdir("/var/www/app").
|
|
WithExec([]string{"composer", "install"}).
|
|
WithEnvVariable("APP_VERSION", appVersion)
|
|
|
|
publishedVersion, err := image.
|
|
WithRegistryAuth(
|
|
os.Getenv("SAV_OST"),
|
|
os.Getenv("SAV_SER"),
|
|
secret).
|
|
Publish(ctx, os.Getenv("SAV_HOST")+"savnet:"+version)
|
|
|
|
fmt.Println("Enviando notificación a Rocket.Chat...")
|
|
url_ := os.Getenv("SAV_DEVELOPMENT")
|
|
chatMatriz := sendNotification(url_, dataDevelopment)
|
|
if err != nil {
|
|
fmt.Println("Error al enviar la notificación:", chatMatriz)
|
|
} else {
|
|
fmt.Println("Notificación enviada correctamente")
|
|
}
|
|
|
|
fmt.Println("Finalizo deploy")
|
|
return nil
|
|
}
|