This repository has been archived by the owner on Aug 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
96 lines (83 loc) · 2.59 KB
/
main.go
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
/*
Copyright (c) 2020 white duck Gesellschaft für Softwareentwicklung mbH
This code is licensed under MIT license (see LICENSE for details)
*/
package main
import (
"context"
"fmt"
"os"
"os/signal"
"github.com/sirupsen/logrus"
"github.com/whiteducksoftware/azure-arm-action/pkg/github"
"github.com/whiteducksoftware/azure-arm-action/pkg/github/actions"
"github.com/whiteducksoftware/golang-utilities/github/actions/io"
)
func init() {
lvl, ok := os.LookupEnv("LOG_LEVEL")
// LOG_LEVEL not set, let's default to info
if !ok {
lvl = "info"
}
// parse string, this is built-in feature of logrus
ll, err := logrus.ParseLevel(lvl)
if err != nil {
ll = logrus.InfoLevel
}
// set global log level
logrus.SetLevel(ll)
}
func main() {
opts, err := github.LoadOptions()
if err != nil {
logrus.Errorf("failed to load options: %s", err.Error())
io.WriteError(io.Message{Message: fmt.Sprintf("failed to load options: %s", err.Error())})
os.Exit(1)
}
// read inptus
ctx, cancel := context.WithTimeout(context.Background(), opts.Timeout)
setupInterruptHandler(cancel)
// Output some information
if opts.RunningAsAction {
logrus.Infof("==== Running workflow %s for %s@%s ====", opts.Workflow, opts.Ref, opts.Commit)
}
// authenticate
authorizer, err := actions.Authenticate(opts)
if err != nil {
logrus.Errorf("Failed to authenticate with azure: %s", err.Error())
io.WriteError(io.Message{Message: fmt.Sprintf("Failed to authenticate with azure: %s", err.Error())})
os.Exit(1)
}
// deploy the template
resultDeployment, err := actions.Deploy(ctx, opts, authorizer)
if err != nil {
logrus.Errorf("Failed to deploy the template: %s", err.Error())
io.WriteError(io.Message{Message: fmt.Sprintf("Failed to deploy the template: %s", err.Error())})
os.Exit(1)
}
// parse the template outputs
outputs, err := actions.ParseOutputs(resultDeployment.Properties.Outputs)
if err != nil {
logrus.Errorf("Failed to parse the template outputs: %s", err.Error())
io.WriteError(io.Message{Message: fmt.Sprintf("Failed to parse the template outputs: %s", err.Error())})
os.Exit(1)
}
// write the outputs and the deploymentName to our outputs
io.SetOutput("deploymentName", *resultDeployment.Name)
for name, output := range outputs {
io.SetOutput(name, output.Value)
}
if opts.RunningAsAction {
logrus.Info("==== Successfully finished running the workflow ====")
}
}
func setupInterruptHandler(cancel func()) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c // wait for the signal
logrus.Info("Received interrupt, exiting now...")
cancel()
os.Exit(1)
}()
}