Created
December 20, 2024 20:56
-
-
Save azizoid/f4bf943e0042a1210d05b1004dfb4989 to your computer and use it in GitHub Desktop.
This file contains hidden or 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: Build and Deploy | |
| on: | |
| push: | |
| branches: | |
| - main | |
| env: | |
| SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | |
| HOST: ${{ secrets.HOST }} | |
| USERNAME: ${{ secrets.USERNAME }} | |
| TARGET_DIRECTORY: ${{ secrets.TARGET_DIRECTORY }} | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| node-version: [21] | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - uses: pnpm/action-setup@v4 | |
| with: | |
| version: 9.12.3+sha512.cce0f9de9c5a7c95bef944169cc5dfe8741abfb145078c0d508b868056848a87c81e626246cb60967cbd7fd29a6c062ef73ff840d96b3c86c40ac92cf4a813ee | |
| - name: Use Node.js ${{ matrix.node-version }} | |
| uses: actions/[email protected] | |
| with: | |
| node-version: ${{ matrix.node-version }} | |
| cache: 'pnpm' | |
| - name: Install dependencies | |
| run: pnpm install | |
| - name: Cache pnpm and Next.js build cache | |
| uses: actions/cache@v4 | |
| with: | |
| path: | | |
| ~/.pnpm-store | |
| ${{ github.workspace }}/.next/cache | |
| key: | |
| ${{ runner.os }}-nextjs-${{ hashFiles('**/pnpm-lock.yaml') }}-${{ hashFiles('**/*.js', | |
| '**/*.jsx', '**/*.ts', '**/*.tsx') }} | |
| restore-keys: | | |
| ${{ runner.os }}-nextjs-${{ hashFiles('**/pnpm-lock.yaml') }}- | |
| - name: Build with Next.js | |
| run: pnpm next build | |
| - name: Upload artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: webfiles-next-build | |
| path: ./.next | |
| include-hidden-files: true | |
| deploy: | |
| needs: build | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Setup SSH | |
| uses: webfactory/ssh-agent@master | |
| with: | |
| ssh-private-key: ${{ env.SSH_PRIVATE_KEY }} | |
| - name: Download artifact | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: webfiles-next-build | |
| path: ./.next | |
| - name: Upload to Ubuntu server | |
| uses: appleboy/scp-action@master | |
| with: | |
| host: ${{ env.HOST }} | |
| username: ${{ env.USERNAME }} | |
| key: ${{ env.SSH_PRIVATE_KEY }} | |
| source: ./* | |
| target: ${{ env.TARGET_DIRECTORY }} | |
| - name: Execute deployment commands on the server | |
| uses: appleboy/ssh-action@master | |
| with: | |
| host: ${{ env.HOST }} | |
| username: ${{ env.USERNAME }} | |
| key: ${{ env.SSH_PRIVATE_KEY }} | |
| script: | | |
| cd ${{ env.TARGET_DIRECTORY }} | |
| git fetch | |
| git checkout main | |
| git pull | |
| pnpm install | |
| pm2 restart webfiles.com | |
| echo "Files uploaded successfully!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment