Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save naviocean/f69cc22c32939c80e0ebb94b4ac01b3d to your computer and use it in GitHub Desktop.
Save naviocean/f69cc22c32939c80e0ebb94b4ac01b3d to your computer and use it in GitHub Desktop.

Revisions

  1. @danielwetan danielwetan revised this gist Mar 1, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nodejs-cicd-github-actions.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    ## Deploy Node.js to VPS using Github Actions
    ![](https://raw.githubusercontent.com/danielwetan/node1/master/deploy-nodejs-github-actions.png)
    ![](https://raw.githubusercontent.com/danielwetan/node1/master/nodejs-github-actions.png)


    > Steps to deploy Node.js to VPS using PM2 and Github Actions
  2. @danielwetan danielwetan revised this gist Feb 28, 2022. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions nodejs-cicd-github-actions.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    ## Deploy Node.js to VPS using Github Actions
    ![](https://raw.githubusercontent.com/danielwetan/node1/master/deploy-nodejs-github-actions.png)


    > Steps to deploy Node.js to VPS using PM2 and Github Actions

    @@ -118,6 +121,7 @@ HOST = "YOUR SERVER ADDRESS, example: 172.41.91.123"
    USERNAME = "YOUR SERVER USERNAME, example: daniel"
    ```

    ---
    Reference:

    [https://www.youtube.com/watch?v=pvPJARjqAa8](https://www.youtube.com/watch?v=pvPJARjqAa8)
  3. @danielwetan danielwetan revised this gist Feb 28, 2022. 1 changed file with 13 additions and 9 deletions.
    22 changes: 13 additions & 9 deletions nodejs-cicd-github-actions.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    > Steps to deploy Node.js to VPS using PM2 and Github Actions

    #### 1. Clone repo to vps folder
    #### 1. Clone repo to VPS folder

    ```bash
    git clone https://github.com/danielwetan/node1.git
    @@ -11,11 +11,14 @@ git clone https://github.com/danielwetan/node1.git
    #### 2. Setup CI workflows

    location: `node1/.github/workflows/ci.yml`

    example: https://github.com/danielwetan/node1/blob/master/.github/workflows/ci.yml

    ```yaml
    # This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
    # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
    # This workflow will do a clean install of node dependencies,
    # build the source code and run tests across different versions of node
    # For more information see:
    # https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

    name: Node.js CI

    @@ -41,6 +44,7 @@ jobs:
    #### 3. Setup CD workflows
    location: `node1/.github/workflows/cd.yml`

    example: https://github.com/danielwetan/node1/blob/master/.github/workflows/cd.yml

    ```yaml
    @@ -84,11 +88,11 @@ jobs:
    ssh-keygen -t rsa -b 4096 -m PEM -C "github-actions-node1"
    # view the key value
    cat id_rsa-github-node1.pub # public key
    cat id_rsa-github-node1 # private key
    cat id_rsa-github-node1.pub # Public Key
    cat id_rsa-github-node1 # Private Key
    ```

    #### 5. Copy public key to `authorized key`
    #### 5. Copy Public Key to `authorized key`

    ```bash
    cat id_rsa-github-node1.pub
    @@ -97,16 +101,16 @@ cat id_rsa-github-node1.pub
    vi ~/.ssh/authorized_keys
    ```

    #### 6. Copy private key
    #### 6. Copy Private Key
    ```bash
    # Type and copy the output
    cat ~/.ssh/id_rsa
    ```

    #### 7. Setup github secret keys
    #### 7. Setup Github Secret keys

    ```bash
    # Github secret location
    # Github Secret location
    Settings -> Secrets -> Actions -> New repository secret
    PRIVATE_KEY = "Copy generated private key from vps to github secret"
  4. @danielwetan danielwetan revised this gist Feb 28, 2022. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions nodejs-cicd-github-actions.md
    Original file line number Diff line number Diff line change
    @@ -106,6 +106,7 @@ cat ~/.ssh/id_rsa
    #### 7. Setup github secret keys

    ```bash
    # Github secret location
    Settings -> Secrets -> Actions -> New repository secret
    PRIVATE_KEY = "Copy generated private key from vps to github secret"
  5. @danielwetan danielwetan revised this gist Feb 28, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nodejs-cicd-github-actions.md
    Original file line number Diff line number Diff line change
    @@ -71,7 +71,7 @@ jobs:
    key: ${{ secrets.PRIVATE_KEY }}
    port: 22
    script: |
    cd ~/home/danielwetan/apps/node1 # this is your app directory in the server
    cd ~/home/danielwetan/apps/node1
    git pull origin master
    git status
    npm install --only=prod
  6. @danielwetan danielwetan revised this gist Feb 28, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nodejs-cicd-github-actions.md
    Original file line number Diff line number Diff line change
    @@ -71,7 +71,7 @@ jobs:
    key: ${{ secrets.PRIVATE_KEY }}
    port: 22
    script: |
    cd ~/home/danielwetan/apps/node1
    cd ~/home/danielwetan/apps/node1 # this is your app directory in the server
    git pull origin master
    git status
    npm install --only=prod
  7. @danielwetan danielwetan revised this gist Feb 28, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions nodejs-cicd-github-actions.md
    Original file line number Diff line number Diff line change
    @@ -84,8 +84,8 @@ jobs:
    ssh-keygen -t rsa -b 4096 -m PEM -C "github-actions-node1"
    # view the key value
    cat id_rsa-github-node1.pub
    cat id_rsa-github-node1
    cat id_rsa-github-node1.pub # public key
    cat id_rsa-github-node1 # private key
    ```

    #### 5. Copy public key to `authorized key`
  8. @danielwetan danielwetan revised this gist Feb 28, 2022. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions nodejs-cicd-github-actions.md
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,9 @@ git clone https://github.com/danielwetan/node1.git

    #### 2. Setup CI workflows

    location: `node1/.github/workflows/ci.yml`
    example: https://github.com/danielwetan/node1/blob/master/.github/workflows/ci.yml

    ```yaml
    # This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
    # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
    @@ -37,6 +40,9 @@ jobs:
    #### 3. Setup CD workflows
    location: `node1/.github/workflows/cd.yml`
    example: https://github.com/danielwetan/node1/blob/master/.github/workflows/cd.yml

    ```yaml
    # This is a basic workflow to help you get started with Actions
  9. @danielwetan danielwetan revised this gist Feb 28, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions nodejs-cicd-github-actions.md
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ git clone https://github.com/danielwetan/node1.git

    #### 2. Setup CI workflows

    ```jsx
    ```yaml
    # This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
    # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

    @@ -37,7 +37,7 @@ jobs:
    #### 3. Setup CD workflows
    ```jsx
    ```yaml
    # This is a basic workflow to help you get started with Actions

    name: Node.js CD
  10. @danielwetan danielwetan revised this gist Feb 28, 2022. 1 changed file with 16 additions and 13 deletions.
    29 changes: 16 additions & 13 deletions nodejs-cicd-github-actions.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@

    #### 1. Clone repo to vps folder

    ```jsx
    ```bash
    git clone https://github.com/danielwetan/node1.git
    ```

    @@ -74,34 +74,37 @@ jobs:

    #### 4. Setup Private Key and Public Key

    ```jsx
    ```bash
    ssh-keygen -t rsa -b 4096 -m PEM -C "github-actions-node1"

    // view the key value
    # view the key value
    cat id_rsa-github-node1.pub
    cat id_rsa-github-node1
    ```

    #### 5. Copy public key to `authorized key`

    ```jsx
    cat id_rsa-github-node1

    // then copy to
    ```bash
    cat id_rsa-github-node1.pub

    # Copy the output to
    vi ~/.ssh/authorized_keys
    ```

    #### 6. Copy private key to github secret
    #### 6. Copy private key
    ```bash
    # Type and copy the output
    cat ~/.ssh/id_rsa
    ```

    #### 7. Setup github secret keys

    ```jsx
    Settings -> Secrets -> New secret
    ```bash
    Settings -> Secrets -> Actions -> New repository secret

    PRIVATE_KEY = # Copy generated private key from vps to github secret
    HOST = ""
    USERNAME = ""
    PRIVATE_KEY = "Copy generated private key from vps to github secret"
    HOST = "YOUR SERVER ADDRESS, example: 172.41.91.123"
    USERNAME = "YOUR SERVER USERNAME, example: daniel"
    ```

    Reference:
  11. @danielwetan danielwetan revised this gist Feb 15, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nodejs-cicd-github-actions.md
    Original file line number Diff line number Diff line change
    @@ -62,7 +62,7 @@ jobs:
    with:
    host: ${{ secrets.HOST }}
    username: ${{ secrets.USERNAME }}
    key: ${{ secrets.PASSWORD }}
    key: ${{ secrets.PRIVATE_KEY }}
    port: 22
    script: |
    cd ~/home/danielwetan/apps/node1
  12. @danielwetan danielwetan created this gist Jun 6, 2021.
    111 changes: 111 additions & 0 deletions nodejs-cicd-github-actions.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,111 @@
    ## Deploy Node.js to VPS using Github Actions
    > Steps to deploy Node.js to VPS using PM2 and Github Actions

    #### 1. Clone repo to vps folder

    ```jsx
    git clone https://github.com/danielwetan/node1.git
    ```

    #### 2. Setup CI workflows

    ```jsx
    # This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
    # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

    name: Node.js CI

    on:
    pull_request:
    branches: [ master ]

    jobs:
    build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js 12
    uses: actions/setup-node@v2
    with:
    node-version: 12.x
    - run: npm i
    - run: npm run build --if-present
    - run: npm test
    ```

    #### 3. Setup CD workflows

    ```jsx
    # This is a basic workflow to help you get started with Actions

    name: Node.js CD

    # Controls when the action will run.
    on:
    # Triggers the workflow on push or pull request events but only for the master branch
    push:
    branches: [ master ]

    # A workflow run is made up of one or more jobs that can run sequentially or in parallel
    jobs:
    # This workflow contains a single job called "build"
    build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
    - name: Deploy using ssh
    uses: appleboy/ssh-action@master
    with:
    host: ${{ secrets.HOST }}
    username: ${{ secrets.USERNAME }}
    key: ${{ secrets.PASSWORD }}
    port: 22
    script: |
    cd ~/home/danielwetan/apps/node1
    git pull origin master
    git status
    npm install --only=prod
    pm2 restart node1
    ```

    #### 4. Setup Private Key and Public Key

    ```jsx
    ssh-keygen -t rsa -b 4096 -m PEM -C "github-actions-node1"

    // view the key value
    cat id_rsa-github-node1.pub
    cat id_rsa-github-node1
    ```

    #### 5. Copy public key to `authorized key`

    ```jsx
    cat id_rsa-github-node1

    // then copy to

    vi ~/.ssh/authorized_keys
    ```

    #### 6. Copy private key to github secret

    #### 7. Setup github secret keys

    ```jsx
    Settings -> Secrets -> New secret

    PRIVATE_KEY = # Copy generated private key from vps to github secret
    HOST = ""
    USERNAME = ""
    ```

    Reference:

    [https://www.youtube.com/watch?v=pvPJARjqAa8](https://www.youtube.com/watch?v=pvPJARjqAa8)

    [https://github.com/appleboy/ssh-action](https://github.com/appleboy/ssh-action)