ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Spring Boot + maven + AWS EC2 - ubuntu GitHub Action(깃헙 액션 CI/CD) (1)
    Spring boot , 스프링 2023. 4. 6. 21:53
    반응형

    Spring Boot GitHub Action(깃헙 액션 CI/CD) (1) 

    ※ 본 예시는 깃허브에 레포지토리 등록이 되있는것을 가정하여 진행합니다.

    1. EC2 Ubuntu 에서 도커부터 설치합니다.

    // 도커 설치
    sudo pip install docker
    
    // 도커 실행
    sudo service docker start
    
    // 도커 상태 확인
    systemctl status docker.service
    
    // Docker 관련 권한 추가
    sudo chmod 666 /var/run/docker.sock
    docker ps

    2. Github Repository 로 들어가서 Actions 탭으로 이동하고난후 이미지를 따라해줍니다.

    2번에 Maven 입력 이후 밑에 나온 Java with Maven 을 Configure 해줍니다. 

    이후 폴더 내부에 .github 내부에 maven.yml 을 자동생성해줍니다.

    생성이 되고난 이후 밑 이미지처럼 --------아래--------코드를 입력하고 "Start Commit" 이후 본프로젝트에서 Pull 받는다.

    # This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
    # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven
    
    # This workflow uses actions that are not certified by GitHub.
    # They are provided by a third-party and are governed by
    # separate terms of service, privacy policy, and support
    # documentation.
    
    name: Java CI with Maven
    
    on:
      push:
        branches: [ "main" ] #푸쉬할떄 브런치
      pull_request:
        branches: [ "main" ] #푸쉬할떄 브런치
    
    jobs:
      build:
    
        runs-on: ubuntu-latest
    
        steps:
        - uses: actions/checkout@v3
        - name: Set up JDK 11
          uses: actions/setup-java@v3
          with:
            java-version: '11'
            distribution: 'temurin'
            cache: maven
            
        # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive
        - name: Update dependency graph
          uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6
    ---------------------------------------------------------아래를 입력------------------------
    
        # Caching dependencies
        - name: Cache Maven packages
          uses: actions/cache@v2
          with:
            path: ~/.m2
            key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
            restore-keys: ${{ runner.os }}-m2
    
        - name: Build with Maven
          run: mvn clean install
    
        - name: web docker build and push
          run: |
            sudo docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
            sudo docker build -t ${{ secrets.DOCKER_REPO }} .
            sudo docker push ${{ secrets.DOCKER_REPO }}
    
          ## docker compose up
        - name: executing remote ssh commands using password
          uses: appleboy/ssh-action@v0.1.6
          with:
            host: ${{ secrets.HOST }}
            username: ubuntu
            password: ${{ secrets.PASSWORD }}
            port: 22
            script: |
              sudo docker rm -f $(docker ps -qa)
              sudo docker pull ${{ secrets.DOCKER_REPO }}
              sudo docker run -d -p 8080:8080 --name {원하는 생성 네임} ${{ secrets.DOCKER_REPO }}

     

    본 프로젝트 Pull 이후  위에서 보이는 Secrets.Host들을 입력해준다.

    사진의 화면으로 이동해주면 Secrets 추가 할수있다.

    • DOCKER_PASSWORD: 도커 계정 패스워드
    • DOCKER_REPO: 도커 레포지토리
    • DOCKER_USERNAME: 도커 ID
    • HOST : AWS EC2 주소
    • PASSWORD: EC2 인스턴스 패스워드

    Spring Boot + maven + AWS EC2 - ubuntu GitHub Action(깃헙 액션 CI/CD) (2) 에서 마무리 하겠습니다. 

    반응형

    댓글

Designed by Tistory.