Update index.html #17
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: Update and Deploy Node.js App as Container | |
on: | |
push: | |
branches: [main] | |
pull_request: | |
branches: [main] | |
jobs: | |
update_repository: | |
runs-on: ubuntu-latest | |
services: | |
docker: | |
image: docker:latest | |
options: --privileged | |
steps: | |
- name: Checkout Code | |
uses: actions/checkout@v3 | |
- name: Setup Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: '16' | |
- name: Install npm dependencies | |
run: npm ci | |
- name: Build Docker image | |
run: docker build -t node-app:latest . | |
- name: Verify Docker image | |
run: docker images | |
deploy_to_ec2: | |
runs-on: ubuntu-latest | |
needs: update_repository | |
steps: | |
- name: Deploy to EC2 | |
uses: easingthemes/ssh-deploy@main | |
env: | |
SSH_PRIVATE_KEY: ${{ secrets.EC2_SSH_KEY }} | |
REMOTE_HOST: ${{ secrets.HOST_DNS }} | |
REMOTE_USER: ${{ secrets.USERNAME }} | |
TARGET: ~/my-node-app | |
SOURCE: ./ | |
- name: Build Docker image on EC2 | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.HOST_DNS }} | |
username: ${{ secrets.USERNAME }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
port: 22 | |
script: | | |
cd ~/my-node-app | |
sudo docker build -t node-app:latest . | |
- name: Run Docker container on EC2 | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.HOST_DNS }} | |
username: ${{ secrets.USERNAME }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
port: 22 | |
script: | | |
# Stop and remove existing container named node-app if it exists | |
if [ "$(sudo docker ps -q -f name=node-app)" ]; then | |
sudo docker stop node-app | |
sudo docker rm node-app | |
fi | |
# Run the newly built Docker image | |
sudo docker run -d -p 3000:3000 --name node-app node-app:latest |