diff --git a/.github/workflows/exec.yml b/.github/workflows/exec.yml new file mode 100644 index 0000000..aeb9199 --- /dev/null +++ b/.github/workflows/exec.yml @@ -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 diff --git a/.gitignore b/.gitignore index c6bba59..af68306 100644 --- a/.gitignore +++ b/.gitignore @@ -128,3 +128,4 @@ dist .yarn/build-state.yml .yarn/install-state.gz .pnp.* +dist.tar.gz diff --git a/readme.md b/readme.md index ac83b3a..7eae848 100755 --- a/readme.md +++ b/readme.md @@ -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 @@ -35,4 +35,4 @@ jobs: - run: npm test - run: npm run build -``` \ No newline at end of file +``` diff --git a/src/.github/workflows/test.yml b/src/.github/workflows/test.yml new file mode 100644 index 0000000..8509a89 --- /dev/null +++ b/src/.github/workflows/test.yml @@ -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 diff --git a/src/index.ts b/src/index.ts index 71ad74c..decc777 100755 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import express from "express"; +import express from "express" import cors from "cors"; import body from "body-parser"; @@ -44,4 +44,4 @@ function stop() { export { server, startServer, stop }; -startServer(); \ No newline at end of file +startServer();