From b6ecd15051e8acee7f7dac593faa0d6fb301e00b Mon Sep 17 00:00:00 2001 From: tran-doan Date: Wed, 22 Mar 2023 09:38:21 +0700 Subject: [PATCH 1/7] add workflows --- .github/workflows/github-actions-demo.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 .github/workflows/github-actions-demo.yml diff --git a/.github/workflows/github-actions-demo.yml b/.github/workflows/github-actions-demo.yml new file mode 100644 index 0000000..e007988 --- /dev/null +++ b/.github/workflows/github-actions-demo.yml @@ -0,0 +1,18 @@ +name: GitHub Actions Demo +run-name: ${{ github.actor }} is testing out GitHub Actions 🚀 +on: [push] +jobs: + Explore-GitHub-Actions: + runs-on: ubuntu-latest + steps: + - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event." + - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!" + - run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}." + - name: Check out repository code + uses: actions/checkout@v3 + - run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner." + - run: echo "🖥️ The workflow is now ready to test your code on the runner." + - name: List files in the repository + run: | + ls ${{ github.workspace }} + - run: echo "🍏 This job's status is ${{ job.status }}." From bc8d127b7f213efc91a58b686c7942ef8024b918 Mon Sep 17 00:00:00 2001 From: tran-doan Date: Thu, 23 Mar 2023 10:49:29 +0700 Subject: [PATCH 2/7] automate deployment on AWS --- .github/workflows/github-actions-demo.yml | 54 ++++++++++++++++------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/.github/workflows/github-actions-demo.yml b/.github/workflows/github-actions-demo.yml index e007988..ff41f77 100644 --- a/.github/workflows/github-actions-demo.yml +++ b/.github/workflows/github-actions-demo.yml @@ -1,18 +1,42 @@ -name: GitHub Actions Demo -run-name: ${{ github.actor }} is testing out GitHub Actions 🚀 -on: [push] +# name: GitHub Actions Demo +# run-name: ${{ github.actor }} is testing out GitHub Actions 🚀 +# on: [push] +# jobs: +# Explore-GitHub-Actions: +# runs-on: ubuntu-latest +# steps: +# - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event." +# - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!" +# - run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}." +# - name: Check out repository code +# uses: actions/checkout@v3 +# - run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner." +# - run: echo "🖥️ The workflow is now ready to test your code on the runner." +# - name: List files in the repository +# run: | +# ls ${{ github.workspace }} +# - run: echo "🍏 This job's status is ${{ job.status }}." +name: Deploy to AWS + +on: + push: + branches: + - master + jobs: - Explore-GitHub-Actions: + deploy: runs-on: ubuntu-latest steps: - - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event." - - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!" - - run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}." - - name: Check out repository code - uses: actions/checkout@v3 - - run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner." - - run: echo "🖥️ The workflow is now ready to test your code on the runner." - - name: List files in the repository - run: | - ls ${{ github.workspace }} - - run: echo "🍏 This job's status is ${{ job.status }}." + - name: Checkout code~ + uses: actions/checkout@v2 + + - 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-southeast-1 + + - name: Deploy pod~ + run: | + kubectl apply -f petstore.yml From de756213852d581349f2a483e7a3e96dd244d669 Mon Sep 17 00:00:00 2001 From: tran-doan Date: Thu, 23 Mar 2023 11:15:54 +0700 Subject: [PATCH 3/7] xxx --- .github/workflows/github-actions-demo.yml | 73 +++++++++++++++++++---- 1 file changed, 61 insertions(+), 12 deletions(-) diff --git a/.github/workflows/github-actions-demo.yml b/.github/workflows/github-actions-demo.yml index ff41f77..9cb2d7a 100644 --- a/.github/workflows/github-actions-demo.yml +++ b/.github/workflows/github-actions-demo.yml @@ -16,27 +16,76 @@ # run: | # ls ${{ github.workspace }} # - run: echo "🍏 This job's status is ${{ job.status }}." -name: Deploy to AWS + + + +# name: Deploy to AWS + +# on: +# push: +# branches: +# - master + +# jobs: +# deploy: +# runs-on: ubuntu-latest +# steps: +# - name: Checkout code~ +# uses: actions/checkout@v2 + +# - 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-southeast-1 + +# - name: Deploy to Server 1 +# uses: easingthemes/ssh-deploy@main +# env: +# SSH_PRIVATE_KEY: ${ { secrets.EC2_SSH_KEY }} +# REMOTE_HOST: ${ { secrets.HOST_DNS }} +# REMOTE_USER: ${ { secrets.USERNAME }} +# TARGET: ${ { secrets.TARGET_DIR }} + # - name: Deploy pod~ + # run: | + # kubectl apply -f petstore.yml + + + +name: Deploy Spring Boot app to EC2 on: push: branches: - - master + - main jobs: deploy: runs-on: ubuntu-latest + steps: - - name: Checkout code~ + - name: Checkout code uses: actions/checkout@v2 - - - name: Configure AWS credentials - uses: aws-actions/configure-aws-credentials@v1 + + - name: Set up Java 8 + uses: actions/setup-java@v8 with: - aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} - aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - aws-region: ap-southeast-1 - - - name: Deploy pod~ + java-version: 8 + + - name: Build Spring Boot app + run: ./mvnw clean package + + - name: Install AWS CLI + run: sudo apt-get install awscli + + - name: Deploy to EC2 instance run: | - kubectl apply -f petstore.yml + aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }} + aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws configure set default.region us-west-2 + aws ec2 describe-instances --instance-ids ${{ secrets.EC2_INSTANCE_ID }} + scp -i ${{ secrets.EC2_SSH_KEY }} target/*.jar ec2-user@${{ secrets.EC2_INSTANCE_IP }}:/home/ec2-user/ + ssh -i ${{ secrets.EC2_SSH_KEY }} ec2-user@${{ secrets.EC2_INSTANCE_IP }} 'sudo systemctl stop myapp.service' + ssh -i ${{ secrets.EC2_SSH_KEY }} ec2-user@${{ secrets.EC2_INSTANCE_IP }} 'sudo mv /home/ec2-user/*.jar /opt/myapp/myapp.jar' + ssh -i ${{ secrets.EC2_SSH_KEY }} ec2-user@${{ secrets.EC2_INSTANCE_IP }} 'sudo systemctl start myapp.service' From fac40f9063eea871ea40cc084c14cf6f2e601301 Mon Sep 17 00:00:00 2001 From: tran-doan Date: Thu, 23 Mar 2023 11:18:32 +0700 Subject: [PATCH 4/7] xxx --- .github/workflows/github-actions-demo.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/github-actions-demo.yml b/.github/workflows/github-actions-demo.yml index 9cb2d7a..9d62cee 100644 --- a/.github/workflows/github-actions-demo.yml +++ b/.github/workflows/github-actions-demo.yml @@ -58,7 +58,7 @@ name: Deploy Spring Boot app to EC2 on: push: branches: - - main + - master jobs: deploy: From 3d4ea98a276ae9f86ca4c74cedfa5d61d087f239 Mon Sep 17 00:00:00 2001 From: tran-doan Date: Thu, 23 Mar 2023 11:27:20 +0700 Subject: [PATCH 5/7] xxx --- .github/workflows/github-actions-demo.yml | 42 ++++++++++++++--------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/.github/workflows/github-actions-demo.yml b/.github/workflows/github-actions-demo.yml index 9d62cee..12a9c4c 100644 --- a/.github/workflows/github-actions-demo.yml +++ b/.github/workflows/github-actions-demo.yml @@ -67,25 +67,35 @@ jobs: steps: - name: Checkout code uses: actions/checkout@v2 - - - name: Set up Java 8 - uses: actions/setup-java@v8 + + - name: Setup Java + uses: actions/setup-java@v1 with: - java-version: 8 + java-version: '8' - name: Build Spring Boot app - run: ./mvnw clean package + run: mvn clean install -DskipTests + # run: ./mvnw clean package - name: Install AWS CLI run: sudo apt-get install awscli - - - name: Deploy to EC2 instance - run: | - aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }} - aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }} - aws configure set default.region us-west-2 - aws ec2 describe-instances --instance-ids ${{ secrets.EC2_INSTANCE_ID }} - scp -i ${{ secrets.EC2_SSH_KEY }} target/*.jar ec2-user@${{ secrets.EC2_INSTANCE_IP }}:/home/ec2-user/ - ssh -i ${{ secrets.EC2_SSH_KEY }} ec2-user@${{ secrets.EC2_INSTANCE_IP }} 'sudo systemctl stop myapp.service' - ssh -i ${{ secrets.EC2_SSH_KEY }} ec2-user@${{ secrets.EC2_INSTANCE_IP }} 'sudo mv /home/ec2-user/*.jar /opt/myapp/myapp.jar' - ssh -i ${{ secrets.EC2_SSH_KEY }} ec2-user@${{ secrets.EC2_INSTANCE_IP }} 'sudo systemctl start myapp.service' + + - name: Deploy to EC2 + uses: easingthemes/ssh-deploy@v2 + with: + ssh-private-key: ${{ secrets.EC2_SSH_KEY }} + ssh-user: ${{ secrets.USERNAME }} + ssh-host: ${{ secrets.HOST_DNS }} + remote-path: /home/ubuntu/myapp + local-path: target/myapp.jar + args: --exclude=*.log + # - name: Deploy to EC2 instance + # run: | + # aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }} + # aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }} + # aws configure set default.region us-west-2 + # aws ec2 describe-instances --instance-ids ${{ secrets.EC2_INSTANCE_ID }} + # scp -i ${{ secrets.EC2_SSH_KEY }} target/*.jar ec2-user@${{ secrets.EC2_INSTANCE_IP }}:/home/ec2-user/ + # ssh -i ${{ secrets.EC2_SSH_KEY }} ec2-user@${{ secrets.EC2_INSTANCE_IP }} 'sudo systemctl stop myapp.service' + # ssh -i ${{ secrets.EC2_SSH_KEY }} ec2-user@${{ secrets.EC2_INSTANCE_IP }} 'sudo mv /home/ec2-user/*.jar /opt/myapp/myapp.jar' + # ssh -i ${{ secrets.EC2_SSH_KEY }} ec2-user@${{ secrets.EC2_INSTANCE_IP }} 'sudo systemctl start myapp.service' From c8ed5ff7b475968091cadf34e8c9489f42aa4647 Mon Sep 17 00:00:00 2001 From: tran-doan Date: Thu, 23 Mar 2023 11:43:10 +0700 Subject: [PATCH 6/7] xxx --- .github/workflows/github-actions-demo.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/github-actions-demo.yml b/.github/workflows/github-actions-demo.yml index 12a9c4c..b66925d 100644 --- a/.github/workflows/github-actions-demo.yml +++ b/.github/workflows/github-actions-demo.yml @@ -84,7 +84,7 @@ jobs: uses: easingthemes/ssh-deploy@v2 with: ssh-private-key: ${{ secrets.EC2_SSH_KEY }} - ssh-user: ${{ secrets.USERNAME }} + ssh-user: 'ec2-user' ssh-host: ${{ secrets.HOST_DNS }} remote-path: /home/ubuntu/myapp local-path: target/myapp.jar From 82fa1420df4618f253262fc9ef07bff903a7dc9c Mon Sep 17 00:00:00 2001 From: tran-doan Date: Thu, 23 Mar 2023 11:45:32 +0700 Subject: [PATCH 7/7] xxx --- .github/workflows/github-actions-demo.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/github-actions-demo.yml b/.github/workflows/github-actions-demo.yml index b66925d..71c12f2 100644 --- a/.github/workflows/github-actions-demo.yml +++ b/.github/workflows/github-actions-demo.yml @@ -83,12 +83,12 @@ jobs: - name: Deploy to EC2 uses: easingthemes/ssh-deploy@v2 with: - ssh-private-key: ${{ secrets.EC2_SSH_KEY }} - ssh-user: 'ec2-user' - ssh-host: ${{ secrets.HOST_DNS }} + SSH_PRIVATE_KEY: ${{ secrets.EC2_SSH_KEY }} + REMOTE_USER: 'ec2-user' + REMOTE_HOST: ${{ secrets.HOST_DNS }} remote-path: /home/ubuntu/myapp local-path: target/myapp.jar - args: --exclude=*.log + ARGS: --exclude=*.log # - name: Deploy to EC2 instance # run: | # aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }}