Jenkins를 활용한 CI/CD 입문 (with.AWS)
uploading-files-to-s3-via-jenkins
✅ 1. Jenkins를 활용하여 S3에 파일 업로드하기
- 해당 문서의 Example을 활용하여 Jenkins로 S3에 파일 업로드를 진행해보자.
stage('AWS') {
agent {
docker {
image 'amazon/aws-cli'
// aws-cli 이미지는 기본적으로 실행 후 바로 종료되므로 엔트리포인트 무력화
args "--entrypoint=''"
}
}
steps {
withCredentials([usernamePassword(credentialsId: 'my-aws', passwordVariable: 'AWS_SECRET_ACCESS_KEY', usernameVariable: 'AWS_ACCESS_KEY_ID')]) {
sh '''
aws --version
echo "Hello S3!" > index.html
aws s3 cp index.html s3://본인버킷이름/index.html
'''
}
}
}
- AWS S3 버킷에 index.html 파일이 잘 올라가 있는지 확인한다.
✅ 2. 버킷명 환경변수로 등록하기
- 현재는 버킷명을 하드 코딩하여 작성하고 있다.
- 버킷명을 하드 코딩 하는 것보다는 환경변수로 등록하여 관리해보도록 하자.
stages {
stage('AWS') {
agent {
docker {
image 'amazon/aws-cli'
args "--entrypoint=''"
}
}
environment {
AWS_S3_BUCKET = '본인버킷명'
}
steps {
withCredentials([usernamePassword(credentialsId: 'my-aws', passwordVariable: 'AWS_SECRET_ACCESS_KEY', usernameVariable: 'AWS_ACCESS_KEY_ID')]) {
sh '''
aws --version
echo "Hello S3!" > index.html
aws s3 cp index.html s3://$AWS_S3_BUCKET/index.html
'''
}
}
}
- 동일하게 테스트를 진행해 보았을 때 파일이 성공적으로 업로드되어야 한다.
- 만약 업로드가 되지 않았다면 오탈자 등 문법 오류를 찾아서 해결해 볼 것!