Skip to content

Commit de1c8fe

Browse files
committed
Add config file and add go build file
1 parent cd87d1d commit de1c8fe

File tree

7 files changed

+581
-9
lines changed

7 files changed

+581
-9
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Dockerfiles/*
22
logs/*
3-
production
3+
_production

config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
port: 8000
2+
path: /auto-deploy

builder.sh renamed to docker_builder.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ fi
3434
# Check if a container with the given ID already exists
3535
if [[ "$(docker ps -a -q -f name=$REPO_ID)" ]]; then
3636
echo "Container with ID $REPO_ID already exists. Updating it..."
37-
37+
3838
docker stop $REPO_ID &>/dev/null && docker rm $REPO_ID &>/dev/null
39-
39+
4040
docker build --no-cache -t $REPO_ID -f Dockerfiles/Dockerfile-$REPO_ID .
4141
docker run --name $REPO_ID --network auto-deploy --restart unless-stopped -d -p 9000 $REPO_ID &>/dev/null
4242
else
4343
echo "Container with ID $REPO_ID does not exists. Creating new one..."
44-
44+
4545
docker build --no-cache -t $REPO_ID -f Dockerfiles/Dockerfile-$REPO_ID .
4646
docker run --name $REPO_ID --network auto-deploy --restart unless-stopped -d -p 9000 $REPO_ID &>/dev/null
4747
fi

go.mod

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,21 @@ module Autodeploy
22

33
go 1.21.0
44

5-
require github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
5+
require (
6+
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
7+
github.com/fsnotify/fsnotify v1.6.0 // indirect
8+
github.com/hashicorp/hcl v1.0.0 // indirect
9+
github.com/magiconair/properties v1.8.7 // indirect
10+
github.com/mitchellh/mapstructure v1.5.0 // indirect
11+
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
12+
github.com/spf13/afero v1.9.5 // indirect
13+
github.com/spf13/cast v1.5.1 // indirect
14+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
15+
github.com/spf13/pflag v1.0.5 // indirect
16+
github.com/spf13/viper v1.16.0 // indirect
17+
github.com/subosito/gotenv v1.4.2 // indirect
18+
golang.org/x/sys v0.8.0 // indirect
19+
golang.org/x/text v0.9.0 // indirect
20+
gopkg.in/ini.v1 v1.67.0 // indirect
21+
gopkg.in/yaml.v3 v3.0.1 // indirect
22+
)

go.sum

Lines changed: 472 additions & 0 deletions
Large diffs are not rendered by default.

go_build.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash
2+
3+
# Colors
4+
GREEN='\033[0;32m'
5+
YELLOW='\033[0;33m'
6+
RED='\033[0;31m'
7+
BLUE='\033[0;34m'
8+
ORANGE='\033[0;33m'
9+
MAGENTA='\033[0;35m'
10+
NC='\033[0m' # No Color
11+
12+
# Symbols
13+
CHECKMARK='\xE2\x9C\x94'
14+
ROCKET='\xF0\x9F\x9A\x80'
15+
CROSSMARK='\xE2\x9C\x98'
16+
HOURGLASS='\xE2\x8C\x9B'
17+
18+
# Check if Go module is enabled
19+
if go env GOMOD &> /dev/null; then
20+
echo -e "${BLUE}${HOURGLASS} Go module is enabled. Now building your program...${NC}"
21+
else
22+
echo -e "${RED}${CROSSMARK} Go module is not enabled. Please enable it before continuing.${NC}"
23+
exit 1
24+
fi
25+
26+
# Clean up build folder if it exists
27+
if [ -d "_production" ]; then
28+
rm -rf "_production"
29+
fi
30+
mkdir -p "_production"
31+
32+
# Build program to production folder
33+
go build -o "_production"
34+
35+
# Get the executable file name
36+
executable=$(basename "_production"/*)
37+
echo ""
38+
echo -e "${GREEN}Build success!${NC}"
39+
40+
# Copy dependencies
41+
echo ""
42+
if [ $# -gt 0 ]; then
43+
echo -e "${MAGENTA}Copying dependencies...${NC}"
44+
for arg in "$@"; do
45+
if [ -e "$arg" ]; then
46+
cp -r "$arg" "_production/"
47+
echo -e " ${MAGENTA}${CHECKMARK} $arg${NC}"
48+
else
49+
echo -e " ${RED}${CROSSMARK} File or directory not found: $arg${NC}"
50+
fi
51+
done
52+
echo ""
53+
echo -e "${GREEN}All operations completed successfully!${NC}"
54+
else
55+
echo -e "${ORANGE}Warning: No dependencies provided.${NC}"
56+
fi
57+
58+
echo ""
59+
echo -e "${ROCKET} You can now use ${BLUE}./_production/$executable${NC} to execute your program."

main.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"github.com/acarl005/stripansi"
7+
"github.com/spf13/viper"
78
"io/ioutil"
89
"log"
910
"net/http"
@@ -12,9 +13,6 @@ import (
1213
"strconv"
1314
)
1415

15-
var port string = "8000"
16-
var path string = "/auto-deploy"
17-
1816
type Payload struct {
1917
Repository struct {
2018
ID int `json:"id"`
@@ -23,7 +21,31 @@ type Payload struct {
2321
} `json:"repository"`
2422
}
2523

24+
var defaultPort = "8000"
25+
var defaultPath = "/auto-deploy"
26+
2627
func main() {
28+
viper.SetConfigName("config")
29+
viper.SetConfigType("yaml")
30+
viper.AddConfigPath("./")
31+
32+
err := viper.ReadInConfig()
33+
if err != nil {
34+
fmt.Println("No config file found. Using default values.")
35+
} else {
36+
fmt.Println("Config file found. Using user defined values.")
37+
}
38+
39+
port := viper.GetString("port")
40+
if port == "" {
41+
port = defaultPort
42+
}
43+
44+
path := viper.GetString("path")
45+
if path == "" {
46+
path = defaultPath
47+
}
48+
2749
http.HandleFunc(path, payloadHandler)
2850

2951
fmt.Println("Server listening on port " + port + " ...")
@@ -63,7 +85,7 @@ func payloadHandler(w http.ResponseWriter, r *http.Request) {
6385

6486
// Execute build process in a goroutine
6587
go func() {
66-
buildContainer := exec.Command("./builder.sh", "REPO_URL="+RepoURL, "DEFAULT_BRANCH="+DefaultBranch, "REPO_ID="+RepoID)
88+
buildContainer := exec.Command("./docker_builder.sh", "REPO_URL="+RepoURL, "DEFAULT_BRANCH="+DefaultBranch, "REPO_ID="+RepoID)
6789

6890
logFile, buildContainerError := os.Create("./logs/log-" + RepoID + ".log")
6991
if buildContainerError != nil {

0 commit comments

Comments
 (0)