Merge pull request #300 from QASTUDIODEV/feature/#66-test #192
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: Deploy to AWS EC2 using Docker | |
| on: | |
| push: | |
| branches: | |
| - develop | |
| env: | |
| DOCKER_IMAGE_NAME: developerjamey/qastudiodev | |
| EC2_HOST: ${{ secrets.EC2_URL }} | |
| EC2_SSH_USER: ubuntu | |
| PRIVATE_KEY: ${{ secrets.EC2_SSH_PRIVATE_KEY }} | |
| CONTAINER_NAME: qastudio_dev_container | |
| jobs: | |
| build-and-push-docker: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v3 | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v3 | |
| with: | |
| java-version: '17' | |
| distribution: 'temurin' | |
| - name: Generate resources folder | |
| run: mkdir -p ./src/main/resources | |
| - name: Set up application.yml | |
| run: echo "${{ secrets.APPLICATION_DEV }}" > ./src/main/resources/application.yml | |
| - name: Grant execute permission for gradlew | |
| run: chmod +x ./gradlew | |
| - name: Build with Gradle | |
| run: ./gradlew build -x test | |
| - name: Build the Docker image | |
| run: docker build . --file Dockerfile --tag ${{ env.DOCKER_IMAGE_NAME }}:latest | |
| - name: Login to Docker Hub using Access Token | |
| run: echo "${{ secrets.DOCKER_HUB_TOKEN }}" | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin | |
| - name: Push the Docker image | |
| run: docker push ${{ env.DOCKER_IMAGE_NAME }}:latest | |
| deploy-to-ec2: | |
| needs: build-and-push-docker | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Deploy to EC2 | |
| uses: appleboy/ssh-action@master | |
| with: | |
| host: ${{ env.EC2_HOST }} | |
| username: ${{ env.EC2_SSH_USER }} | |
| port: 22 | |
| key: ${{ env.PRIVATE_KEY }} | |
| script: | | |
| TIMESTAMP=$(date +"%Y%m%d%H%M%S") | |
| NEW_CONTAINER_NAME="${{ env.CONTAINER_NAME }}-$TIMESTAMP" | |
| # selenium_net create | |
| if sudo docker network inspect selenium-net >/dev/null 2>&1; then | |
| echo "Network 'selenium-net' already exists." | |
| else | |
| echo "Creating network 'selenium_net'..." | |
| sudo docker network create selenium-net | |
| fi | |
| # Check for running container on port 8080 | |
| CONTAINER_ID=$(sudo docker ps -q --filter "publish=8080") | |
| # Stop the running container if it exists | |
| if [ ! -z "$CONTAINER_ID" ]; then | |
| echo "Stopping existing container: $CONTAINER_ID" | |
| sudo docker stop $CONTAINER_ID | |
| fi | |
| SELENIUM_CONTAINER=$(sudo docker ps -q --filter "name=selenium_chrome") | |
| if [ -z "$SELENIUM_CONTAINER" ]; then | |
| echo "Starting Selenium Standalone Chrome container..." | |
| sudo docker run -d \ | |
| --name selenium-chrome \ | |
| --network selenium-net \ | |
| -p 4444:4444 \ | |
| --shm-size=2g \ | |
| --memory=10g \ | |
| --memory-swap=12g \ | |
| -e SE_DISABLE_AUTH=true \ | |
| selenium/standalone-chrome:132.0 | |
| else | |
| echo "Selenium container is already running." | |
| fi | |
| # Pull the latest image from Docker Hubs | |
| echo "Pulling latest image: ${{ env.DOCKER_IMAGE_NAME }}" | |
| sudo docker pull ${{ env.DOCKER_IMAGE_NAME }} | |
| # Run the new container | |
| echo "Starting new container: ${{ env.CONTAINER_NAME }}" | |
| sudo docker run --name $NEW_CONTAINER_NAME --network selenium-net -d -p 8080:8080 -e TZ=Asia/Seoul ${{ env.DOCKER_IMAGE_NAME }} | |
| # Perform health check | |
| echo "Performing health check on the new container..." | |
| HEALTH_CHECK_URL="http://127.0.0.1:8080/health" | |
| for i in {1..30}; do | |
| HEALTH_STATUS=$(curl -s -o /dev/null -w "%{http_code}" $HEALTH_CHECK_URL) | |
| if [ "$HEALTH_STATUS" == "200" ]; then | |
| echo "Health check passed. New container is healthy." | |
| # Remove old container and image | |
| if [ ! -z "$CONTAINER_ID" ]; then | |
| echo "Removing old container: $CONTAINER_ID" | |
| sudo docker rm $CONTAINER_ID | |
| fi | |
| IMAGE_ID=$(sudo docker images -q ${{ env.DOCKER_IMAGE_NAME }}) | |
| if [ ! -z "$IMAGE_ID" ]; then | |
| echo "Removing old image: $IMAGE_ID" | |
| sudo docker rmi $IMAGE_ID | |
| fi | |
| exit 0 | |
| fi | |
| echo "Health check failed. Retrying in 5 seconds... ($i/30)" | |
| sleep 10 | |
| done | |
| # If health check failed | |
| if [ "$HEALTH_STATUS" != "200" ]; then | |
| echo "Health check failed after multiple attempts. Restarting old container." | |
| sudo docker stop $NEW_CONTAINER_NAME | |
| sudo docker rm $NEW_CONTAINER_NAME | |
| if [ ! -z "$CONTAINER_ID" ]; then | |
| echo "Restarting old container: $CONTAINER_ID" | |
| sudo docker start $CONTAINER_ID | |
| fi | |
| LATEST_IMAGE_ID=$(sudo docker images ${{ env.DOCKER_IMAGE_NAME }}:latest -q) | |
| if [ ! -z "$LATEST_IMAGE_ID" ]; then | |
| echo "Removing latest image: $LATEST_IMAGE_ID" | |
| sudo docker rmi $LATEST_IMAGE_ID | |
| fi | |
| exit 1 | |
| fi |