{"id":21445828,"url":"https://github.com/developer-sujon/github-actions-101","last_synced_at":"2025-03-17T01:23:25.884Z","repository":{"id":189823911,"uuid":"681377657","full_name":"developer-sujon/github-actions-101","owner":"developer-sujon","description":null,"archived":false,"fork":false,"pushed_at":"2024-12-17T05:55:42.000Z","size":930,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-23T11:20:01.527Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/developer-sujon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-21T22:16:10.000Z","updated_at":"2024-12-17T05:55:46.000Z","dependencies_parsed_at":null,"dependency_job_id":"8e2df477-c3dd-41a2-a977-f1da25c369aa","html_url":"https://github.com/developer-sujon/github-actions-101","commit_stats":null,"previous_names":["developer-sujon/cicd","developer-sujon/github-actions-101"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developer-sujon%2Fgithub-actions-101","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developer-sujon%2Fgithub-actions-101/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developer-sujon%2Fgithub-actions-101/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/developer-sujon%2Fgithub-actions-101/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/developer-sujon","download_url":"https://codeload.github.com/developer-sujon/github-actions-101/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243955729,"owners_count":20374374,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-23T02:39:09.842Z","updated_at":"2025-03-17T01:23:25.855Z","avatar_url":"https://github.com/developer-sujon.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# **Automatic Deployment of Node.js Express Server to Self-Hosted Ubuntu VPS**\n\nThis guide walks you through automating the deployment of your **Node.js Express server** to a self-hosted Ubuntu Virtual Private Server (VPS) using **GitHub Actions**.\n\n---\n\n### **Prerequisites**\nEnsure you have the following ready:\n1. A **GitHub account**.\n2. A **VPS** running Ubuntu (e.g., AWS EC2, DigitalOcean, Linode, etc.).\n3. Your **Node.js Express server** code pushed to a GitHub repository.\n4. Basic knowledge of the command-line interface (CLI).\n\n---\n\n## **Step 1: Set Up Your Ubuntu VPS**\n\n1. **Log in to your VPS**:\n   ```bash\n   ssh username@your-vps-ip\n   ```\n\n2. **Update the system and install necessary tools**:\n   ```bash\n   sudo apt update \u0026\u0026 sudo apt upgrade -y\n   sudo apt install -y curl git tar\n   ```\n\n3. **Install Node.js and PM2**:\n   ```bash\n   curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -\n   sudo apt install -y nodejs\n   sudo npm install -g pm2\n   ```\n\n4. **Optional - Set up a firewall**:\n   Configure your firewall to allow SSH, HTTP, and your application port (e.g., `3000`):\n   ```bash\n   sudo ufw allow OpenSSH\n   sudo ufw allow 3000\n   sudo ufw enable\n   ```\n\n---\n\n## **Step 2: Create a GitHub Repository**\n\n1. Go to [GitHub](https://github.com) and create a new repository.\n2. Push your Node.js Express server code to the repository:\n   ```bash\n   git init\n   git remote add origin https://github.com/your-username/your-repo.git\n   git add .\n   git commit -m \"Initial commit\"\n   git branch -M main\n   git push -u origin main\n   ```\n\n---\n\n## **Step 3: Set Up a Self-Hosted Runner on Your VPS**\n\n1. **Navigate to your GitHub Repository**:\n   - Go to **Settings \u003e Actions \u003e Runners** and click **Add Runner**.\n\n2. **Install the GitHub Runner**:\n   Follow the instructions for Linux provided on GitHub, or run:\n   ```bash\n   cd ~\n   curl -o actions-runner-linux-x64.tar.gz -L https://github.com/actions/runner/releases/latest/download/actions-runner-linux-x64.tar.gz\n   mkdir actions-runner \u0026\u0026 tar xzf actions-runner-linux-x64.tar.gz -C actions-runner\n   cd actions-runner\n   ```\n\n3. **Configure the Runner**:\n   Replace `REPO_URL` and `YOUR_TOKEN` with your repository URL and the token provided during setup:\n   ```bash\n   ./config.sh --url https://github.com/your-username/your-repo --token YOUR_TOKEN\n   ```\n\n4. **Start the Runner**:\n   ```bash\n   ./run.sh\n   ```\n\n5. **Optional - Run as a Service**:\n   To ensure the runner starts on boot:\n   ```bash\n   sudo ./svc.sh install\n   sudo ./svc.sh start\n   ```\n\n---\n\n## **Step 4: Create the GitHub Workflow**\n\n1. Inside your repository, create the directory `.github/workflows`.\n2. Add a new file called `deploy.yml` with the following content:\n\n   ```yaml\n   name: Deploy to Self-Hosted VPS\n\n   on:\n     push:\n       branches:\n         - main\n\n   jobs:\n     deploy:\n       name: Deploy to Self-Hosted VPS\n       runs-on: self-hosted\n\n       steps:\n         # Step 1: Checkout repository\n         - name: Checkout repository\n           uses: actions/checkout@v2\n\n         # Step 2: Use Node.js\n         - name: Use Node.js\n           uses: actions/setup-node@v2\n           with:\n             node-version: '16.x'\n\n         # Step 3: Install dependencies\n         - name: Install dependencies\n           run: |\n             npm install --frozen-lockfile\n             npm install -g pm2\n\n         # Step 4: Configure environment variables\n         - name: Configure environment\n           run: echo \"SERVER_PORT=${{ secrets.SERVER_PORT }}\" \u003e\u003e .env\n\n         # Step 5: Build application (if applicable)\n         - name: Build application\n           run: npm run build\n\n         # Step 6: Start the application with PM2\n         - name: Start application\n           run: pm2 start npm --name \"my-app\" -- start\n   ```\n\n---\n\n## **Step 5: Configure GitHub Secrets**\n\n1. Navigate to your repository on GitHub.\n2. Go to **Settings \u003e Secrets** and click **New repository secret**.\n3. Add the required secrets:\n   - **`SERVER_PORT`**: The port number your app will run on (e.g., `3000`).\n   - Add any other environment variables your app requires.\n\n---\n\n## **Step 6: Trigger the Workflow**\n\n1. Push changes to the `main` branch:\n   ```bash\n   git add .\n   git commit -m \"Deploy to VPS\"\n   git push origin main\n   ```\n\n2. Go to the **Actions** tab in your GitHub repository to monitor the workflow.\n\n---\n\n## **Step 7: Monitor and Test**\n\n1. **Check Runner Status**:\n   On GitHub, go to **Settings \u003e Actions \u003e Runners**. Ensure your runner is listed as **online**.\n\n2. **Check Workflow Logs**:\n   If the deployment fails, review the logs in the **Actions** tab to debug any issues.\n\n3. **Test Your Application**:\n   Visit your VPS IP or domain at the specified port (e.g., `http://your-vps-ip:3000`) to ensure your Node.js application is running.\n\n---\n\n### **Additional Tips**\n1. **Using a Custom Domain**: \n   Set up a reverse proxy like **Nginx** to serve your app over a custom domain with HTTPS.\n\n2. **Automate with PM2**:\n   PM2 ensures your application restarts if it crashes:\n   ```bash\n   pm2 startup\n   pm2 save\n   ```\n\n3. **Monitoring**:\n   Use `pm2 monit` to monitor your application in real-time.\n\n---\n\nThis workflow ensures your Node.js Express server is automatically deployed to your Ubuntu VPS upon pushing code changes to your GitHub repository. Let me know if you need further clarification or help!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeveloper-sujon%2Fgithub-actions-101","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeveloper-sujon%2Fgithub-actions-101","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeveloper-sujon%2Fgithub-actions-101/lists"}