Skip to content

Commit

Permalink
Add config file and add go build file
Browse files Browse the repository at this point in the history
  • Loading branch information
OnCloud125252 committed Sep 6, 2023
1 parent cd87d1d commit de1c8fe
Show file tree
Hide file tree
Showing 7 changed files with 581 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Dockerfiles/*
logs/*
production
_production
2 changes: 2 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
port: 8000
path: /auto-deploy
6 changes: 3 additions & 3 deletions builder.sh → docker_builder.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ fi
# Check if a container with the given ID already exists
if [[ "$(docker ps -a -q -f name=$REPO_ID)" ]]; then
echo "Container with ID $REPO_ID already exists. Updating it..."

docker stop $REPO_ID &>/dev/null && docker rm $REPO_ID &>/dev/null

docker build --no-cache -t $REPO_ID -f Dockerfiles/Dockerfile-$REPO_ID .
docker run --name $REPO_ID --network auto-deploy --restart unless-stopped -d -p 9000 $REPO_ID &>/dev/null
else
echo "Container with ID $REPO_ID does not exists. Creating new one..."

docker build --no-cache -t $REPO_ID -f Dockerfiles/Dockerfile-$REPO_ID .
docker run --name $REPO_ID --network auto-deploy --restart unless-stopped -d -p 9000 $REPO_ID &>/dev/null
fi
19 changes: 18 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,21 @@ module Autodeploy

go 1.21.0

require github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
require (
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.16.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
472 changes: 472 additions & 0 deletions go.sum

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions go_build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash

# Colors
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
RED='\033[0;31m'
BLUE='\033[0;34m'
ORANGE='\033[0;33m'
MAGENTA='\033[0;35m'
NC='\033[0m' # No Color

# Symbols
CHECKMARK='\xE2\x9C\x94'
ROCKET='\xF0\x9F\x9A\x80'
CROSSMARK='\xE2\x9C\x98'
HOURGLASS='\xE2\x8C\x9B'

# Check if Go module is enabled
if go env GOMOD &> /dev/null; then
echo -e "${BLUE}${HOURGLASS} Go module is enabled. Now building your program...${NC}"
else
echo -e "${RED}${CROSSMARK} Go module is not enabled. Please enable it before continuing.${NC}"
exit 1
fi

# Clean up build folder if it exists
if [ -d "_production" ]; then
rm -rf "_production"
fi
mkdir -p "_production"

# Build program to production folder
go build -o "_production"

# Get the executable file name
executable=$(basename "_production"/*)
echo ""
echo -e "${GREEN}Build success!${NC}"

# Copy dependencies
echo ""
if [ $# -gt 0 ]; then
echo -e "${MAGENTA}Copying dependencies...${NC}"
for arg in "$@"; do
if [ -e "$arg" ]; then
cp -r "$arg" "_production/"
echo -e " ${MAGENTA}${CHECKMARK} $arg${NC}"
else
echo -e " ${RED}${CROSSMARK} File or directory not found: $arg${NC}"
fi
done
echo ""
echo -e "${GREEN}All operations completed successfully!${NC}"
else
echo -e "${ORANGE}Warning: No dependencies provided.${NC}"
fi

echo ""
echo -e "${ROCKET} You can now use ${BLUE}./_production/$executable${NC} to execute your program."
30 changes: 26 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/acarl005/stripansi"
"github.com/spf13/viper"
"io/ioutil"
"log"
"net/http"
Expand All @@ -12,9 +13,6 @@ import (
"strconv"
)

var port string = "8000"
var path string = "/auto-deploy"

type Payload struct {
Repository struct {
ID int `json:"id"`
Expand All @@ -23,7 +21,31 @@ type Payload struct {
} `json:"repository"`
}

var defaultPort = "8000"
var defaultPath = "/auto-deploy"

func main() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath("./")

err := viper.ReadInConfig()
if err != nil {
fmt.Println("No config file found. Using default values.")
} else {
fmt.Println("Config file found. Using user defined values.")
}

port := viper.GetString("port")
if port == "" {
port = defaultPort
}

path := viper.GetString("path")
if path == "" {
path = defaultPath
}

http.HandleFunc(path, payloadHandler)

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

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

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

0 comments on commit de1c8fe

Please sign in to comment.