-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.gitlab-ci-test-ssh.yml
59 lines (45 loc) · 1.82 KB
/
.gitlab-ci-test-ssh.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
stages:
- sshtest
sshtest:
stage: sshtest
image: docker:latest
services:
- docker:dind
before_script:
# Install SSH tools
- echo "Updating APK packages and installing SSH tools"
- apk update && apk add --no-cache openssh sshpass
# Debug: Verify installation of tools
- echo "Checking SSH version"
- ssh -V || echo "SSH not installed correctly"
# Create the SSH directory
- echo "Setting up SSH directory"
- mkdir -p ~/.ssh
# Debug: Show environment variables
- echo "VPS_USER=$VPS_USER"
- echo "HOST_URL=$HOST_URL"
# Decode the Base64 private key and save it to ~/.ssh/id_rsa
- echo "Decoding private key"
- echo "$HOST_PRIVATE_KEY_B64" | base64 -d > ~/.ssh/id_rsa
# Debug: Verify the key file
- echo "Private key file content (first few lines):"
- head -n 5 ~/.ssh/id_rsa || echo "Failed to create private key"
# Set proper permissions for the private key
- chmod 600 ~/.ssh/id_rsa
# Add the VPS host to known_hosts to avoid authenticity prompts
- echo "Adding VPS host to known_hosts"
- ssh-keyscan -H "$HOST_URL" >> ~/.ssh/known_hosts
# Debug: Show known_hosts content
- echo "Known hosts file content:"
- cat ~/.ssh/known_hosts
# Start ssh-agent and add the private key with the passphrase
- echo "Starting ssh-agent and adding private key"
- eval $(ssh-agent -s)
- echo "$HOST_PASSPHRASE" | ssh-add ~/.ssh/id_rsa || echo "Failed to add private key"
# Debug: List identities in the agent
- echo "Listing identities added to ssh-agent"
- ssh-add -l
script:
# Debug: Test SSH connection
- echo "Testing SSH connection to VPS..."
- ssh -v -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa $VPS_USER@$HOST_URL "echo 'Connected to VPS!' && uname -a && uptime" || echo "SSH connection failed"