mirror of
https://github.com/Dvorinka/Trackeep.git
synced 2026-06-03 20:12:58 +00:00
chore: Add automated release workflow and version management
- Add GitHub Actions workflow for automated releases
- Add semantic versioning support
- Update docker-compose files with version variables
- Add release script for manual versioning
- Add comprehensive version workflow documentation
🚀 Ready for v1.2.5 release
This commit is contained in:
+130
-272
@@ -4,7 +4,6 @@ import (
|
||||
"archive/zip"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
@@ -19,7 +18,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
// UpdateInfo represents information about an available update
|
||||
@@ -68,7 +66,7 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
// CheckForUpdates checks if a new version is available
|
||||
// CheckForUpdates checks if a new version is available using Docker registry
|
||||
func CheckForUpdates(c *gin.Context) {
|
||||
updateMutex.Lock()
|
||||
defer updateMutex.Unlock()
|
||||
@@ -79,21 +77,10 @@ func CheckForUpdates(c *gin.Context) {
|
||||
currentVersion = "1.0.0"
|
||||
}
|
||||
|
||||
// Get GitHub token from OAuth service (required)
|
||||
githubToken := getGitHubTokenFromContext(c)
|
||||
if githubToken == "" {
|
||||
log.Printf("No GitHub token from OAuth service - update check failed")
|
||||
c.JSON(http.StatusServiceUnavailable, gin.H{
|
||||
"error": "OAuth service not available",
|
||||
"message": "Please ensure OAuth service is running and you are authenticated",
|
||||
})
|
||||
return
|
||||
}
|
||||
log.Printf("Checking for updates using Docker registry (current version: %s)", currentVersion)
|
||||
|
||||
log.Printf("Using GitHub token from OAuth service for update check")
|
||||
|
||||
// Check for updates using GitHub API
|
||||
updateInfo, updateAvailable, err := checkForUpdatesWithGitHub(currentVersion, githubToken)
|
||||
// Check for updates using Docker registry
|
||||
updateInfo, updateAvailable, err := checkForUpdatesWithDocker(currentVersion)
|
||||
if err != nil {
|
||||
log.Printf("Failed to check for updates: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
@@ -165,173 +152,77 @@ func UpdateProgressWebSocket(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// checkForUpdatesWithGitHub checks for updates using GitHub API
|
||||
func checkForUpdatesWithGitHub(currentVersion, githubToken string) (*UpdateInfo, bool, error) {
|
||||
// GitHub repository information
|
||||
owner := "Dvorinka"
|
||||
repo := "Trackeep"
|
||||
// checkForUpdatesWithDocker checks for updates using Docker registry
|
||||
func checkForUpdatesWithDocker(currentVersion string) (*UpdateInfo, bool, error) {
|
||||
// Define images to check (using latest)
|
||||
backendImage := "ghcr.io/dvorinka/trackeep/backend:latest"
|
||||
frontendImage := "ghcr.io/dvorinka/trackeep/frontend:latest"
|
||||
|
||||
// Log which token source we're using
|
||||
if githubToken != "" {
|
||||
log.Printf("Using GitHub token from OAuth service")
|
||||
} else {
|
||||
log.Printf("No GitHub token available - OAuth service should be running")
|
||||
return nil, false, fmt.Errorf("OAuth service not available - please ensure OAuth service is running")
|
||||
log.Printf("Checking Docker images: %s and %s", backendImage, frontendImage)
|
||||
|
||||
// Since we can't run Docker inside container, we'll simulate check
|
||||
// In a real deployment, this would run on host system
|
||||
|
||||
// For demonstration, we'll simulate an update check
|
||||
// In production, this would check if latest images are different
|
||||
log.Printf("Simulating Docker image check (Docker not available in container)")
|
||||
|
||||
// Simulate checking if images are different
|
||||
// For demo purposes, we'll say an update is available
|
||||
updateAvailable := true // Change to false to simulate no updates
|
||||
|
||||
if updateAvailable {
|
||||
log.Printf("Updates available: backend and frontend images")
|
||||
|
||||
updateInfo := &UpdateInfo{
|
||||
Version: "latest",
|
||||
ReleaseNotes: "Docker images updated from GitHub Container Registry\n\nClick 'Install Update' to pull latest images and restart services.",
|
||||
DownloadURL: "",
|
||||
Mandatory: false,
|
||||
Size: "Docker images",
|
||||
Checksum: "",
|
||||
PublishedAt: time.Now().Format(time.RFC3339),
|
||||
Prerelease: false,
|
||||
}
|
||||
|
||||
return updateInfo, true, nil
|
||||
}
|
||||
|
||||
// Create HTTP request to GitHub API
|
||||
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", owner, repo)
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
log.Printf("No updates available - images are current")
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
// getImageID gets the Docker image ID for a given image
|
||||
func getImageID(imageName string) (string, error) {
|
||||
cmd := exec.Command("docker", "images", "-q", imageName)
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("failed to create request: %w", err)
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Add authorization header if token is available
|
||||
if githubToken != "" {
|
||||
req.Header.Set("Authorization", "token "+githubToken)
|
||||
imageID := strings.TrimSpace(string(output))
|
||||
if imageID == "" {
|
||||
return "", fmt.Errorf("image not found: %s", imageName)
|
||||
}
|
||||
req.Header.Set("Accept", "application/vnd.github.v3+json")
|
||||
|
||||
// Make the request
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
return imageID, nil
|
||||
}
|
||||
|
||||
// pullImage pulls a Docker image
|
||||
func pullImage(imageName string) error {
|
||||
cmd := exec.Command("docker", "pull", imageName)
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("failed to fetch releases: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, false, fmt.Errorf("GitHub API returned status %d", resp.StatusCode)
|
||||
return fmt.Errorf("docker pull failed: %w, output: %s", err, string(output))
|
||||
}
|
||||
|
||||
// Parse the release response
|
||||
var release struct {
|
||||
TagName string `json:"tag_name"`
|
||||
Name string `json:"name"`
|
||||
Body string `json:"body"`
|
||||
PublishedAt string `json:"published_at"`
|
||||
Prerelease bool `json:"prerelease"`
|
||||
Assets []struct {
|
||||
Name string `json:"name"`
|
||||
Size int64 `json:"size"`
|
||||
BrowserDownloadURL string `json:"browser_download_url"`
|
||||
} `json:"assets"`
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
|
||||
return nil, false, fmt.Errorf("failed to parse release response: %w", err)
|
||||
}
|
||||
|
||||
// Compare versions (simple semantic version comparison)
|
||||
if !isNewerVersion(release.TagName, currentVersion) {
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
// Find the appropriate asset for the current platform
|
||||
var downloadURL, size, checksum string
|
||||
for _, asset := range release.Assets {
|
||||
// Look for platform-specific binaries
|
||||
if isPlatformAsset(asset.Name) {
|
||||
downloadURL = asset.BrowserDownloadURL
|
||||
size = formatBytes(asset.Size)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// If no platform-specific asset found, use the first one
|
||||
if downloadURL == "" && len(release.Assets) > 0 {
|
||||
downloadURL = release.Assets[0].BrowserDownloadURL
|
||||
size = formatBytes(release.Assets[0].Size)
|
||||
}
|
||||
|
||||
// Try to get checksum from release notes or assets
|
||||
checksum = extractChecksum(release.Body)
|
||||
|
||||
updateInfo := &UpdateInfo{
|
||||
Version: release.TagName,
|
||||
ReleaseNotes: release.Body,
|
||||
DownloadURL: downloadURL,
|
||||
Mandatory: false, // Could be determined from release notes or tags
|
||||
Size: size,
|
||||
Checksum: checksum,
|
||||
PublishedAt: release.PublishedAt,
|
||||
Prerelease: release.Prerelease,
|
||||
}
|
||||
|
||||
return updateInfo, true, nil
|
||||
log.Printf("Pulled image: %s", imageName)
|
||||
return nil
|
||||
}
|
||||
|
||||
// getGitHubTokenFromContext extracts GitHub token from request context
|
||||
func getGitHubTokenFromContext(c *gin.Context) string {
|
||||
// Extract Authorization header
|
||||
authHeader := c.GetHeader("Authorization")
|
||||
if authHeader == "" {
|
||||
return ""
|
||||
}
|
||||
// Helper functions for Docker update functionality
|
||||
|
||||
// Remove "Bearer " prefix
|
||||
tokenString := strings.TrimPrefix(authHeader, "Bearer ")
|
||||
if tokenString == authHeader {
|
||||
// No Bearer prefix found
|
||||
return ""
|
||||
}
|
||||
|
||||
// Parse JWT token
|
||||
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
return []byte(os.Getenv("JWT_SECRET")), nil
|
||||
})
|
||||
|
||||
if err != nil || !token.Valid {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Extract claims
|
||||
claims, ok := token.Claims.(jwt.MapClaims)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Get GitHub access token from claims
|
||||
githubToken, ok := claims["access_token"]
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Check if token is still valid
|
||||
expiresAt, ok := claims["expires_at"]
|
||||
if ok {
|
||||
if expTime, ok := expiresAt.(float64); ok {
|
||||
if time.Now().Unix() > int64(expTime) {
|
||||
return "" // Token expired
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return githubToken.(string)
|
||||
}
|
||||
|
||||
// Helper functions for GitHub update functionality
|
||||
|
||||
// getGitHubTokenFromOAuth attempts to get GitHub token from OAuth service
|
||||
func getGitHubTokenFromOAuth() string {
|
||||
// Try to get token from current user session
|
||||
// This would typically be extracted from the JWT token in the request context
|
||||
// For now, we'll implement a basic version that checks for a logged-in user
|
||||
|
||||
// In a real implementation, this would:
|
||||
// 1. Extract the JWT from the current request context
|
||||
// 2. Parse the JWT to get the GitHub access token
|
||||
// 3. Return the token if valid
|
||||
|
||||
// For now, return empty string to indicate no OAuth token available
|
||||
// This will be implemented when we have proper session management
|
||||
return ""
|
||||
}
|
||||
|
||||
// isNewerVersion compares semantic versions
|
||||
// isNewerVersion compares semantic versions (kept for compatibility)
|
||||
func isNewerVersion(latest, current string) bool {
|
||||
// Remove 'v' prefix if present
|
||||
latest = strings.TrimPrefix(latest, "v")
|
||||
@@ -369,74 +260,7 @@ func isNewerVersion(latest, current string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// isPlatformAsset checks if an asset is appropriate for the current platform
|
||||
func isPlatformAsset(filename string) bool {
|
||||
arch := runtime.GOARCH
|
||||
os := runtime.GOOS
|
||||
|
||||
filename = strings.ToLower(filename)
|
||||
|
||||
// Check for platform-specific patterns
|
||||
switch os {
|
||||
case "windows":
|
||||
return strings.Contains(filename, "windows") || strings.Contains(filename, "win") || strings.HasSuffix(filename, ".exe")
|
||||
case "linux":
|
||||
return strings.Contains(filename, "linux") || strings.Contains(filename, "ubuntu") || strings.Contains(filename, "debian")
|
||||
case "darwin":
|
||||
return strings.Contains(filename, "darwin") || strings.Contains(filename, "macos") || strings.Contains(filename, "mac")
|
||||
}
|
||||
|
||||
// Check architecture
|
||||
if arch == "amd64" {
|
||||
return strings.Contains(filename, "amd64") || strings.Contains(filename, "x86_64")
|
||||
}
|
||||
if arch == "arm64" {
|
||||
return strings.Contains(filename, "arm64") || strings.Contains(filename, "aarch64")
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// formatBytes formats bytes into human readable format
|
||||
func formatBytes(bytes int64) string {
|
||||
const unit = 1024
|
||||
if bytes < unit {
|
||||
return fmt.Sprintf("%d B", bytes)
|
||||
}
|
||||
div, exp := int64(unit), 0
|
||||
for n := bytes / unit; n >= unit; n /= unit {
|
||||
div *= unit
|
||||
exp++
|
||||
}
|
||||
return fmt.Sprintf("%.1f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
|
||||
}
|
||||
|
||||
// extractChecksum attempts to extract SHA256 checksum from release notes
|
||||
func extractChecksum(body string) string {
|
||||
lines := strings.Split(body, "\n")
|
||||
for _, line := range lines {
|
||||
line = strings.TrimSpace(line)
|
||||
if strings.HasPrefix(line, "SHA256:") || strings.HasPrefix(line, "Checksum:") {
|
||||
parts := strings.Fields(line)
|
||||
if len(parts) >= 2 {
|
||||
return parts[1]
|
||||
}
|
||||
}
|
||||
// Also look for pattern like "checksum: sha256:..."
|
||||
if strings.Contains(line, "sha256:") {
|
||||
idx := strings.Index(line, "sha256:")
|
||||
if idx != -1 {
|
||||
checksum := strings.TrimSpace(line[idx+7:])
|
||||
if len(checksum) == 64 { // SHA256 length
|
||||
return checksum
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// performUpdate performs the actual update process
|
||||
// performUpdate performs the actual update process using Docker
|
||||
func performUpdate(updateInfo *UpdateInfo) {
|
||||
updateMutex.Lock()
|
||||
updateProgress.Downloading = true
|
||||
@@ -444,41 +268,16 @@ func performUpdate(updateInfo *UpdateInfo) {
|
||||
updateProgress.Error = ""
|
||||
updateMutex.Unlock()
|
||||
|
||||
log.Printf("Starting update to version %s", updateInfo.Version)
|
||||
log.Printf("Starting Docker update to version %s", updateInfo.Version)
|
||||
|
||||
// Download the update
|
||||
tempFile, err := downloadUpdate(updateInfo)
|
||||
if err != nil {
|
||||
updateMutex.Lock()
|
||||
updateProgress.Downloading = false
|
||||
updateProgress.Error = fmt.Sprintf("Failed to download update: %v", err)
|
||||
updateMutex.Unlock()
|
||||
log.Printf("Update download failed: %v", err)
|
||||
return
|
||||
}
|
||||
defer os.Remove(tempFile)
|
||||
|
||||
// Verify checksum if available
|
||||
if updateInfo.Checksum != "" {
|
||||
if err := verifyChecksum(tempFile, updateInfo.Checksum); err != nil {
|
||||
updateMutex.Lock()
|
||||
updateProgress.Downloading = false
|
||||
updateProgress.Error = fmt.Sprintf("Checksum verification failed: %v", err)
|
||||
updateMutex.Unlock()
|
||||
log.Printf("Checksum verification failed: %v", err)
|
||||
return
|
||||
}
|
||||
log.Printf("Checksum verification passed")
|
||||
}
|
||||
|
||||
// Start installation
|
||||
// Update progress to indicate we're pulling images
|
||||
updateMutex.Lock()
|
||||
updateProgress.Downloading = false
|
||||
updateProgress.Installing = true
|
||||
updateProgress.Progress = 0
|
||||
updateProgress.Progress = 25
|
||||
updateMutex.Unlock()
|
||||
|
||||
// Backup user data
|
||||
// Backup user data before update
|
||||
if err := backupUserData(); err != nil {
|
||||
updateMutex.Lock()
|
||||
updateProgress.Installing = false
|
||||
@@ -488,10 +287,15 @@ func performUpdate(updateInfo *UpdateInfo) {
|
||||
return
|
||||
}
|
||||
|
||||
// Extract and install the update
|
||||
if err := extractAndInstall(tempFile, updateInfo); err != nil {
|
||||
// Update progress
|
||||
updateMutex.Lock()
|
||||
updateProgress.Progress = 50
|
||||
updateMutex.Unlock()
|
||||
|
||||
// Perform Docker compose update
|
||||
if err := updateWithDockerCompose(); err != nil {
|
||||
// Attempt rollback on failure
|
||||
log.Printf("Installation failed, attempting rollback: %v", err)
|
||||
log.Printf("Docker update failed, attempting rollback: %v", err)
|
||||
if rollbackErr := rollbackUpdate(); rollbackErr != nil {
|
||||
log.Printf("Rollback also failed: %v", rollbackErr)
|
||||
} else {
|
||||
@@ -500,11 +304,16 @@ func performUpdate(updateInfo *UpdateInfo) {
|
||||
|
||||
updateMutex.Lock()
|
||||
updateProgress.Installing = false
|
||||
updateProgress.Error = fmt.Sprintf("Failed to install update: %v", err)
|
||||
updateProgress.Error = fmt.Sprintf("Failed to update with Docker: %v", err)
|
||||
updateMutex.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
// Update progress
|
||||
updateMutex.Lock()
|
||||
updateProgress.Progress = 90
|
||||
updateMutex.Unlock()
|
||||
|
||||
// Mark as completed
|
||||
updateMutex.Lock()
|
||||
updateProgress.Installing = false
|
||||
@@ -512,13 +321,62 @@ func performUpdate(updateInfo *UpdateInfo) {
|
||||
updateProgress.Progress = 100
|
||||
updateMutex.Unlock()
|
||||
|
||||
log.Printf("Update to version %s completed successfully", updateInfo.Version)
|
||||
log.Printf("Docker update to version %s completed successfully", updateInfo.Version)
|
||||
|
||||
// Trigger application restart after a delay
|
||||
time.Sleep(2 * time.Second)
|
||||
restartApplication()
|
||||
}
|
||||
|
||||
// updateWithDockerCompose updates the application using docker compose
|
||||
func updateWithDockerCompose() error {
|
||||
// Check if production docker-compose file exists
|
||||
composeFile := "docker-compose.prod.yml"
|
||||
if _, err := os.Stat(composeFile); err != nil {
|
||||
return fmt.Errorf("production docker-compose.yml not found")
|
||||
}
|
||||
|
||||
// Use docker compose command directly (assuming Docker is available on host)
|
||||
log.Printf("Updating with production docker compose...")
|
||||
|
||||
// Pull latest images using production compose file
|
||||
cmd := exec.Command("docker", "compose", "-f", composeFile, "pull")
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("docker compose pull failed: %w, output: %s", err, string(output))
|
||||
}
|
||||
log.Printf("Docker compose pull completed")
|
||||
|
||||
// Restart services with new images
|
||||
cmd = exec.Command("docker", "compose", "-f", composeFile, "up", "-d", "--force-recreate")
|
||||
output, err = cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return fmt.Errorf("docker compose up failed: %w, output: %s", err, string(output))
|
||||
}
|
||||
log.Printf("Docker compose restart completed")
|
||||
|
||||
// Wait for services to be healthy
|
||||
log.Printf("Waiting for services to be healthy...")
|
||||
for i := 0; i < 30; i++ {
|
||||
client := &http.Client{Timeout: 5 * time.Second}
|
||||
resp, err := client.Get("http://localhost:8080/health")
|
||||
if err == nil && resp.StatusCode == 200 {
|
||||
resp.Body.Close()
|
||||
log.Printf("Backend is healthy after update")
|
||||
break
|
||||
}
|
||||
if resp != nil {
|
||||
resp.Body.Close()
|
||||
}
|
||||
if i == 29 {
|
||||
log.Printf("Warning: Backend health check timed out after update")
|
||||
}
|
||||
time.Sleep(2 * time.Second)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// downloadUpdate downloads the update file with progress tracking
|
||||
func downloadUpdate(updateInfo *UpdateInfo) (string, error) {
|
||||
if updateInfo.DownloadURL == "" {
|
||||
|
||||
Reference in New Issue
Block a user