Git Action으로 DockerHub에 빌드 Image 업로드
- main branch push -> Git Action 동작 -> DockerHub 이미지 업로드
deploy_server_with_docker_ec2.yaml 생성
name: Deploy to Ec2 With Docker
on:
push:
branches:
- main
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'temurin'
- name: Build Docker Image
working-directory: ./ordering
run: docker build -t greatjang/ordering-server:v2 .
- name: DockerHub Login
uses: docker/login-action@v1 #docker 재단에서 만들어놓은 docker 사용
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Push Docker Image to DockerHub
run: docker push greatjang/ordering-server:v2
Git Action에 secrets.DOCKER_USERNAME, secrets.DOCKER_PASSWORD 설정
git push
Git Action 동작
Docker Hub 이미지 업로드 확인
Docker EC2 배포
EC2 생성 및 RDS 생성 -> Git Actions secrets and variables에 EC2정보 및 RDS(DB)정보 수정
보안그룹 설정 주의
deploy_server_with_docker_ec2.yaml에 name: ec2 ssh login and docker run 내용 추가(EC2, RDS)
name: Deploy to Ec2 With Docker
on:
push:
branches:
- main
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'temurin'
- name: Build Docker Image
working-directory: ./ordering
run: docker build -t greatjang/ordering-server:v2 .
- name: DockerHub Login
uses: docker/login-action@v1 #docker 재단에서 만들어놓은 docker 사용
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Push Docker Image to DockerHub
run: docker push greatjang/ordering-server:v2
- name: ec2 ssh login and docker run
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST1 }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_SSH_KEY }}
# type docker : docker명령어가 현재 시스템에 존재하는지를 확인
# > /dev/null : 명령어의 출력을 무시할 때 사용
# curl -s https://get.docker.com -o get-docker.sh // get-docker.sh 이름으로 https://get.docker.com의 html 내용을 다운받아서 저장
# sudo docker login --username ${{ secrets.DOCKER_USERNAME }} --password ${{ secrets.DOCKER_PASSWORD }} docker hub repo private에서는 로그인 필요
script: |
if ! type docker > /dev/null ; then
curl -s https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
fi
sudo docker login --username ${{ secrets.DOCKER_USERNAME }} --password ${{ secrets.DOCKER_PASSWORD }}
sudo docker pull greatjang/ordering-server:v2
sudo docker rm -f order-backend || true
sudo docker run -d --name order-backend -p 80:8080 \
-e SPRING_DATASOURCE_URL=jdbc:mariadb://${{ secrets.DB_HOST }}:3306/spring_order \
-e SPRING_DATASOURCE_USERNAME=${{ secrets.DB_USERNAME }} \
-e SPRING_DATASOURCE_PASSWORD=${{ secrets.DB_PASSWORD }} greatjang/ordering-server:v2
EC2 생성
RDS 생성
DB 접속 확인
spring_order 스키마 생성하기
Git Actions : EC2정보 및 RDS(DB)정보 수정
git push
Git Action 정상동작 확인
DB 테이블 생성 확인
Docker 컨테이너 접속
컨테이너 접속 확인
sudo docker ps, sudo docker images 이미지 생성 및 컨테이너 동작 확인
'Cloud > AWS' 카테고리의 다른 글
Kubernetes2_2024_03_06 (0) | 2024.03.06 |
---|---|
Kubernetes1_2024_03_05 (0) | 2024.03.05 |
Docker2_2024_03_04 (1) | 2024.03.05 |
AWS 배포4_Git action_2024-02-28 (0) | 2024.03.05 |
Docker1_2023_02_29 (0) | 2024.03.04 |