prod 서버와 동일하게 스크립트 변경 #222
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: Coduo Backend Test Server CD | |
on: | |
push: | |
branches: [ "BE/test" ] | |
jobs: | |
build: | |
environment: test | |
runs-on: ubuntu-latest | |
defaults: | |
run: | |
working-directory: ./backend | |
steps: | |
- name: checkout | |
uses: actions/checkout@v4 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '17' | |
distribution: 'corretto' | |
- name: Setup Gradle | |
uses: gradle/actions/setup-gradle@v3 | |
with: | |
cache-write-only: true | |
- name: Grant Execute Permission For Gradlew | |
run: chmod +x gradlew | |
- name: Build with Gradle | |
run: ./gradlew bootJar | |
- name: Login to Docker Hub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKER_HUB_ID }} | |
password: ${{ secrets.DOCKER_HUB_PASSWORD }} | |
- name: Image build and push | |
run: | | |
docker build --build-arg PROFILE=test --build-arg DEPENDENCY=build/dependency -t ${{ secrets.DOCKER_REPO_NAME }}/springboot-app:test-latest --platform linux/arm64 . | |
docker push ${{ secrets.DOCKER_REPO_NAME }}/springboot-app:test-latest | |
- name: Upload docker-compose yaml script to artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: docker-compose | |
path: ${{ github.workspace }}/backend/be_app-docker-compose.yml | |
deployA: | |
environment: test | |
runs-on: test-ec2-runner-a | |
needs: build | |
defaults: | |
run: | |
working-directory: ./backend | |
steps: | |
- name: Set docker-compose YAML script to runner | |
uses: actions/download-artifact@v4 | |
with: | |
name: docker-compose | |
path: ${{ github.workspace }}/backend | |
- name: Move docker-compose YAML | |
run: | | |
sudo mv be_app-docker-compose.yml ${{ secrets.DOCKER_COMPOSE_YAML_PATH }}/ | |
- name: Extract secrets as .be_app-env file | |
run: | | |
echo $(pwd) | |
chmod +x ./deploy.sh | |
echo chmod ok | |
source ./deploy.sh | |
echo source ok | |
echo "BLUE_PORT=$BLUE_PORT" >> $GITHUB_ENV | |
echo "GREEN_PORT=$GREEN_PORT" >> $GITHUB_ENV | |
cat <<EOF > ${{ secrets.DOCKER_COMPOSE_YAML_PATH }}/.be_app-env | |
# Docker Hub info from Github Secrets | |
DOCKER_REPO_NAME=${{ secrets.DOCKER_REPO_NAME }} | |
DOCKER_IMAGE_VERSION=test-latest | |
# DB Configuration secrets info from Github Secrets | |
MYSQL_TIME_ZONE=${{ secrets.MYSQL_TIME_ZONE }} | |
DB_BINDING_PORT=${{ secrets.DB_BINDING_PORT }} | |
MASTER_DB_URL=${{ secrets.TEST_SERVER_MASTER_DB_URL }} | |
MASTER_DB_USERNAME=${{ secrets.TEST_SERVER_DB_USERNAME }} | |
MASTER_DB_PASSWORD=${{ secrets.TEST_SERVER_DB_PASSWORD }} | |
SLAVE_DB_URL=${{ secrets.TEST_SERVER_SLAVE_DB_URL }} | |
SLAVE_DB_USERNAME=${{ secrets.TEST_SERVER_DB_USERNAME }} | |
SLAVE_DB_PASSWORD=${{ secrets.TEST_SERVER_DB_PASSWORD }} | |
DDL_AUTO=${{ secrets.DDL_AUTO }} | |
# OAUTH & JWT | |
CLIENT_ID=${{ secrets.TEST_CLIENT_ID }} | |
CLIENT_SECRET=${{ secrets.TEST_CLIENT_SECRET }} | |
CLIENT_REDIRECT_URI=${{ secrets.TEST_CLIENT_REDIRECT_URI }} | |
JWT_KEY=${{ secrets.JWT_KEY}} | |
# INSTANCE NAME | |
INSTANCE_NAME=${{ secrets.INSTANCE_A_NAME }} | |
# Server App | |
BLUE_SERVER_BINDING_PORT=${BLUE_PORT} | |
GREEN_SERVER_BINDING_PORT=${GREEN_PORT} | |
SERVER_LOGS_PATH=${{ secrets.SERVER_LOGS_PATH }} | |
EOF | |
- name: remove old spring-app image | |
run: | | |
sudo docker image rm -f coduo2024/springboot-app:test-latest; | |
- name: Start the new (green/blue) environment | |
run: | | |
sudo docker-compose --env-file ${{ secrets.DOCKER_COMPOSE_YAML_PATH }}/.be_app-env -f ${{ secrets.DOCKER_COMPOSE_YAML_PATH }}/be_app-docker-compose.yml -p $GREEN_PORT up -d | |
- name: Wait for new environment to be healthy | |
run: | | |
until curl -sf http://localhost:${GREEN_PORT}/api/actuator/health | grep "UP"; do sleep 5; done | |
- name: Reload NGINX | |
run: | | |
sudo systemctl reload nginx | |
- name: Stop and remove the old environment | |
run: | | |
sudo docker-compose --env-file ${{ secrets.DOCKER_COMPOSE_YAML_PATH }}/.be_app-env -f ${{ secrets.DOCKER_COMPOSE_YAML_PATH }}/be_app-docker-compose.yml -p $BLUE_PORT down | |
- name: Delete Dangling docker images | |
run: | | |
sudo docker image prune | |
deployB: | |
environment: test | |
runs-on: test-ec2-runner-b | |
needs: build | |
defaults: | |
run: | |
working-directory: ./backend | |
steps: | |
- name: Set docker-compose YAML script to runner | |
uses: actions/download-artifact@v4 | |
with: | |
name: docker-compose | |
path: ${{ github.workspace }}/backend | |
- name: Move docker-compose YAML | |
run: | | |
sudo mv be_app-docker-compose.yml ${{ secrets.DOCKER_COMPOSE_YAML_PATH }}/ | |
- name: Extract secrets as .be_app-env file | |
run: | | |
echo $(pwd) | |
chmod +x ./deploy.sh | |
echo chmod ok | |
source ./deploy.sh | |
echo source ok | |
echo "BLUE_PORT=$BLUE_PORT" >> $GITHUB_ENV | |
echo "GREEN_PORT=$GREEN_PORT" >> $GITHUB_ENV | |
cat <<EOF > ${{ secrets.DOCKER_COMPOSE_YAML_PATH }}/.be_app-env | |
# Docker Hub info from Github Secrets | |
DOCKER_REPO_NAME=${{ secrets.DOCKER_REPO_NAME }} | |
DOCKER_IMAGE_VERSION=test-latest | |
# DB Configuration secrets info from Github Secrets | |
MYSQL_TIME_ZONE=${{ secrets.MYSQL_TIME_ZONE }} | |
DB_BINDING_PORT=${{ secrets.DB_BINDING_PORT }} | |
MASTER_DB_URL=${{ secrets.TEST_SERVER_MASTER_DB_URL }} | |
MASTER_DB_USERNAME=${{ secrets.TEST_SERVER_DB_USERNAME }} | |
MASTER_DB_PASSWORD=${{ secrets.TEST_SERVER_DB_PASSWORD }} | |
SLAVE_DB_URL=${{ secrets.TEST_SERVER_SLAVE_DB_URL }} | |
SLAVE_DB_USERNAME=${{ secrets.TEST_SERVER_DB_USERNAME }} | |
SLAVE_DB_PASSWORD=${{ secrets.TEST_SERVER_DB_PASSWORD }} | |
DDL_AUTO=${{ secrets.DDL_AUTO }} | |
# OAUTH & JWT | |
CLIENT_ID=${{ secrets.TEST_CLIENT_ID }} | |
CLIENT_SECRET=${{ secrets.TEST_CLIENT_SECRET }} | |
CLIENT_REDIRECT_URI=${{ secrets.TEST_CLIENT_REDIRECT_URI }} | |
JWT_KEY=${{ secrets.JWT_KEY}} | |
# INSTANCE NAME | |
INSTANCE_NAME=${{ secrets.INSTANCE_B_NAME }} | |
# Server App | |
BLUE_SERVER_BINDING_PORT=${BLUE_PORT} | |
GREEN_SERVER_BINDING_PORT=${GREEN_PORT} | |
SERVER_LOGS_PATH=${{ secrets.SERVER_LOGS_PATH }} | |
EOF | |
- name: remove old spring-app image | |
run: | | |
sudo docker image rm -f ${{ secrets.DOCKER_REPO_NAME }}/springboot-app; | |
- name: Start the new (green/blue) environment | |
run: | | |
sudo docker-compose --env-file ${{ secrets.DOCKER_COMPOSE_YAML_PATH }}/.be_app-env -f ${{ secrets.DOCKER_COMPOSE_YAML_PATH }}/be_app-docker-compose.yml -p $GREEN_PORT up -d | |
- name: Wait for new environment to be healthy | |
run: | | |
until curl -sf http://localhost:${GREEN_PORT}/api/actuator/health | grep "UP"; do sleep 5; done | |
- name: Reload NGINX | |
run: | | |
sudo systemctl reload nginx | |
- name: Stop and remove the old environment | |
run: | | |
sudo docker-compose --env-file ${{ secrets.DOCKER_COMPOSE_YAML_PATH }}/.be_app-env -f ${{ secrets.DOCKER_COMPOSE_YAML_PATH }}/be_app-docker-compose.yml -p $BLUE_PORT down | |
- name: Delete Dangling docker images | |
run: | | |
sudo docker image prune -a -f |