2017-09-25 5 views
0

Jenkins Declarative 파이프 라인을보고 도커 컨테이너에서 내 빌드를 실행하기 시작합니다. git을 통해 npm 패키지를 가져오고 따라서 ssh 키를 설정해야하는 프로젝트가 있습니다. 내 JenkinsfileDeclarative Jenkins 파이프 라인과 Docker

pipeline { 
    agent { 
    dockerfile { 
     args '''--build-arg ssh_prv_key="$(cat /var/lib/jenkins-git/.ssh/id_rsa)"''' 
    } 
    } 

    stages { 
    stage('Test') { 
     steps { 
     sh 'echo $ssh_prv_key' 
     } 
    } 
    } 
} 

내에서 다음과 같은 접근 방식을 촬영 한

ARG ssh_pub_key 

빌드를 실행할 때 내 Dockerfile에 다음과 같은 --build-arg ssh_pub_key="$(cat ~/.ssh/id_rsa.pub)" 등의 인수를 구축 설정할 수 있습니다를 통해 내가 온 것과

젠킨스에서 이미지를 만들 때 아래 출력을 얻습니다 (--build-arg 언급 없음).

docker build -t 085eb412f6dd28c1a7843aa9f9ed84e7c4af3e1b -f Dockerfile . 

및 변수가 없습니다.

올바르게 설정하지 않았습니까? 다른 사람이 키 복사를 다른 방식으로 처리합니까?

감사

UPDATE

MY Jenkinsfile 이제 아래처럼 보이지만

Required context class hudson.FilePath is missing 
Perhaps you forgot to surround the code with a step that provides this, such as: node 

내가 파이프 라인 선언 이외의 모든 스크립트를 실행할 수 없습니다 보인다 얻을으로 실행되지 않습니다?

def ssh_prv_key = sh script: 'cat /var/lib/jenkins-git/.ssh/id_rsa', returnStdout: true 
def ssh_pub_key = sh script: 'cat /var/lib/jenkins-git/.ssh/id_rsa.pub', returnStdout: true 

pipeline { 
    agent { 
    dockerfile { 
     args """--build-arg ssh_prv_key=\"${ssh_prv_key}\" --build-arg ssh_pub_key=\"${ssh_pub_key}\" """ 
    } 
    } 
    stages { 
     stage('Test') { 
     steps { 
      sh 'echo $ssh_prv_key' 
     } 
     } 
    } 
} 
+0

SSH 키가 이미지의 환경 변수의 일부가되도록'ENV ssh_pub_key = $ ssh_pub_key'를 추가해야합니다. 지금은 빌드 인수를 전달하지만 도움을 주신 덕분에 –

답변

1

여기서 $(cat /var/lib/jenkins-git/.ssh/id_rsa)은 쉘 명령입니다.

AFAIK, 바인딩은 에이전트를 정의 할 때 파이프 라인 줄 밖에서 선언해야합니다.

그래서 파이프 라인 작업을 매개 변수화하십시오.

  • 자격 증명 매개 변수ssh_prv_key를 추가합니다.
  • 그런 다음 dockerfileadditionalBuildArgs 지시에 ssh_prv_key를 사용 ssh_pub_key

Parameterized Pipeline

에 대한

  • 단계를 반복 secretfile 업로드에 Secretfile
  • 설정의 기본 값을 선택합니다.

    pipeline { 
        agent { 
        dockerfile { 
         additionalBuildArgs ""--build-arg ssh_prv_key=\"$ssh_prv_key\" --build-arg ssh_pub_key=\"$ssh_pub_key\"" 
        } 
        } 
        stages { 
         stage('Test') { 
         steps { 
          sh "echo $ssh_prv_key" 
         } 
         } 
        } 
    } 
    
  • +0

    감사합니다. 파이프 라인 블록 외부 또는 내부에 있습니까? – Richlewis

    +0

    보통 변수 정의를 파이프 라인 블록 외부의 시작 부분에 배치합니다. –

    +0

    내 질문을 업데이트했습니다. 실행 방법에 대해 알고 싶습니다. 감사합니다 – Richlewis

    관련 문제