-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
103 lines (88 loc) · 2.72 KB
/
main.go
File metadata and controls
103 lines (88 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"cno-ui-ci/cno/models"
cno "cno-ui-ci/cno/services"
"cno-ui-ci/config"
"context"
"errors"
"fmt"
"os"
"path/filepath"
"dagger.io/dagger"
"github.com/spf13/viper"
"k8s.io/client-go/util/homedir"
)
const (
GIT_REPOSITORY_URL = "git@github.com:beopencloud/cno-api.git"
GIT_BRANCH = "develop"
PUBLISH_ADDRESS = "beopenit/cno-api"
IMAGE_TAG = "v1.123"
NAMESPACE = "ali-ka"
DEPLOYMENT_NAME = "cno-api"
DEPLOYMENT_FILE_NAME = "cno-api-deploy.yml"
ENVIRONMENT_NAME = "dev"
PROJECT_NAME = "develop"
WORKLOAD_NAME = "cno-api"
)
func main() {
ctx := context.Background()
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))
if err != nil {
fmt.Println("Cannot connect to dagger client : ", err)
}
defer client.Close()
codeSrc := client.Git(GIT_REPOSITORY_URL).Branch(GIT_BRANCH).Tree()
goBaseImage := client.Container().From("golang:latest").WithMountedDirectory("/src", codeSrc).WithWorkdir("/src")
resultTest := goBaseImage.Exec(dagger.ContainerExecOpts{
Args: []string{"go", "test", "./..."},
})
fmt.Println(resultTest.Stdout().Contents(ctx))
goApp := client.Container().Build(codeSrc)
address, err := goApp.Publish(ctx, PUBLISH_ADDRESS+":"+IMAGE_TAG)
if err != nil {
fmt.Println("Could not publish container: ", err)
os.Exit(1)
}
fmt.Println("Container pushed at ", address)
err = setUpConfig()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
token, err := cno.Login(models.Credentials{Username: config.CNO_USERNAME, Password: config.CNO_PASSWORD, OrgName: config.CNO_ORGANIZATION_NAME})
if err != nil {
fmt.Println("Could not login :", err)
os.Exit(1)
}
workload, err := cno.GetWorkload(WORKLOAD_NAME, ENVIRONMENT_NAME, PROJECT_NAME)
if err != nil {
fmt.Println("Could not get workload")
os.Exit(1)
}
workloadPatch := models.WorkloadPatchSpec{LiveContainers: []models.Container{{Image: PUBLISH_ADDRESS + ":" + IMAGE_TAG}}, AutoDeploy: true}
err = cno.PatchWorkload(*workload, workloadPatch)
if err != nil {
fmt.Println("Could not patch workload:", err)
os.Exit(1)
}
fmt.Println("Workload deployed")
}
func setUpConfig() error {
home := homedir.HomeDir()
viper.AddConfigPath(filepath.Join(home, ".cno"))
viper.SetConfigType("yaml")
viper.SetConfigName("config")
if _, err := os.Stat(filepath.Join(home, ".cno/config")); os.IsNotExist(err) {
os.MkdirAll(filepath.Join(home, ".cno"), os.ModePerm)
_, err := os.Create(filepath.Join(home, ".cno/config"))
if err != nil {
err = errors.New("error to create file config: " + err.Error())
return err
}
}
if err := viper.ReadInConfig(); err != nil {
err = errors.New("Using Config file error: " + err.Error())
return err
}
return nil
}