forked from acoronadoc/python-deployment-script-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline.go
More file actions
85 lines (72 loc) · 2.47 KB
/
pipeline.go
File metadata and controls
85 lines (72 loc) · 2.47 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"os/exec"
"strings"
)
// execCmd runs a shell command in a specified directory and returns its output
func execCmd(cmd []string, workingDir string, returnJSON bool) ([]byte, error) {
// Create the command
command := exec.Command(cmd[0], cmd[1:]...)
command.Dir = workingDir
// Capture standard output and error
var stdout, stderr bytes.Buffer
command.Stdout = &stdout
command.Stderr = &stderr
err := command.Run()
if err != nil {
fmt.Printf("\nERROR EXECUTING TASK: %s\nSTDOUT: %s\nSTDERR: %s\n\n", cmd, stdout.String(), stderr.String())
return nil, err
}
if returnJSON {
return json.Marshal(stdout.String())
}
return stdout.Bytes(), nil
}
// pipelineStage executes a specific stage in the pipeline and logs any errors
func pipelineStage(cmd []string, workingDir string) error {
fmt.Printf("Executing stage: %s\n", strings.Join(cmd, " "))
_, err := execCmd(cmd, workingDir, false)
if err != nil {
fmt.Printf("Error in stage %s: %v\n", cmd[0], err)
}
return err
}
// processPipeline performs each stage of the pipeline for deploying the application
func processPipeline() {
// Step 1: Remove the previous directory if it exists
if err := pipelineStage([]string{"rm", "-R", "-f", "files"}, "."); err != nil {
return // Stop if this step fails
}
// Step 2: Create a new directory for cloning the repo
if err := pipelineStage([]string{"mkdir", "files"}, "."); err != nil {
return // Stop if this step fails
}
// Step 3: Clone the repository into the new directory
if err := pipelineStage([]string{"git", "clone", RepoLink, "files"}, "."); err != nil {
return // Stop if this step fails
}
// Step 4: Launch Docker containers with docker-compose
if err := pipelineStage([]string{"docker-compose", "up", "--build", "-d"}, "files"); err != nil {
return // Stop if this step fails
}
// Additional steps can be added here using pipelineStage
}
// fetchLatestCommit retrieves the latest commit from the specified branch in the remote repository
func fetchLatestCommit() (string, error) {
// Get the list of remote commits
r, err := execCmd([]string{"git", "ls-remote", "--heads", RepoLink}, ".", false)
if err != nil {
return "", err
}
// Look for the commit in the specified branch
commits := strings.Split(string(r), "\n")
for _, line := range commits {
if strings.HasSuffix(line, "refs/heads/"+BranchName) {
return strings.Fields(line)[0], nil
}
}
return "", fmt.Errorf("no branch commit found")
}