Merge remote-tracking branch 'origin/main' #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 EC2 | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Latest Repo | |
uses: actions/checkout@v3 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
- name: Grant execute permission for gradlew | |
run: chmod +x gradlew | |
- name: Create application.yml from secret | |
run: | | |
mkdir -p src/main/resources | |
echo "${{ secrets.APPLICATION_YML }}" > src/main/resources/application.yml | |
- name: Build with Gradle | |
run: ./gradlew clean build -x test | |
- name: Docker login | |
run: echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin | |
- name: Docker build | |
run: | | |
docker build -t huranit-server . | |
docker tag huranit-server hysong4u/huranit-server:latest | |
docker push hysong4u/huranit-server:latest | |
- name: Get timestamp | |
uses: gerred/actions/current-time@master | |
id: current-time | |
- name: Run string replace | |
uses: frabert/replace-string-action@master | |
id: format-time | |
with: | |
pattern: '[:\.]+' | |
string: "${{ steps.current-time.outputs.time }}" | |
replace-with: '-' | |
flags: 'g' | |
- name: Deploy to EC2 | |
env: | |
PRIVATE_KEY: ${{ secrets.EC2_SSH_PRIVATE_KEY }} | |
SERVER_ADDRESS: ${{ secrets.EC2_SERVER_ADDRESS }} | |
run: | | |
echo "$PRIVATE_KEY" > private_key.pem | |
chmod 600 private_key.pem | |
ssh -T -o StrictHostKeyChecking=no -i private_key.pem ubuntu@$SERVER_ADDRESS << 'EOF' | |
docker pull hysong4u/huranit-server:latest | |
existing_container_id=$(sudo docker ps -q --filter "publish=8081") | |
[ -n "$existing_container_id" ] && sudo docker stop $existing_container_id && sudo docker rm $existing_container_id | |
sudo docker image prune -f | |
docker run -d -p 8081:8081 hysong4u/huranit-server:latest | |
EOF | |
# docker 권한 문제 발생시 | |
# sudo chmod 660 /var/run/docker.sock | |
# sudo /usr/sbin/usermod -aG docker $USER | |
# sudo /usr/sbin/usermod -aG docker ubuntu | |