Merge pull request #35 from poten001/feat/cicd #3
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: CICD | |
on: | |
push: | |
branches: [main] | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout source | |
uses: actions/checkout@v3 | |
- name: Setup Java | |
uses: actions/setup-java@v3 | |
with: | |
distribution: 'temurin' | |
java-version: '17' | |
- name: Grant execute permission for gradlew | |
run: chmod +x ./gradlew | |
- name: Generate application.yml | |
run: | | |
mkdir -p ./src/main/resources | |
echo "${{ secrets.CD_APPLICATION }}" > ./src/main/resources/application.yml | |
cat ./src/main/resources/application.yml | |
- name: Build Project | |
run: ./gradlew clean build -x test | |
- name: Login to Docker Hub | |
run: docker login -u ${{ secrets.DOCKER_LOGIN_USERNAME }} -p ${{ secrets.DOCKER_LOGIN_ACCESSTOKEN }} | |
- name: Build Docker Image | |
run: docker build -t ${{ secrets.DOCKER_LOGIN_USERNAME }}/tcc . | |
- name: Publish Image to Docker Hub | |
run: docker push ${{ secrets.DOCKER_LOGIN_USERNAME }}/tcc:latest | |
deploy: | |
needs: build | |
runs-on: ubuntu-latest | |
steps: | |
- name: Install SSH Client | |
run: sudo apt-get update && sudo apt-get install -y openssh-client | |
- name: Create SSH Key File | |
run: | | |
echo "${{ secrets.SERVER_KEY }}" > ssh_key.pem | |
chmod 600 ssh_key.pem | |
- name: SSH into EC2 and Deploy | |
run: | | |
ssh -o StrictHostKeyChecking=no -i ssh_key.pem ubuntu@${{ secrets.SERVER_IP }} << 'EOF' | |
sudo docker login -u ${{ secrets.DOCKER_LOGIN_USERNAME }} -p ${{ secrets.DOCKER_LOGIN_ACCESSTOKEN }} | |
sudo docker pull ${{ secrets.DOCKER_LOGIN_USERNAME }}/tcc:latest | |
sudo docker rm -f tcc-container || true | |
sudo docker run -d -p 8080:8080 --name tcc-container ${{ secrets.DOCKER_LOGIN_USERNAME }}/tcc | |
EOF |