fix : fix CI discord notification error #6
This file contains 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: Deploy to AWS ECS | |
on: | |
push: | |
branches: [ "main", "feat/CICD" ] | |
jobs: | |
build: | |
name: Build and push Docker image | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- uses: actions/checkout@v3 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'adopt' | |
- name: Set yml file | |
uses: microsoft/variable-substitution@v1 | |
with: | |
files: ./src/main/resources/application-prod.yml | |
env: | |
spring.datasource.url: ${{ secrets.MYSQL_URL }} | |
spring.datasource.username: ${{ secrets.MYSQL_USERNAME }} | |
spring.datasource.password: ${{ secrets.MYSQL_PASSWORD }} | |
- name: Build with Gradle | |
run: | | |
chmod +x ./gradlew | |
./gradlew clean build -x test | |
- name: Login to Docker Hub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Build Docker image | |
run: docker build -t ${{ secrets.DOCKER_USERNAME }}/server:latest . | |
- name: Push Docker image to Docker Hub | |
run: docker push ${{ secrets.DOCKER_USERNAME }}/server:latest | |
deploy: | |
name: Deploy | |
needs: build | |
runs-on: ubuntu-latest | |
steps: | |
- name: executing remote ssh commands using ssh key | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.SERVER_HOST }} | |
username: ${{ secrets.SSH_USERNAME }} | |
key: ${{ secrets.SSH_KEY }} | |
port: ${{ secrets.SSH_PORT }} | |
script: | | |
docker pull ${{ secrets.DOCKER_USERNAME }}/server:latest | |
if [ "$(docker ps -aq -f name=server)" ] | |
then | |
echo "[ spring is running ]" | |
docker stop server | |
docker rm server | |
else | |
echo "[ spring is not running ]" | |
fi | |
docker run -d --name server -p 80:8080 ${{ secrets.DOCKER_USERNAME }}/server | |
result: | |
name: Send notification | |
needs: [build, deploy] | |
runs-on: ubuntu-latest | |
steps: | |
- name: Send Discord notification on success | |
if: ${{ needs.build.result == 'success' && needs.deploy.result == 'success' }} | |
run: | | |
curl -X POST -H "Content-Type: application/json" -d '{"content": "✅ : Deployment to AWS ECS was successful!"}' ${{ secrets.DISCORD_WEBHOOK_URL }} | |
- name: Send Discord notification on failure | |
if: ${{ needs.build.result != 'success' || needs.deploy.result != 'success' }} | |
run: | | |
curl -X POST -H "Content-Type: application/json" -d '{"content": "❌ : Deployment to AWS ECS failed!"}' ${{ secrets.DISCORD_WEBHOOK_URL }} |