From e14056bcffc991d384e7eb326a39b5e5775640ee Mon Sep 17 00:00:00 2001 From: withsang Date: Sun, 17 Sep 2023 09:49:29 +0900 Subject: [PATCH] feat(actions): add cd to dev and prod --- .github/workflows/dev-build-image.yaml | 36 ++++++++++++++++++++++ .github/workflows/dev-deploy.yaml | 32 ++++++++++++++++++++ .github/workflows/prod-build-image.yaml | 40 +++++++++++++++++++++++++ .github/workflows/prod-deploy.yaml | 32 ++++++++++++++++++++ 4 files changed, 140 insertions(+) create mode 100644 .github/workflows/dev-build-image.yaml create mode 100644 .github/workflows/dev-deploy.yaml create mode 100644 .github/workflows/prod-build-image.yaml create mode 100644 .github/workflows/prod-deploy.yaml diff --git a/.github/workflows/dev-build-image.yaml b/.github/workflows/dev-build-image.yaml new file mode 100644 index 0000000..4a63753 --- /dev/null +++ b/.github/workflows/dev-build-image.yaml @@ -0,0 +1,36 @@ +name: Build docker image on push to develop + +on: + push: + branches: ["develop"] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Cache Docker layers + uses: actions/cache@v3 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- + - name: Log in to GitHub Container Registry + run: echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u USERNAME --password-stdin + - name: Build and push Image + id: docker-build + uses: docker/build-push-action@v5 + env: + IMAGE_TAG: dev + with: + push: true + tags: "ghcr.io/sparcs-kaist/zabo-server:${{ env.IMAGE_TAG }}" + cache-from: type=local,src=/tmp/.buildx-cache + cache-to: type=local,dest=/tmp/.buildx-cache-new + - name: Remove old cache + run: | + rm -rf /tmp/.buildx-cache + mv /tmp/.buildx-cache-new /tmp/.buildx-cache diff --git a/.github/workflows/dev-deploy.yaml b/.github/workflows/dev-deploy.yaml new file mode 100644 index 0000000..9a70279 --- /dev/null +++ b/.github/workflows/dev-deploy.yaml @@ -0,0 +1,32 @@ +name: Deploy to dev server + +# when dev image build action is completed, run this action +on: + workflow_run: + workflows: ["Build docker image on push to develop"] + types: + - completed + +jobs: + if_workflow_success: + name: Deploy to dev server + runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.conclusion == 'success' }} + + steps: + - name: pull the image and restart the container + uses: appleboy/ssh-action@v1.0.0 + with: + host: ${{ secrets.DEV_HOST }} + port: ${{ secrets.DEV_PORT }} + username: ${{ secrets.DEV_USERNAME }} + password: ${{ secrets.DEV_PASSWORD }} + proxy_host: ${{ secrets.DEV_PROXY_HOST }} + proxy_port: ${{ secrets.DEV_PROXY_PORT }} + proxy_username: ${{ secrets.DEV_PROXY_USERNAME }} + proxy_password: ${{ secrets.DEV_PROXY_PASSWORD }} + script_stop: true # stop script if any command has failed + script: | + cd ${{ secrets.DEV_WORKING_DIRECTORY }} + docker compose -f .docker/docker-compose.stage.yml -p zabo --env-file=.docker/.env.prod pull + docker compose -f .docker/docker-compose.stage.yml -p zabo --env-file=.docker/.env.prod up --force-recreate --build -d \ No newline at end of file diff --git a/.github/workflows/prod-build-image.yaml b/.github/workflows/prod-build-image.yaml new file mode 100644 index 0000000..182f4c8 --- /dev/null +++ b/.github/workflows/prod-build-image.yaml @@ -0,0 +1,40 @@ +name: Build docker image on release tag created + +on: + push: + tags: + - "**" + +jobs: + build: + name: Build and push Image to GitHub Container Registry + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Cache Docker layers + uses: actions/cache@v3 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- + - name: Log in to GitHub Container Registry + run: echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u USERNAME --password-stdin + - name: Build and push Image + id: docker-build + uses: docker/build-push-action@v5 + env: + IMAGE_TAG: ${{github.ref_name}} + with: + push: true + tags: | + "ghcr.io/sparcs-kaist/zabo-server:${{ env.IMAGE_TAG }}" + "ghcr.io/sparcs-kaist/zabo-server:latest" + cache-from: type=local,src=/tmp/.buildx-cache + cache-to: type=local,dest=/tmp/.buildx-cache-new + - name: Remove old cache + run: | + rm -rf /tmp/.buildx-cache + mv /tmp/.buildx-cache-new /tmp/.buildx-cache diff --git a/.github/workflows/prod-deploy.yaml b/.github/workflows/prod-deploy.yaml new file mode 100644 index 0000000..f688600 --- /dev/null +++ b/.github/workflows/prod-deploy.yaml @@ -0,0 +1,32 @@ +name: Deploy to prod server + +# when prod image build action is completed, run this action +on: + workflow_run: + workflows: ["Build docker image on release tag created"] + types: + - completed + +jobs: + if_workflow_success: + name: Deploy to prod server + runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.conclusion == 'success' }} + + steps: + - name: pull the image and restart the container + uses: appleboy/ssh-action@v1.0.0 + with: + host: ${{ secrets.PROD_HOST }} + port: ${{ secrets.PROD_PORT }} + username: ${{ secrets.PROD_USERNAME }} + password: ${{ secrets.PROD_PASSWORD }} + proxy_host: ${{ secrets.PROD_PROXY_HOST }} + proxy_port: ${{ secrets.PROD_PROXY_PORT }} + proxy_username: ${{ secrets.PROD_PROXY_USERNAME }} + proxy_password: ${{ secrets.PROD_PROXY_PASSWORD }} + script_stop: true # stop script if any command has failed + script: | + cd ${{ secrets.PROD_WORKING_DIRECTORY }} + yarn docker:prod pull + yarn docker:prod up --force-recreate --build -d