From 808907482968988d0b21c6e13de9594593e31974 Mon Sep 17 00:00:00 2001 From: balakatumtum Date: Mon, 8 Jan 2024 13:27:23 +0700 Subject: [PATCH] feat: add azure support and workflow --- .env.example | 2 +- .github/workflows/azure_dev.yml | 29 ++++++++++++++++++++++ build/web.config | 10 ++++++++ constants/common.go | 4 +-- docker-compose.yml | 43 +++++---------------------------- main.go | 14 ++++++++++- utils/azure/azure.go | 39 ++++++++++++++++++++++++++++++ 7 files changed, 100 insertions(+), 41 deletions(-) create mode 100644 .github/workflows/azure_dev.yml create mode 100644 build/web.config create mode 100644 utils/azure/azure.go diff --git a/.env.example b/.env.example index 7b3caf4..cc6fb45 100644 --- a/.env.example +++ b/.env.example @@ -5,7 +5,7 @@ DB_NAME = DB_PORT = 5432 NGINX_PORT=8080 -GOLANG_PORT=8888 +HTTP_PLATFORM_PORT=8888 SMTP_HOST=smtp.gmail.com SMTP_PORT=587 diff --git a/.github/workflows/azure_dev.yml b/.github/workflows/azure_dev.yml new file mode 100644 index 0000000..71cb322 --- /dev/null +++ b/.github/workflows/azure_dev.yml @@ -0,0 +1,29 @@ +name: Build Go app and push to Azure (Development) + +on: + push: + branches: [ main ] + +jobs: + + build: + runs-on: windows-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: 1.18 + + - name: Build + run: go build -o build/go-app.exe -v + + - name: Deploy to Azure + uses: Azure/webapps-deploy@v2 + with: + # Name of the Azure Web App + app-name: tedxits-dev + # Applies to Web App only: Path to package or folder. *.zip, *.war, *.jar or a folder to deploy + package: build/ + publish-profile: ${{ secrets.AZUREWEBAPPPUBLISHPROFILEDEVELOPMENT }} \ No newline at end of file diff --git a/build/web.config b/build/web.config new file mode 100644 index 0000000..85fd372 --- /dev/null +++ b/build/web.config @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/constants/common.go b/constants/common.go index ad0523e..91af6f9 100644 --- a/constants/common.go +++ b/constants/common.go @@ -4,8 +4,8 @@ const ( ENUM_ROLE_ADMIN = "admin" ENUM_ROLE_USER = "user" - ENUM_RUN_PRODUCTION = "production" - ENUM_RUN_TESTING = "testing" + ENUM_RUN_PRODUCTION = "production" + ENUM_RUN_DEVELOPMENT = "development" ENUM_PAGINATION_LIMIT = 10 ENUM_PAGINATION_PAGE = 1 diff --git a/docker-compose.yml b/docker-compose.yml index bdc797c..8cc5e91 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,41 +1,10 @@ -version: '3.9' - services: postgres: - hostname: postgres - image: postgres:latest - ports: - - ${DB_PORT}:5432 - volumes: - - ./volumes/postgres:/var/lib/postgresql/data + container_name: tedxits2024 + image: postgres environment: - - POSTGRES_USER=${DB_USER} - - POSTGRES_PASSWORD=${DB_PASS} - - POSTGRES_DB=${DB_NAME} - networks: - - app-network - - app: - hostname: app - container_name: golang-clean-template - build: - context: . - dockerfile: Dockerfile + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=password + - POSTGRES_DB=tedxits2024 ports: - - ${GOLANG_PORT}:${GOLANG_PORT} - restart: always - volumes: - - ./:/app - depends_on: - - postgres - env_file: - - .env - networks: - - app-network - -volumes: - app_vol: - -networks: - app-network: - driver: bridge + - 5432:5432 \ No newline at end of file diff --git a/main.go b/main.go index 92e34c0..850d19b 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "github.com/TEDxITS/website-backend-2024/repository" "github.com/TEDxITS/website-backend-2024/routes" "github.com/TEDxITS/website-backend-2024/service" + "github.com/TEDxITS/website-backend-2024/utils/azure" "github.com/gin-gonic/gin" "gorm.io/gorm" @@ -45,7 +46,7 @@ func main() { log.Fatalf("error migration seeder: %v", err) } - port := os.Getenv("GOLANG_PORT") + port := os.Getenv("HTTP_PLATFORM_PORT") if port == "" { port = "8888" } @@ -54,4 +55,15 @@ func main() { if err := server.Run(":" + port); err != nil { log.Fatalf("error running server: %v", err) } + + /* + Deployed on Azure App Service with .NET Stack. + The workflow will failed to deploy on updates + because the server is already running and it + wont lets us replace it. Normally in .NET apps + on azure, it will create a file called "app_offline.htm" + and the ASP .NET will notice it the file is created + and stop the application. This replicate said behavior. + */ + azure.StopOnNewDeployment() } diff --git a/utils/azure/azure.go b/utils/azure/azure.go new file mode 100644 index 0000000..8733d2e --- /dev/null +++ b/utils/azure/azure.go @@ -0,0 +1,39 @@ +package azure + +import ( + "fmt" + "os" + "strings" + + "github.com/fsnotify/fsnotify" +) + +func StopOnNewDeployment() { + // creates a new file watcher for App_offline.htm + watcher, err := fsnotify.NewWatcher() + if err != nil { + fmt.Println("ERROR", err) + } + defer watcher.Close() + + // watch for App_offline.htm and exit the program if present + // This allows continuous deployment on App Service as the .exe will not be + // terminated otherwise + go func() { + for { + select { + case event := <-watcher.Events: + if strings.HasSuffix(event.Name, "app_offline.htm") { + fmt.Println("Exiting due to app_offline.htm being present") + os.Exit(0) + } + } + } + }() + + // get the current working directory and watch it + currentDir, err := os.Getwd() + if err := watcher.Add(currentDir); err != nil { + fmt.Println("ERROR", err) + } +}