Difference between revisions of "Github CI/CD"

From Logic Wiki
Jump to: navigation, search
(Created page with "Category:github Category:CICD In Linux you have to do everything with a user but not root. In order to create a user use '''adduser''' command Like the ''agents''...")
 
(→‎PM2)
 
(11 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
[[Category:CICD]]
 
[[Category:CICD]]
  
 +
== Preparing Linux ==
 
In Linux you have to do everything with a user but not root.  
 
In Linux you have to do everything with a user but not root.  
  
 
In order to create a user use '''adduser''' command
 
In order to create a user use '''adduser''' command
 +
== NGinx setup ==
 +
Follow [[Nginx]]
  
  
 +
== Github Runner ==
 
Like the ''agents'' in TFS Github has ''runners'' So we need to install runners to pull github repositories when they are pushed.  
 
Like the ''agents'' in TFS Github has ''runners'' So we need to install runners to pull github repositories when they are pushed.  
  
Line 29: Line 33:
 
  sudo ./svc.sh start
 
  sudo ./svc.sh start
  
 +
[https://youtu.be/b_sv1iNEA4I https://youtu.be/b_sv1iNEA4I]
  
 +
if there is a problem during uninstall check the folders '''/etc/systemd/system''' and '''/etc/systemd/system/multi-user.target.wants'''
 +
 +
== Sudo Trick ==
 +
sudo visudo -f /etc/sudoers.d/<USERNAME>
 +
in the file
 +
<USERNAME> ALL=(ALL) NOPASSWD: /usr/sbin/service nginx start, /usr/sbin/service nginx stop, /usr/sbin/service nginx restart
 +
With this Linux will not ask sudo pass for stop start restart of nginx
 +
 +
== Github Action ==
 +
Before everything make sure that your project is in the root of git folders. If not build action complains about finding package.json. So here how you can move :
 +
<pre>
 +
git mv sykes/* .
 +
git rm -r sykes
 +
git add *
 +
git commit -m "Folders moved out of sykes directory "
 +
</pre>
 +
 +
For react projects it's better to set CI environment variable to false in '''package.json''' to ignore warnings
 +
<pre>
 +
  "scripts": {
 +
    "build": "CI=false && react-scripts build",
 +
</pre>
 +
* Create a folder in the root  "'''.github'''" and inside of it create another folder "'''workflows'''"
 +
* Create a yaml file with any name like "'''deploytotest.yml'''"
 +
The content of the file is like :
 +
<pre>
 +
name: deploytotest CI
 +
 +
on:
 +
  push:
 +
    branches: [ develop ]
 +
  pull_request:
 +
    branches: [ develop ]
 +
 +
jobs:
 +
  build:
 +
    runs-on: self-hosted
 +
 +
    steps:
 +
      - uses: actions/checkout@v3
 +
      - name: Use Node.js ${{ matrix.node-version }}
 +
        uses: actions/setup-node@v3
 +
        with:
 +
          node-version: ${{ matrix.node-version }}
 +
          cache: 'npm'
 +
      - run: npm ci
 +
      - run: npm run build
 +
</pre>
 +
runs-on is the label / label array '''''runs-on: [self-hosted, linux, x64, gpu]''''' which you can bind runner to actions
 +
---------------------
 +
if everything successful you should see files under /var/www/FrontEnd once you push your codes to develop branch
 +
---------------------
 +
== PM2 ==
 +
sudo npm i pm2@latest -g
 +
after installation
 +
[https://youtu.be/6-RtA6FlbgQ?t=3706 https://youtu.be/6-RtA6FlbgQ?t=3706]
 +
pm2 status
 +
Go to the folder where it needs to be started.
 +
pm2 start npm --name "<PROCESSNAME>" -- run start
 +
<PROCESSNAME> can be anything to identify it later
 +
 +
pm2 delete 0
 +
 +
start is the script definition in package.json
 +
pm2 save
 +
 +
=== Running React sites in PM2 ===
 +
npm install -g serve
 +
npm run build
 +
Now we can run the following command to deploy the app
 +
  pm2 serve <path> <port> --spa
 +
example
 +
pm2 serve build 8082 --spa
 +
Check the status of the application following command in the shell.
 +
pm2 list
 +
Bonus: Below are some utility commands to manage the pm2 process
 +
* Specify the app name pm2 --name <app-name>
 +
* Delete all pm2 process pm2 delete all
 +
* Delete specific process pm2 delete <app-name>
 +
* Check the CPU and memory usage pm2 monit
 +
 +
== Add PM2 to Action ==
 +
<pre>
 +
  ....
 +
      - run: npm run build
 +
      - run pm2 stop 0
 +
      - run pm2 start 0
 +
      - run pm2 save
 +
      - sudo service nginx restart
 +
</pre>
 +
instead of 0 or pid number you can use the names
  
  
 
---------------------
 
---------------------
 
see : [https://www.youtube.com/watch?v=6-RtA6FlbgQ&list=PL_7mM2_n2qvbtLhKo6Ijqt2cnmCAd2JVi&index=10&t=2348s https://www.youtube.com/watch?v=6-RtA6FlbgQ&list=PL_7mM2_n2qvbtLhKo6Ijqt2cnmCAd2JVi&index=10&t=2348s]
 
see : [https://www.youtube.com/watch?v=6-RtA6FlbgQ&list=PL_7mM2_n2qvbtLhKo6Ijqt2cnmCAd2JVi&index=10&t=2348s https://www.youtube.com/watch?v=6-RtA6FlbgQ&list=PL_7mM2_n2qvbtLhKo6Ijqt2cnmCAd2JVi&index=10&t=2348s]

Latest revision as of 23:47, 22 April 2022


Preparing Linux

In Linux you have to do everything with a user but not root.

In order to create a user use adduser command

NGinx setup

Follow Nginx


Github Runner

Like the agents in TFS Github has runners So we need to install runners to pull github repositories when they are pushed.

1 - Go to repository

2 - Go to settings

3 - Go to Actions -> Runners

4 - Click New self-hosted runner

5 - Select the operating system

6 - Go to the folder you want to install the runner and pull the codes in your environment

7 - Give full permission

sudo chmod -R 777 /var/www/FrontEnd

8 - Follow the download steps in github

9 - Follow the Configure steps in github

sudo ./svc.sh install
sudo ./svc.sh start

https://youtu.be/b_sv1iNEA4I

if there is a problem during uninstall check the folders /etc/systemd/system and /etc/systemd/system/multi-user.target.wants

Sudo Trick

sudo visudo -f /etc/sudoers.d/<USERNAME>

in the file

<USERNAME> ALL=(ALL) NOPASSWD: /usr/sbin/service nginx start, /usr/sbin/service nginx stop, /usr/sbin/service nginx restart

With this Linux will not ask sudo pass for stop start restart of nginx

Github Action

Before everything make sure that your project is in the root of git folders. If not build action complains about finding package.json. So here how you can move :

git mv sykes/* .
git rm -r sykes
git add * 
git commit -m "Folders moved out of sykes directory "

For react projects it's better to set CI environment variable to false in package.json to ignore warnings

  "scripts": {
    "build": "CI=false && react-scripts build",
  • Create a folder in the root ".github" and inside of it create another folder "workflows"
  • Create a yaml file with any name like "deploytotest.yml"

The content of the file is like :

name: deploytotest CI

on:
  push:
    branches: [ develop ]
  pull_request:
    branches: [ develop ]

jobs:
  build:
    runs-on: self-hosted

    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'npm'
      - run: npm ci
      - run: npm run build

runs-on is the label / label array runs-on: [self-hosted, linux, x64, gpu] which you can bind runner to actions


if everything successful you should see files under /var/www/FrontEnd once you push your codes to develop branch


PM2

sudo npm i pm2@latest -g

after installation https://youtu.be/6-RtA6FlbgQ?t=3706

pm2 status

Go to the folder where it needs to be started.

pm2 start npm --name "<PROCESSNAME>" -- run start

<PROCESSNAME> can be anything to identify it later

pm2 delete 0

start is the script definition in package.json

pm2 save

Running React sites in PM2

npm install -g serve
npm run build 

Now we can run the following command to deploy the app

 pm2 serve <path> <port> --spa

example

pm2 serve build 8082 --spa 

Check the status of the application following command in the shell.

pm2 list

Bonus: Below are some utility commands to manage the pm2 process

  • Specify the app name pm2 --name <app-name>
  • Delete all pm2 process pm2 delete all
  • Delete specific process pm2 delete <app-name>
  • Check the CPU and memory usage pm2 monit

Add PM2 to Action

  ....
      - run: npm run build
      - run pm2 stop 0
      - run pm2 start 0
      - run pm2 save 
      - sudo service nginx restart

instead of 0 or pid number you can use the names



see : https://www.youtube.com/watch?v=6-RtA6FlbgQ&list=PL_7mM2_n2qvbtLhKo6Ijqt2cnmCAd2JVi&index=10&t=2348s