관리 메뉴

피터의 개발이야기

[jenkins] jenkins 빌드 후 JAR 파일 복사 본문

DevOps/Jenkins

[jenkins] jenkins 빌드 후 JAR 파일 복사

기록하는 백앤드개발자 2023. 10. 17. 00:42
반응형

ㅁ 개요

ㅇ 회사 업무 중 고객에게 재공되는 Jar를 빌드하는 Jenkins 작업이 있었다. 빌드가 완료된 Jar는 scp를 통해 로컬로 옮겨서 반입요청 메일을 보내는 작업이 있는데, 이 과정을 개선하기로 하였다. 이를 위해 Jenkins에서 바로 다운로드 받는 방법을 정리하였다.

 

https://medium.com/@gustavo.guss/jenkins-archive-artifact-save-file-in-pipeline-ac6d8b569c2c

 

 

ㅁ 젠킨스 테스트 환경

 ㅇ 젠킨스 테스트 환경은 [Jenkins] Docker 기반 Jenkins quick start examples을 이용하였습니다.

 

ㅁ 샘플 item 생성

 ㅇ fileArchive라는 파이프라인 item을 생성하였습니다.

 

pipeline {
  agent any
  stages {

    stage('Download') {
      steps {
        sh 'echo "artifact file" > generatedFile.txt'
      }
    }
  }
    
  post {
     always {
        archiveArtifacts artifacts: 'generatedFile.txt', # 싱글파일
        onlyIfSuccessful: true # 성공일때만
    }
  }
  
}

 ㅇ Pipeline에 간단하게 파일을 생성하고 다운로드 하는 Script를 작성하였습니다.

 

ㅁ 테스트 빌드

 ㅇ 왼쪽의 "지금 빌드" 메뉴를 클릭하면 Last Successful Artifacts 부분이 표출됩니다.

 ㅇ txt 파일명을 클릭 시 파일의 내용을 웹브라우저를 통해 직접 확인을 할 수도 있습니다.

 ㅇ Last Successful Artifacts을 클릭 시 아래의 화면으로 이동하여 파일을 압축 다운로드 받을 수 있습니다.

 

 

ㅁ 결과파일 압축 방식

 ㅇ 단일파일

archiveArtifacts artifacts: 'teste.js'

 ㅇ 멀티파일

archiveArtifacts artifacts: '*.js'

 ㅇ 다중패턴

archiveArtifacts artifacts: 'target/*.jar, target/*.war'

 

ㅇ 단일폴더

archiveArtifacts artifacts: 'build/*.js'

 ㅇ 다중폴더

archiveArtifacts artifacts: '**/*.js'
  • js/build.min.js
  • css/build.min.js

위의 폴더 구조 시에 사용할 수 있습니다.

 

ㅇ 성공일 때만

onlyIfSuccessful: true

 

 

ㅁ archive artifacts 적용 트러블 슈팅

https://stackoverflow.com/questions/43187095/jenkins-archive-artifacts-doesn-t-match-anything

 ㅇ 실 운영환경에서 완성된 jar가 다른 곳으로 이동되어서 workspace 밖의 경로에서는 실패가 났다.

 ㅇ 함께 일하는 동료가 알려준 글을 보고 원인을 찾을 수 있었다.

 

ㅁ Jenkins Workspace에서만 작동

archiveArtifacts artifacts: "build/**" 
archiveArtifacts artifacts: "./build/**" // Fails 
archiveArtifacts artifacts: "$workspace/build/**" // Fails

ㅇ 아카이브 아티팩트는 작업공간에서만 작동하고 외부에서는 실패합니다.
ㅇ 경로는 작업 공간의 상대 경로여야 하며 "."으로 시작하지 않습니다. 
ㅇ "*"는 디렉터리의 모든 파일과 일치하고,
      **는 dir 및 하위 디렉터리를 포함한 모든 항목과 일치하며,

      **/.log는 potfix가 있는 모든 하위 디렉터리의 모든 파일과 일치합니다.

ㅇ Windows 노드인지 Linux 노드인지에 따라 경로를 다르게 표현시 해야합니다.

 

ㅁ 함께 보면 좋은 사이트

Jenkins Core archiveArtifacts: Archive the artifacts

 

Jenkins Core

The following plugin provides functionality available through Pipeline-compatible steps. Read more about how to integrate steps into your Pipeline in the Steps section of the Pipeline Syntax page. For a list of other such plugins, see the Pipeline Steps Re

www.jenkins.io

Jenkins archive artifact/save file in Pipeline

 

Jenkins archive artifact/save file in Pipeline

In this tutorial i will show how to save files “outside build workspace” to get after others builds.

medium.com

반응형
Comments