1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2026-06-03 16:35:37 +02:00

fix(agent): persist agent ID after auth to prevent crashloop duplicates (#6543)

Signed-off-by: wucm667 <stevenwucongmin@gmail.com>
This commit is contained in:
wucm667
2026-05-07 08:12:15 +08:00
committed by GitHub
parent 98b0140b75
commit a7a123dc8b
3 changed files with 45 additions and 0 deletions
+4
View File
@@ -41,6 +41,10 @@ func NewAuthGrpcClient(conn *grpc.ClientConn, agentToken string, agentID int64)
return client
}
func (c *AuthClient) AgentID() int64 {
return c.agentID
}
func (c *AuthClient) Auth(ctx context.Context) (string, int64, error) {
ctx, cancel := context.WithTimeout(ctx, authClientTimeout)
defer cancel()
+32
View File
@@ -0,0 +1,32 @@
// Copyright 2024 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rpc
import (
"testing"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func TestAuthClientAgentID(t *testing.T) {
conn, err := grpc.NewClient("localhost:0", grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.NoError(t, err)
defer conn.Close()
client := NewAuthGrpcClient(conn, "test-token", 42)
assert.Equal(t, int64(42), client.AgentID())
}
+9
View File
@@ -137,6 +137,15 @@ func run(ctx context.Context, c *cli.Command, backends []types.Backend) error {
return fmt.Errorf("agent could not auth: %w", err)
}
// Persist the agent ID received during auth so that crashloops reuse the
// same server-side entry instead of creating a new one on every restart.
if agentConfigPath != "" {
agentConfig.AgentID = authClient.AgentID()
if err := writeAgentConfig(agentConfig, agentConfigPath); err == nil {
log.Debug().Msgf("persisted agent ID %d after auth", agentConfig.AgentID)
}
}
conn, err := grpc.NewClient(
c.String("server"),
transport,