test #36
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Java CI with Gradle | |
| on: | |
| push: | |
| branches: [ "main" ] | |
| jobs: | |
| build: | |
| permissions: | |
| contents: read | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '17' | |
| distribution: 'temurin' | |
| - name: Setup Gradle | |
| uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0 | |
| - name: Grant execute permission for gradlew | |
| run: chmod +x gradlew | |
| - name: Build with Gradle Wrapper | |
| run: ./gradlew clean build --exclude-task test | |
| # Docker 이미지 Build (2) | |
| - name: docker image build | |
| run: docker build -t ilyoil2/cicdcicd . | |
| # DockerHub Login (3) | |
| - name: docker login | |
| uses: docker/login-action@v2 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| # Docker Hub push (4) | |
| - name: docker Hub push | |
| run: docker push ilyoil2/cicdcicd | |
| # GET GitHub IP (5) | |
| - name: get GitHub IP | |
| id: ip | |
| uses: haythem/public-ip@v1.2 | |
| # Configure AWS Credentials (6) - AWS 접근 권한 취득(IAM) | |
| - name: Configure AWS Credentials | |
| uses: aws-actions/configure-aws-credentials@v1 | |
| with: | |
| aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
| aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
| aws-region: ap-northeast-2 | |
| # Add github ip to AWS (7) | |
| - name: Add GitHub IP to AWS | |
| run: | | |
| aws ec2 authorize-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32 | |
| # AWS EC2 Server Connect & Docker 명령어 실행 (8) | |
| - name: AWS EC2 Connection | |
| uses: appleboy/ssh-action@v1.0.3 | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USERNAME }} | |
| key: ${{ secrets.EC2_PRIVATE_KEY }} | |
| port: ${{ secrets.EC2_SSH_PORT }} | |
| timeout: 60s | |
| script: | | |
| sudo docker stop cicdcicd | |
| sudo docker rm cicdcicd | |
| sudo docker run -dp 8080:8080 --name cicdcicd ilyoil2/cicdcicd | |
| # REMOVE Github IP FROM security group (9) | |
| - name: Remove IP FROM security group | |
| run: | | |
| aws ec2 revoke-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32 |