Github CI/CD
Contents
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
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 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
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