젠킨슨 SSH 플러그인들
https://bo20cy.tistory.com/24
이전 글과 연결됩니다.
마스터와 슬레이브 노드라고 거창하게 적었지만
SSH통신으로 연결된 상태이다.
파일 전송하려면 sftp같은 프로토콜을 결국 써야한다.
서버에 직접 젠킨슨으로 설치했으면 바로 보내면 되지만
컨테이너에 올렸기 때문에 젠킨슨 컨테이너에서 SSH프로토콜을 간단하게 사용하게 플러그인을 사용합니다.
Publish Over SSH
플러그인은 SSH를 사용해 파일을 보내거나 명령어를 작동(파일전송, 명령어 실행)
젠킨슨에서 프로젝트로 파일을 가공해서 다시 내보낼때 유용합니다.
- Publish Over SSH 공식홈페이지
https://plugins.jenkins.io/publish-over-ssh/
Publish Over SSH
Send build artifacts over SSH
plugins.jenkins.io
- Publish Over SSH 예시
Publish Over SSH
environment {SSH_CONFIG = 'ssh_config'}' //Jenkins Credentials에서 설정한 SSH 이름 세팅
...
stage('docker-compose-remote') {
steps {
script {
sshPublisher(
publishers: [
sshPublisherDesc(
configName: SSH_CONFIG,
transfers: [
sshTransfer(
execCommand: "cd ${REMOTE_PATH} && docker-compose up -d --build"
)
]
)
]
)
}
}
}
}
}
SSH Pipeline Steps Plugin
플러그인은 SSH를 사용해 파일을 보내거나 명령어를 작동(파일전송, 명령어 실행)
SSH Pipeline Steps Plugin는 ssh기능 sshCommand, sshScript, sshPut, sshGet, sshRemove을 작동할 때 유용합니다.
- SSH Pipeline Steps 공식홈페이지
https://plugins.jenkins.io/ssh-steps/
SSH Pipeline Steps
Jenkins pipeline steps which provides SSH facilities such as command execution or file transfer for continuous delivery.
plugins.jenkins.io
- SSH Pipeline Steps Plugin 예시
node {
def remote = [:]
remote.name = 'test'
remote.host = 'test.domain.com'
remote.user = 'root'
remote.password = 'password'
remote.allowAnyHosts = true
stage('Remote SSH') {
sshRemove remote: remote, path: "abc.sh"
}
}
ssh유저를 정하면 된다.
그러나 password를 직접 쓰지 않고
credential로 관리하려면
environment {SSH_CONFIG = 'ssh_config'}' //Jenkins Credentials에서 설정한 SSH 이름 세팅
...
withCredentials([usernamePassword(credentialsId: 'SSH_CONFIG', passwordVariable: 'MyName', usernameVariable: 'MyPasswd')]){
steps {
script {
def remote = [
name: 'docker-compose-remote',
host: 'test.domain.com',
user: 'MyName',
password: 'MyPasswd,
allowAnyHosts: true // 호스트 키 검사를 건너뛰도록 설정
]
sshCommand remote: remote, command: "cd ${REMOTE_PATH} && docker-compose up -d --build"
}
}
}
아래 같이 Credentials Binding Plugin인 사용합니다
Credentials Binding Plugin
Jenkins에서 SSH 키, 사용자 이름, 비밀번호 정보를 안전하게 관리하고,
파이프라인 스크립트에서 사용합니다.
- Credentials Binding Plugin 공식홈페이지
https://plugins.jenkins.io/credentials-binding/
Credentials Binding
Allows credentials to be bound to environment variables for use from miscellaneous build steps.
plugins.jenkins.io
- Credentials Binding Plugin 예시
withCredentials([sshUserPrivateKey(credentialsId: 'SSH_CONFIG', keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'userName')])
AWS_ACCESS_KEY_ID = credentials('amazon-access-key')
그러나 코드가 점점 복잡해지기 때문에 ssh 인증만 간단하게 하고 스크립트만 사용하려면
SSH Agent Plugin
SSH 키를 관리하고 SSH 접속 시 보안을 강화하기 위해 SSH 키를 임시로 로드하여 사용하고,
작업이 끝난 후 자동으로 키를 해제합니다.
- SSH Agent Plugin 공식홈페이지
https://plugins.jenkins.io/ssh-agent/
SSH Agent
This plugin allows you to provide SSH credentials to builds via a ssh-agent in Jenkins.
plugins.jenkins.io
- SSH Agent Plugin 예시
environment {SSH_CONFIG = 'ssh_config'}' //Jenkins Credentials에서 설정한 SSH 이름 세팅
...
steps {
sshagent(credentials: ['SSH_CONFIG']) {
sh '''
[ -d ~/.ssh ] || mkdir ~/.ssh && chmod 0700 ~/.ssh
ssh-keyscan -t rsa,dsa example.com >> ~/.ssh/known_hosts
ssh user@example.com ...
'''
}
}
다음번에 플러그들을 설치해서 도커 컴포즈 작동시킬 예정입니다.
참고
'젠킨슨' 카테고리의 다른 글
젠킨슨 2.462.3로 업데이트 하기 (1) | 2024.12.02 |
---|---|
젠킨슨 슬레이브 노드 만들기 (0) | 2024.06.21 |
젠킨슨 설치 (0) | 2024.06.17 |