{"id":20246696,"url":"https://github.com/civicdatalab/opub-deploy","last_synced_at":"2026-05-06T09:38:04.523Z","repository":{"id":223860643,"uuid":"761730651","full_name":"CivicDataLab/opub-deploy","owner":"CivicDataLab","description":"Guide to set up a Next.js project to automatically deploy to an EC2 instance using GitHub Actions","archived":false,"fork":false,"pushed_at":"2024-02-23T07:44:55.000Z","size":980,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-09-10T03:05:40.367Z","etag":null,"topics":["actions","ec2","nextjs"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CivicDataLab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2024-02-22T11:36:42.000Z","updated_at":"2024-06-19T13:56:20.000Z","dependencies_parsed_at":"2024-02-22T13:28:16.805Z","dependency_job_id":"c181833d-f9bc-4303-b219-7322d2c09561","html_url":"https://github.com/CivicDataLab/opub-deploy","commit_stats":null,"previous_names":["civicdatalab/opub-deploy"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/CivicDataLab/opub-deploy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CivicDataLab%2Fopub-deploy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CivicDataLab%2Fopub-deploy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CivicDataLab%2Fopub-deploy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CivicDataLab%2Fopub-deploy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CivicDataLab","download_url":"https://codeload.github.com/CivicDataLab/opub-deploy/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CivicDataLab%2Fopub-deploy/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27343693,"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","status":"online","status_checked_at":"2025-11-29T02:00:06.589Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["actions","ec2","nextjs"],"created_at":"2024-11-14T09:32:16.261Z","updated_at":"2025-11-29T09:04:43.103Z","avatar_url":"https://github.com/CivicDataLab.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# NextJS Auto Deployment\n\nThis guide will help you set up a Next.js project to automatically deploy to an EC2 instance using GitHub Actions.\n\n## Quick Start\n\nUse the following if you already have a project deployed on EC2. Otherwise, follow the [detailed guide](#detailed-guide).\n\n### PM2 Setup\n\n```bash\n// EC2\nsudo apt update\nnpm install pm2 -g\n\nsudo ln -s \"$NVM_DIR/versions/node/$(nvm version)/bin/node\" \"/usr/local/bin/node\"\nsudo ln -s \"$NVM_DIR/versions/node/$(nvm version)/bin/npm\" \"/usr/local/bin/npm\"\nsudo ln -s \"$NVM_DIR/versions/node/$(nvm version)/bin/pm2\" \"/usr/local/bin/pm2\"\n\n// project directory\npm2 start npm -n deploy -- start\n```\n\n\u003e for custom port `pm2 start npm -n deploy -- start -- -p 3001`\n\n### Adding SSH Key to GitHub\n\n1. Generate a SSH key in EC2 (skip if already have one):\n\n```bash\nssh-keygen -m PEM\n```\n\n2. Move the public key to authorized_keys:\n\n```bash\ncat ~/.ssh/id_rsa.pub \u003e\u003e ~/.ssh/authorized_keys\n```\n\n3. Copy the private key content:\n\n```bash\ncat ~/.ssh/id_rsa\n// Copy the content manually\n```\n\n4. In GitHub, navigate to Repository \u003e Settings \u003e Secrets and variables \u003e Actions and create the following three secrets:\n\n   - `EC2_HOST`: Public IP of your EC2 instance\n\n   - `EC2_USERNAME`: Username to SSH into EC2\n\n   - `EC2_PRIVATE_KEY`: Paste the private key content generated earlier.\n\n### Action Setup\n\nAdd the following code to `.github/workflows/deploy.yml` in your project:\n\n```yaml\nname: Deploy App to EC2\n\non:\n  push:\n    branches:\n      - main\n\njobs:\n  deploy:\n    runs-on: ubuntu-latest\n\n    steps:\n      - name: Checkout code\n        uses: actions/checkout@v4\n\n      - name: Install dependencies\n        run: npm install\n\n      - name: Build\n        run: npm run build\n\n      - name: Rename .next to .next2\n        run: mv .next .next2\n\n      - name: Send .next2 to EC2\n        uses: appleboy/scp-action@master\n        with:\n          host: ${{ secrets.EC2_HOST }}\n          username: ${{ secrets.EC2_USERNAME }}\n          key: ${{ secrets.EC2_PRIVATE_KEY }}\n          source: .next2\n          target: opub-deploy\n      - name: Update with new Build\n        uses: appleboy/ssh-action@v1.0.3\n        with:\n          host: ${{ secrets.EC2_HOST }}\n          username: ${{ secrets.EC2_USERNAME }}\n          key: ${{ secrets.EC2_PRIVATE_KEY }}\n          script: rm -rf opub-deploy/.next; mv opub-deploy/.next2 opub-deploy/.next; pm2 restart opub-deploy\n```\n\n\u003e Replace all instances `opub-deploy` with your project name in EC2.\n\n## Detailed Guide\n\n### GitHub Repository Setup\n\nCreate a GitHub repository, for example: `opub-deploy`.\n\n### Local Development Setup\n\n1.  Generate a Next.js project:\n\n```bash\nnpx create-next-app@latest opub-deploy\ncd opub-deploy\nnpm install\n```\n\n2.  Add the following code to `.github/workflows/deploy.yml` in your project:\n\n```yaml\nname: Deploy Next.js to EC2\n\non:\n  push:\n    branches:\n      - main\n\njobs:\n  deploy:\n    runs-on: ubuntu-latest\n\n    steps:\n      - name: Checkout code\n        uses: actions/checkout@v4\n\n      - name: Install dependencies\n        run: npm install\n\n      - name: Build\n        run: npm run build\n\n      - name: Rename .next to .next2\n        run: mv .next .next2\n\n      - name: Send .next2 to EC2\n        uses: appleboy/scp-action@master\n        with:\n          host: ${{ secrets.EC2_HOST }}\n          username: ${{ secrets.EC2_USERNAME }}\n          key: ${{ secrets.EC2_PRIVATE_KEY }}\n          source: .next2\n          target: opub-deploy\n      - name: Update with new Build\n        uses: appleboy/ssh-action@v1.0.3\n        with:\n          host: ${{ secrets.EC2_HOST }}\n          username: ${{ secrets.EC2_USERNAME }}\n          key: ${{ secrets.EC2_PRIVATE_KEY }}\n          script: rm -rf opub-deploy/.next; mv opub-deploy/.next2 opub-deploy/.next; pm2 restart deploy\n```\n\n\u003e Replace all instances `opub-deploy` with your project name in EC2.\n\n3. Initialize a Git repository:\n\n```bash\ngit init\ngit add .\ngit branch -M main\ngit remote add origin git@github.com:CivicDataLab/opub-deploy.git\ngit commit -m \"first commit\"\ngit push -u origin main\n```\n\n### Server Setup\n\n#### EC2 Setup\n\n1.  Create an EC2 instance with Ubuntu 20.04.\n2.  Create a new key pair and download the `.pem` file.\n3.  Create a new security group with the following inbound rules:\n    1.  SSH (22) from anywhere\n    2.  HTTP (80) from anywhere\n    3.  HTTPS (443) from anywhere\n    4.  Custom TCP (3000) from anywhere\n4.  Connect to the EC2 instance using SSH:\n\n```bash\nssh -i \"path/to/key.pem\" username@publicIP\n```\n\n#### Add packages\n\nInstall Node.js and npm using [NVM](https://github.com/nvm-sh/nvm):\n\n```bash\nsudo apt update\n\ncurl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash\n\nexport NVM_DIR=\"$([ -z \"${XDG_CONFIG_HOME-}\" ] \u0026\u0026 printf %s \"${HOME}/.nvm\" || printf %s \"${XDG_CONFIG_HOME}/nvm\")\"\n[ -s \"$NVM_DIR/nvm.sh\" ] \u0026\u0026 \\. \"$NVM_DIR/nvm.sh\"\n```\n\nInstall PM2 globally:\n\n```bash\nnpm install pm2 -g\n```\n\nCreate symbolic links\n\n```bash\nsudo ln -s \"$NVM_DIR/versions/node/$(nvm version)/bin/node\" \"/usr/local/bin/node\"\nsudo ln -s \"$NVM_DIR/versions/node/$(nvm version)/bin/npm\" \"/usr/local/bin/npm\"\nsudo ln -s \"$NVM_DIR/versions/node/$(nvm version)/bin/pm2\" \"/usr/local/bin/pm2\"\n```\n\n#### Add Repository\n\nClone your GitHub repository and navigate to its directory:\n\n```bash\ngit clone https://github.com/CivicDataLab/opub-deploy.git\ncd opub-deploy\n```\n\nInstall the dependencies and start the server:\n\n```bash\nnpm install\nnpm run build\npm2 start npm -n deploy -- start\n```\n\n\u003e for custom port `pm2 start npm -n deploy -- start -- -p 3001`\n\n### Adding SSH Key to GitHub\n\n1. Generate a SSH key:\n\n```bash\nssh-keygen -m PEM\n```\n\n2. Move the public key to authorized_keys:\n\n```bash\ncat ~/.ssh/id_rsa.pub \u003e\u003e ~/.ssh/authorized_keys\n```\n\n3. Copy the private key content:\n\n```bash\ncat ~/.ssh/id_rsa\n// Copy the content manually\n```\n\n4. In GitHub, navigate to Repository \u003e Settings \u003e Secrets and variables \u003e Actions\n\n5. Create the following three secrets to allow GitHub to SSH into EC2:\n\n   - `EC2_HOST`: Public IP of your EC2 instance\n\n   - `EC2_USERNAME`: Username to SSH into EC2\n\n   - `EC2_PRIVATE_KEY`: Paste the private key content generated earlier.\n\n## Conclusion\n\nNow, every time you push to the main branch, the GitHub Actions workflow will build the project and deploy it to the EC2 instance without any manual intervention and downtime.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcivicdatalab%2Fopub-deploy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcivicdatalab%2Fopub-deploy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcivicdatalab%2Fopub-deploy/lists"}