huyleminh is building and deploying automatically #4
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: Socket Service Deployment | |
run-name: ${{ github.actor }} is building and deploying automatically | |
on: | |
pull_request: | |
branches: | |
- master | |
workflow_dispatch: | |
env: | |
COMPLETE_IMAGE_NAME: ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }} | |
jobs: | |
build_source: | |
name: Build source code | |
runs-on: ubuntu-22.04 | |
steps: | |
- name: Checkout source code | |
uses: actions/checkout@v3 | |
- name: Setup NodeJS environment | |
uses: actions/setup-node@v3 | |
with: | |
node-version: "16" | |
cache: "npm" | |
- name: Setup our npm registry | |
run: echo "${{ secrets.NPMRC_FILE_CONTENT }}" > .npmrc | |
- name: Install dependencies | |
run: npm ci | |
- name: Build source code | |
run: npm run build --if-present | |
# - name: Test source code | |
# run: npm test --if-present | |
publish_to_registry: | |
name: Build and push docker image to docker registry | |
runs-on: ubuntu-22.04 | |
needs: build_source | |
steps: | |
- name: Checkout source code | |
uses: actions/checkout@v3 | |
- name: Setup our npm registry | |
run: echo "${{ secrets.NPMRC_FILE_CONTENT }}" > .npmrc | |
- name: Login to Docker Hub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_ACCESS_TOKEN }} | |
- name: Extract metadata (tags, labels) for Docker | |
id: meta | |
uses: docker/metadata-action@v4 | |
with: | |
images: ${{ env.COMPLETE_IMAGE_NAME }} | |
tags: ${{ vars.DOCKER_IMAGE_TAG }} | |
- name: Build and push Docker image to registry | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
push: true | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
# deployment: | |
# name: Pull and up the docker image from docker registry | |
# runs-on: ubuntu-22.04 | |
# needs: publish_to_registry | |
# steps: | |
# - name: Connect to EC2 server | |
# run: | | |
# echo "${{ secrets.SSH_PRIVATE_KEY }}" > private_key && chmod 600 private_key | |
# ssh -o StrictHostKeyChecking=no -i private_key ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "sudo su - && cd ${{ secrets.SSH_WORKING_DIR }} && sudo docker pull sehcmusgroup1/socket-service:latest && sudo docker compose up -d socket-service && exit && exit" | |
# - name: Clean private key | |
# run: rm private_key | |
render-deployment: | |
name: Trigger render deploy hook | |
runs-on: ubuntu-22.04 | |
needs: publish_to_registry | |
steps: | |
- name: Call depoy hook api | |
run: curl "${{ secrets.DEPLOY_HOOK_URL }}" |