Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions .github/workflows/exec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Build and Deploy
on:
push:
branches:
- main # Ensure this is a list (with a dash)
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
# 1. Checkout code
- name: Checkout code
uses: actions/checkout@v3
# 2. Install dependencies
- name: Install dependencies
run: npm ci
# 3. Run tests
- name: Run tests
run: npm test
# 4. Build project
- name: Build project
run: npm run build
# 5. Package dist folder
- name: Package dist folder
run: tar -czf dist.tar.gz dist
# 6. Upload artifact for deployment
- name: Upload build artifact
uses: actions/upload-artifact@v4
with:
name: dist
path: dist.tar.gz
deploy:
needs: build-and-test
runs-on: ubuntu-latest
steps:
# 1. Download build artifact
- name: Download build artifact
uses: actions/download-artifact@v4
with:
name: dist
# 2. Set up SSH
- name: Set up SSH
uses: webfactory/ssh-agent@v0.9.0
with:
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
# 3. Add server to known hosts (ADDED THIS STEP!)
- name: Add server to known hosts
run: |
mkdir -p ~/.ssh
ssh-keyscan -H ${{ secrets.SERVER_HOST }} >> ~/.ssh/known_hosts
# 4. Upload build to EC2
- name: Upload build to server
run: scp dist.tar.gz ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }}:${{ secrets.SERVER_PATH }}
# 5. Extract build on EC2
- name: Extract build on server
run: |
ssh "${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }}" << 'EOF'
cd /home/ubuntu/myapp
tar -xzf dist.tar.gz
rm dist.tar.gz
EOF
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,4 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
dist.tar.gz
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Github Actions is a CI/CD tool that allows you to automate your workflow. It is

## Steps
2. Create a new file in the `.github/workflows` directory
3. Define the workflow
3. Define the workflows
4. Push the changes to the repository
5. Check the Actions tab in the repository

Expand All @@ -35,4 +35,4 @@ jobs:
- run: npm test
- run: npm run build

```
```
19 changes: 19 additions & 0 deletions src/.github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Test Project

on:
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest
container:
image: node:20
steps:
- uses: actions/checkout@v3
with:
node-version: 20
- run: npm ci
- run: npm test
- run: npm run build
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import express from "express";
import express from "express"
import cors from "cors";
import body from "body-parser";

Expand Down Expand Up @@ -44,4 +44,4 @@ function stop() {

export { server, startServer, stop };

startServer();
startServer();