From 68de920c5d193deab1ca42825ee37c2a66bdadad Mon Sep 17 00:00:00 2001 From: HiHoi Date: Fri, 2 Feb 2024 13:53:29 +0900 Subject: [PATCH 1/2] WIP --- .github/workflows/main_server.yml | 109 ++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 .github/workflows/main_server.yml diff --git a/.github/workflows/main_server.yml b/.github/workflows/main_server.yml new file mode 100644 index 000000000..13f06a793 --- /dev/null +++ b/.github/workflows/main_server.yml @@ -0,0 +1,109 @@ +name: Main CI/CD + +on: + pull_request: + branches: [ "env/main_cicd" ] + types: [ "closed" ] + +permissions: + contents: read + +jobs: + check-branch: + if: contains(github.head_ref, 'release') + runs-on: ubuntu-latest + steps: + - name: Check branch + run: | + echo "This is a release branch" + + check-merged: + needs: check-branch + if: github.event.pull_request.merged == true + runs-on: ubuntu-latest + steps: + - name: Check merged + run: | + echo "This is a merged release branch" + + extract-version: + needs: check-merged + runs-on: ubuntu-latest + outputs: + version: ${{ steps.extract-version.outputs.version }} + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + ref: ${{ env.GITHUB_SHA }} + - name: Extract version + id: extract-version + run: | + echo "Extract version: this version is $(echo "$(git show -s --format=%s)" | egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')" >> "$GITHUB_OUTPUT" + + CI: + name: Main Server CI Check + needs: extract-version + runs-on: ubuntu-latest + permissions: + pull-requests: write + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + submodules: true + token: ${{ secrets.GITHUB_TOKEN }} + - name: Setup Node + uses: actions/setup-node@v2 + with: + node-version: 16 + - name: Check version + run: | + echo "This release version: ${{ needs.extract-version.outputs.version }}" + - name: Install Docker + run: docker compose up -d + - name: Change directory + run: cd ${{ secrets.WORK_DIRECTORY }} // 환경 변수화 필요 + - name: Install dependencies + run: npm install ${{ secrets.WORK_DIRECTORY }} + - name: Prettier + run: npm run prettier + - name: Lint + run: npm run lint + - name: Build Test + run: npm run build + + CD: + name: Main Server CD Check + needs: CI + runs-on: ubuntu-latest + permissions: + pull-requests: write + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + submodules: true + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and push Docker image + run: | + touch ${{ secrets.WORK_DIRECTORY }}/.env.production + echo "${{ secrets.MAIN_ENV }}" >> /home/runner/work/Peer-Frontend/Peer-Frontend/.env.production + echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin + docker build -f Dockerfile -t ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_DEV_IMAGE }} . + docker push ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_DEV_IMAGE }} + + - name: Deploy + uses: appleboy/ssh-action@v1.0.0 + with: + host: ${{ secrets.MAIN_HOST }} + port: 4222 + username: ${{ secrets.MAIN_USERNAME }} + key: ${{ secrets.MAIN_KEY }} + script: | + docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_DEV_IMAGE }} + docker rm -f ${{ secrets.DOCKER_DEV_IMAGE }} + docker run -d -p 80:3000 ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_DEV_IMAGE }} + docker container prune -f + docker image prune -f From 2ec932232c7c6d03960abd0dcee76bedbef161d9 Mon Sep 17 00:00:00 2001 From: HiHoi Date: Fri, 2 Feb 2024 16:03:59 +0900 Subject: [PATCH 2/2] WIP --- .github/workflows/main_server.yml | 34 ++++++++++++++----------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/.github/workflows/main_server.yml b/.github/workflows/main_server.yml index 13f06a793..799836443 100644 --- a/.github/workflows/main_server.yml +++ b/.github/workflows/main_server.yml @@ -2,8 +2,8 @@ name: Main CI/CD on: pull_request: - branches: [ "env/main_cicd" ] - types: [ "closed" ] + branches: ['env/main_cicd'] + types: ['closed'] permissions: contents: read @@ -14,7 +14,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Check branch - run: | + run: | echo "This is a release branch" check-merged: @@ -23,7 +23,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Check merged - run: | + run: | echo "This is a merged release branch" extract-version: @@ -33,12 +33,11 @@ jobs: version: ${{ steps.extract-version.outputs.version }} steps: - name: Checkout - uses: actions/checkout@v4 - with: - ref: ${{ env.GITHUB_SHA }} + uses: actions/checkout@v2 + - name: Extract version id: extract-version - run: | + run: | echo "Extract version: this version is $(echo "$(git show -s --format=%s)" | egrep -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')" >> "$GITHUB_OUTPUT" CI: @@ -49,16 +48,13 @@ jobs: pull-requests: write steps: - name: Checkout - uses: actions/checkout@v4 - with: - submodules: true - token: ${{ secrets.GITHUB_TOKEN }} + uses: actions/checkout@v2 - name: Setup Node uses: actions/setup-node@v2 with: node-version: 16 - name: Check version - run: | + run: | echo "This release version: ${{ needs.extract-version.outputs.version }}" - name: Install Docker run: docker compose up -d @@ -72,7 +68,7 @@ jobs: run: npm run lint - name: Build Test run: npm run build - + CD: name: Main Server CD Check needs: CI @@ -91,8 +87,8 @@ jobs: touch ${{ secrets.WORK_DIRECTORY }}/.env.production echo "${{ secrets.MAIN_ENV }}" >> /home/runner/work/Peer-Frontend/Peer-Frontend/.env.production echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin - docker build -f Dockerfile -t ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_DEV_IMAGE }} . - docker push ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_DEV_IMAGE }} + docker build -f Dockerfile -t ${{ secrets.DOCKER_USERNAME }}/${{ secrets.MAIN_DOCKER_IMAGE }} . + docker push ${{ secrets.DOCKER_USERNAME }}/${{ secrets.MAIN_DOCKER_IMAGE }} - name: Deploy uses: appleboy/ssh-action@v1.0.0 @@ -102,8 +98,8 @@ jobs: username: ${{ secrets.MAIN_USERNAME }} key: ${{ secrets.MAIN_KEY }} script: | - docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_DEV_IMAGE }} - docker rm -f ${{ secrets.DOCKER_DEV_IMAGE }} - docker run -d -p 80:3000 ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_DEV_IMAGE }} + docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.MAIN_DOCKER_IMAGE }} + docker rm -f ${{ secrets.MAIN_DOCKER_IMAGE }} + docker run -d -p 3000:3000 ${{ secrets.DOCKER_USERNAME }}/${{ secrets.MAIN_DOCKER_IMAGE }} docker container prune -f docker image prune -f