Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8665ea22e1 | |||
| 93b35eb1d9 | |||
| d0a8c5b3a8 | |||
| d3530314c2 | |||
| 64a96fa4cd |
@@ -0,0 +1,21 @@
|
||||
steps:
|
||||
- name: build and push docker image
|
||||
image: woodpeckerci/plugin-docker-buildx
|
||||
settings:
|
||||
repo: git.kolspace.cc/victor.kolomin/controlla
|
||||
registry: git.kolspace.cc
|
||||
platforms: linux/amd64
|
||||
username:
|
||||
from_secret: gitea_registry_user
|
||||
password:
|
||||
from_secret: gitea_registry_pass
|
||||
tags:
|
||||
- ${CI_COMMIT_TAG}
|
||||
- latest
|
||||
cache_from:
|
||||
- git.kolspace.cc/victor.kolomin/controlla:latest
|
||||
cache_to: type=inline
|
||||
|
||||
when:
|
||||
event:
|
||||
- tag
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
FROM public.ecr.aws/docker/library/golang:1.25.1-alpine3.22 AS builder
|
||||
FROM docker.io/library/golang:1.25.1-alpine3.22 AS builder
|
||||
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
@@ -7,7 +7,7 @@ COPY . .
|
||||
RUN go build -o controlla .
|
||||
|
||||
# Final minimal image
|
||||
FROM public.ecr.aws/docker/library/alpine:3.22.1
|
||||
FROM docker.io/library/alpine:3.22.1
|
||||
WORKDIR /app
|
||||
|
||||
# Copy the binary and config file from the builder stage
|
||||
|
||||
+34
-22
@@ -14,12 +14,38 @@ import (
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// UpdateAndPushService updates the image tag of a specified service in a docker-compose.yaml file,
|
||||
// commits the change to a Git repository, and pushes the commit to the remote repository.
|
||||
func UpdateAndPushService(repoPath, filePath, serviceName, newTag, SSHKeyPath, authorName, authorEmail string) error {
|
||||
// UpdateAndPushService updates a service's image tag in its docker-compose file and pushes the change to the Git repository.
|
||||
func UpdateAndPushService(repoPath, filePath, serviceName, newTag, sshKeyPath, authorName, authorEmail string) error {
|
||||
logrus.Infof("Starting update for service '%s' in file '%s' to tag '%s'", serviceName, filePath, newTag)
|
||||
|
||||
// Reading and updating the docker-compose.yaml file
|
||||
// --- CORRECT LOGIC: Open repo and pull first to ensure we are up-to-date ---
|
||||
repo, err := git.PlainOpen(repoPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not open repository: %w", err)
|
||||
}
|
||||
|
||||
worktree, err := repo.Worktree()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get worktree: %w", err)
|
||||
}
|
||||
|
||||
auth, err := ssh.NewPublicKeysFromFile("git", sshKeyPath, "")
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot generate public keys from file: %w", err)
|
||||
}
|
||||
|
||||
logrus.Info("Pulling latest changes before modifying files...")
|
||||
pullOpts := &git.PullOptions{
|
||||
RemoteName: "origin",
|
||||
Auth: auth,
|
||||
}
|
||||
err = worktree.Pull(pullOpts)
|
||||
if err != nil && err != git.NoErrAlreadyUpToDate {
|
||||
return fmt.Errorf("could not pull changes: %w", err)
|
||||
}
|
||||
// --- END CORRECT LOGIC ---
|
||||
|
||||
// 1. Reading and updating the docker-compose.yaml file
|
||||
yamlFile, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not read file: %w", err)
|
||||
@@ -30,7 +56,7 @@ func UpdateAndPushService(repoPath, filePath, serviceName, newTag, SSHKeyPath, a
|
||||
return fmt.Errorf("could not unmarshal YAML: %w", err)
|
||||
}
|
||||
|
||||
// Finding and updating the service image tag
|
||||
// 2. Finding and updating the service image tag
|
||||
services, ok := data["services"].(map[string]interface{})
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid services structure in YAML")
|
||||
@@ -55,16 +81,6 @@ func UpdateAndPushService(repoPath, filePath, serviceName, newTag, SSHKeyPath, a
|
||||
logrus.Infof("docker-compose.yaml file updated successfully.")
|
||||
|
||||
// 4. Committing and pushing the changes to the Git repository
|
||||
repo, err := git.PlainOpen(repoPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open Git repository: %w", err)
|
||||
}
|
||||
|
||||
worktree, err := repo.Worktree()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get worktree: %w", err)
|
||||
}
|
||||
|
||||
relativePath, err := filepath.Rel(repoPath, filePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not get relative path: %w", err)
|
||||
@@ -83,22 +99,18 @@ func UpdateAndPushService(repoPath, filePath, serviceName, newTag, SSHKeyPath, a
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not commit changes: %w", err)
|
||||
return fmt.Errorf("could not create commit: %w", err)
|
||||
}
|
||||
logrus.Infof("New commit created: %s", commit)
|
||||
|
||||
publicKeys, err := ssh.NewPublicKeysFromFile("git", SSHKeyPath, "")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read SSH key: %w", err)
|
||||
}
|
||||
auth := publicKeys
|
||||
// Push the changes to the remote repository
|
||||
logrus.Info("Pushing changes to remote repository...")
|
||||
err = repo.Push(&git.PushOptions{
|
||||
Auth: auth,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not push changes: %w", err)
|
||||
}
|
||||
logrus.Infof("Changes pushed to remote repository successfully.")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
-1
Submodule repos/myrepo deleted from 27608766af
Reference in New Issue
Block a user