From 90ed0defdcf078e2798670bd6487eddbdf715545 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=82=98=EA=B2=BD=ED=98=B8?= Date: Sat, 12 Oct 2024 21:33:30 +0900 Subject: [PATCH] setting: github actions cd --- .github/workflows/cd.yml | 66 ++++++++++++++++++++++++++++++++++++++++ .github/workflows/ci.yml | 2 +- 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/cd.yml diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml new file mode 100644 index 0000000..20078f0 --- /dev/null +++ b/.github/workflows/cd.yml @@ -0,0 +1,66 @@ +name: FINDY EGG CD Workflow + +on: + push: + branches: [ "main", "develop" ] + +permissions: + contents: read + +jobs: + integration: + runs-on: ubuntu-latest + + deploy: + runs-on: ubuntu-latest + + needs: integration + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set environment variables for Docker Compose + run: | + echo "DOCKER_REPO=${{ secrets.DOCKER_REPO }}" >> $GITHUB_ENV + echo "GITHUB_RUN_NUMBER=${{ github.run_number }}" >> $GITHUB_ENV + + - name: Check docker-compose.yml file + run: | + if [ ! -f docker/docker-compose.yml ]; then + echo "Error: docker/docker-compose.yml not found." + exit 1 + fi + + - name: Copy docker-compose into instance + uses: appleboy/scp-action@master + with: + host: ${{ secrets.PUBLIC_DNS }} + username: ${{ secrets.USERNAME }} + key: ${{ secrets.PEM_KEY }} + source: "docker/docker-compose.yml" + target: "/home/${{ secrets.USERNAME }}/docker/docker-compose.yml" + strip_components: 3 + overwrite: true + debug: true + + - name: Deploy to EC2 dev + uses: appleboy/ssh-action@v0.1.0 + if: contains(github.ref, 'develop') + with: + host: ${{ secrets.PUBLIC_DNS }} + username: ${{ secrets.USERNAME }} + key: ${{ secrets.PEM_KEY }} + script: | + cd /home/${{ secrets.USERNAME }} + export DOCKER_REPO=${{ secrets.DOCKER_REPO }} + export GITHUB_RUN_NUMBER=${{ github.run_number }} + echo "DOCKER_REPO=${DOCKER_REPO}" + echo "GITHUB_RUN_NUMBER=${GITHUB_RUN_NUMBER}" + envsubst < <(echo "${{ secrets.DOCKER_COMPOSE_DEV }}") > docker-compose.yml + echo ${{ secrets.DOCKER_PASSWORD }} | sudo docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin + if [ "$(sudo docker ps -q)" ]; then sudo docker stop $(sudo docker ps -q); fi + if [ "$(sudo docker ps -a -q)" ]; then sudo docker rm $(sudo docker ps -a -q); fi + sudo docker pull ${{ secrets.DOCKER_REPO }}:develop-${{ github.run_number }} + sudo docker-compose -f docker-compose-dev.yml up -d + sudo docker image prune -f \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 25fecc8..9c61a49 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,4 +1,4 @@ -name: FINDY CI-CD Workflow +name: FINDY CI Workflow on: push: