너무나 명확한 요약 그림
# dockerfile
FROM openjdk:11.0.15
ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} demo-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java","-jar","/demo-0.0.1-SNAPSHOT.jar"]
# .github/workflows/deploy.yml
name: Java CI with Gradle
on:
push: # 해당 branch에서 push 시 스크립트 실행
branches: [ "main" ]
pull_request: # 해당 branch로 pull request 병합 시 스크립트 실행 - 불확실. 테스트 해 볼 예정
branches: [ "main" ]
permissions: # 기본으로 있던 친군데 아마 코드 내용을 수정 못하게 하는 권한 설정인듯?
contents: read
jobs: # 스크립트에서 실행 될 잡들
build:
runs-on: ubuntu-latest # 해당 스크립트가 돌아갈 환경
steps:
- uses: actions/checkout@v3
- name: Set up JDK 11 # 17로 변경 필요
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
- name: Grant execute permission for gradlew # gradlew 권한 설정
run: chmod +x gradlew
- name: Build with Gradle # gradlew로 초기화 및 빌드 (해당 스크립트에선 jar 파일 생성을 위해 bootJar를 사용, 보통은 build 면 됨)
run: ./gradlew clean bootJar
- name: Docker build # (docker hub에 jar 파일 push) (앞서 나온 dockerfile이 이 때문에 필요)
run: |
docker login -u ${{secrets.DOCKERHUB_USERNAME}} -p ${{secrets.DOCKERHUB_PASSWORD}}
docker build -t hooo0503/demo .
docker push hooo0503/demo
- name: Current Time # elastick beanstalk 에선 버전 이름이 같으면 에러 발생. 버전 이름 중복 방지용 시간
uses: gerred/current-time@master
id: current-time
- name: Replace string # 위와 동일한 이유로 존재. 앞서 수집한 current time을 format화 시켜주는 job
uses: frabert/replace-string-action@v2.1
id: format-time
with:
pattern: '[:\\.]+'
string: "${{ steps.current-time.outputs.time }}"
replace-with: '-'
flags: 'g'
- name: Beanstalk Deploy # aws beanstalk에 deploy하는 script.
uses: einaregilsson/beanstalk-deploy@v20
with:
# AWS Access Key
aws_access_key: ${{secrets.AWS_ACCESS_KEY_ID}}
# AWS Secret Key
aws_secret_key: ${{secrets.AWS_SECRET_ACCESS_KEY}}
# AWS Region
region: ap-northeast-2
# Beanstalk application name
application_name: "docker-test"
# Beanstalk environment name. If empty a version will be created but not deployed anywhere.
environment_name: "Dockertest-env"
# Version label for new Beanstalk version
version_label: "github-action--${{steps.format-time.outputs.replaced}}"
# Zip file with the version to deploy. If skipped the action will deploy existing version.
deployment_package: Dockerrun.aws.json # 생성된 인스턴스 내부에서 도커에게 전달할 데이터 함유
{
"AWSEBDockerrunVersion": "1",
"Image": {
"Name": "hooo0503/demo:latest", //docker hub에 존재하는 레포지토리 이름 및 태그 명
"Update": "true" // 넌 뭐냐?
},
"Ports": [
{
"ContainerPort": 8080, // 컨테이너 내부 포트. 도커 내부에서 돌아갈 Application의 포트 값을 넣어주면 된다.
"HostPort": 5000 // 컨테이너 포트와 연결될 호스트 포트. 호스트의 5000번과 컨테이너의 8080이 연결된다.
}
]
}
2. Github Action & AWS Beanstalk 배포하기 - profile=local로 배포하기
[CI/CD] Docker와 Elastic Beanstalk를 사용한 spring boot 프로젝트 CI/CD 자동화 배포2 (실습내용)