Skip to content

[BE] 6주차 alpha 서버 배포 #68

[BE] 6주차 alpha 서버 배포

[BE] 6주차 alpha 서버 배포 #68

Workflow file for this run

name: deploy
on:
push:
branches: [alpha]
pull_request:
branches: [alpha]
env:
DOCKER_IMAGE: ${{ vars.DOCKERHUB_USERNAME }}/juga-docker
DOCKER_TAG: ${{ github.sha }}
jobs:
build-and-deploy:
runs-on: ubuntu-latest
strategy:
max-parallel: 1
matrix:
app:
[{ name: 'be', dir: 'BE', port: 3000, container: 'juga-docker-be' }]
steps:
- uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
cache-dependency-path: ./${{matrix.app.dir}}/package-lock.json
- name: Create .env file
run: |
touch ./${{ matrix.app.dir }}/.env
echo "${{ secrets.ENV_ALPHA }}" > ./${{matrix.app.dir}}/.env
- name: Install dependencies
working-directory: ./${{matrix.app.dir}}
continue-on-error: true
run: npm ci
- name: Run tests
if: ${{ matrix.app.name == 'be' }}
working-directory: ./${{matrix.app.dir}}
run: npm test
env:
CI: true
- name: Run linter
working-directory: ./${{matrix.app.dir}}
run: npm run lint
- name: Build application
working-directory: ./${{matrix.app.dir}}
run: npm run build
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and Push Docker Image
working-directory: ./${{ matrix.app.dir }}
env:
NCP_ACCESS_KEY: ${{ secrets.NCP_ACCESS_KEY }}
NCP_SECRET_KEY: ${{ secrets.NCP_SECRET_KEY }}
run: |
docker build -t ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:${{ env.DOCKER_TAG }} .
docker tag ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:${{ env.DOCKER_TAG }} ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:latest
docker push ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:${{ env.DOCKER_TAG }}
docker push ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:latest
- name: Get Github Actions IP
id: ip
uses: haythem/[email protected]
- name: Setting NCP CLI & Credentials
run: |
cd ~
wget https://www.ncloud.com/api/support/download/5/65
unzip 65
mkdir ~/.ncloud
echo -e "[DEFAULT]\nncloud_access_key_id = ${{ secrets.NCP_ACCESS_KEY }}\nncloud_secret_access_key = ${{ secrets.NCP_SECRET_KEY }}\nncloud_api_url = ${{ secrets.NCP_API_URI }}" >> ~/.ncloud/configure
- name: Add Github Action Ip to Security group
run: |
cd ~
ls -la
chmod -R 777 ~/cli_linux
cd ~/cli_linux
./ncloud vserver addAccessControlGroupInboundRule --regionCode KR --vpcNo ${{ secrets.NCP_VPC_ID }} --accessControlGroupNo ${{ secrets.NCP_ACG_ID }} --accessControlGroupRuleList "protocolTypeCode='TCP', ipBlock='${{ steps.ip.outputs.ipv4 }}/32', portRange='${{ secrets.SSH_PORT }}'"
- name: Deploy to NCP Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.NCP_ALPHA_SERVER_HOST }}
username: ${{ secrets.NCP_ALPHA_SERVER_USERNAME }}
key: ${{ secrets.NCP_SERVER_SSH_KEY }}
port: 22
script: |
docker system prune -af
echo "${{ secrets.ENV_ALPHA }}" > .env
docker network create juga-network || true
docker run -d \
--name redis \
--network juga-network \
-p 6379:6379 \
-v redis_data:/data \
redis:latest redis-server --appendonly yes
docker pull ${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:${{ env.DOCKER_TAG }}
docker stop ${{ matrix.app.container }} || true
docker rm ${{ matrix.app.container }} || true
docker run -d \
--name ${{ matrix.app.container }} \
--network juga-network \
-p ${{ matrix.app.port }}:${{ matrix.app.port }} \
--env-file .env \
${{ env.DOCKER_IMAGE }}-${{ matrix.app.name }}:${{ env.DOCKER_TAG }}
- name: Remove Github Action Ip to Security group
run: |
chmod -R 777 ~/cli_linux
cd ~/cli_linux
./ncloud vserver removeAccessControlGroupInboundRule --regionCode KR --vpcNo ${{ secrets.NCP_VPC_ID }} --accessControlGroupNo ${{ secrets.NCP_ACG_ID }} --accessControlGroupRuleList "protocolTypeCode='TCP', ipBlock='${{ steps.ip.outputs.ipv4 }}/32', portRange='${{ secrets.SSH_PORT }}'"