Showing posts with label Git. Show all posts
Showing posts with label Git. Show all posts

Wednesday, 15 May 2024

Using GitHub Actions Deploy A Git Repository To A Server

1. Create ssh key for server to pull sources from git repository

2. Create ssh key for Github remote control server

- remote to your server

$ ssh-keygen -t rsa -b 4096 

$ cat /root/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

- copy private key to assign for github

$ cat /root/.ssh/id_rsa 

3. Setup Repository secret

+ goto repository/ Setting/ Secret and variables/ Action/ New repository secret

(1) SSH_PRIVATE_KEY

/ name: SSH_PRIVATE_KEY

/ secret: copy from id_rsa

(2). SSH_USER

/ name: SSH_USER

/ secret: root (or other user_name in server)

* Case use other user need set permission for this user to access workdir

(3). SSH_HOST

/ name: SSH_HOST

/ secret: IP_Address

(3).WORK_DIR

/ name: WORK_DIR

/ secret: ~/var/www/<dir_source_code>

(3).MAIN_BRANCH

/ name: MAIN_BRANCH

/ secret: main

4. Setup Workflow for git Action

+ goto repository/ Actions/ setup a workflow yourself


on:
  push:
    branches:
      - main
  workflow_dispatch:
 
jobs:
  run_pull:
    name: run pull
    runs-on: ubuntu-latest
   
    steps:
    - name: install ssh keys
      # check this thread to understand why its needed:
      # https://stackoverflow.com/a/70447517
      run: |
        install -m 600 -D /dev/null ~/.ssh/id_rsa
        echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
        ssh-keyscan -H ${{ secrets.SSH_HOST }} > ~/.ssh/known_hosts
    - name: connect and pull
      run: ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "cd ${{ secrets.WORK_DIR }} && git checkout ${{ secrets.MAIN_BRANCH }} && git pull && exit"
    - name: cleanup
      run: rm -rf ~/.ssh
 

+ Update for multi branch

name: Deploy to Server

on:
  push:
    branches:
      - main
      - develop
  workflow_dispatch:

jobs:
  run_pull:
    name: Deploy to Appropriate Server
    runs-on: ubuntu-latest

    steps:
    - name: Install SSH keys
      # Check this thread to understand why it's needed:
      # https://stackoverflow.com/a/70447517
      run: |
        install -m 600 -D /dev/null ~/.ssh/id_rsa
        echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
        ssh-keyscan -H ${{ secrets.SSH_HOST_MAIN }} ${{ secrets.SSH_HOST_DEVELOP }} > ~/.ssh/known_hosts

    - name: Determine branch
      id: get_branch
      run: echo "::set-output name=branch::${GITHUB_REF#refs/heads/}"

    - name: Deploy to Main Server
      if: github.ref == 'refs/heads/main'
      run: |
        ssh ${{ secrets.SSH_USER_MAIN }}@${{ secrets.SSH_HOST_MAIN }} "cd ${{ secrets.WORK_DIR_MAIN }} && git checkout main && git pull && exit"

    - name: Deploy to Develop Server
      if: github.ref == 'refs/heads/develop'
      run: |
        ssh ${{ secrets.SSH_USER_DEVELOP }}@${{ secrets.SSH_HOST_DEVELOP }} "cd ${{ secrets.WORK_DIR_DEVELOP }} && git checkout develop && git pull && exit"

    - name: Cleanup
      run: rm -rf ~/.ssh
 

* Errors can happen

Thank you


Wednesday, 8 May 2024

CICD with Git Actions in Github

1. What to use Git Actions for?

- Git Action is an event-driven, when it received one event, it will run scripts.
- Git Action used to build CICD 

2. How to use Git Actions ?

- Step1: Create repository GitActionDemo
- Step2: Prepare environment variable Action secrets
- Step3: Create Workflows: Create file cicd.yml in folder .github/workflows
- Step4: Create job .yml file

name: Deploy to Server Poppin

on:
  push:
    branches:
      - master
      - staging
      - develop

  workflow_dispatch:
 
jobs:
  run_pull:
    name: Deploy to Server Poppin
    runs-on: ubuntu-latest

    steps:
      - run: echo "CHECK Install SSH keys "
      - name: Install SSH keys
        run: |
          install -m 600 -D /dev/null ~/.ssh/id_rsa
          echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
          ssh-keyscan -H ${{ secrets.SSH_HOST }} > ~/.ssh/known_hosts
       
      - run: echo "Determine branch"
      - name: Determine branch
        id: get_branch
        run: echo "branch=$(echo "${GITHUB_REF}" | sed 's|refs/heads/||')" >> $GITHUB_ENV
     
      - name: Deploy to Master Server
        if: github.ref == 'refs/heads/master'
        run: |
          ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "\
          cd ~/deploy &&\
          vendor/bin/dep deploy eccube1 &&\
          vendor/bin/dep deploy eccube2 &&\
          pwd"
 
      - name: Deploy to Staging Server
        if: github.ref == 'refs/heads/staging'
        run: |
          ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "\
          cd ~/deploy &&\
          vendor/bin/dep deploy eccubestaging &&\
          pwd"
 
      - name: Deploy to Develop Server
        if: github.ref == 'refs/heads/develop'
        run: |
          ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "\
          cd ~/deploy &&\
          vendor/bin/dep deploy eccubedev &&\
          pwd"
       
      - name: Cleanup
        run: rm -rf ~/.ssh
 

Combine with deployer in php

3. What does Git Actions include?

- Workflows: Script auto run one or multi jobs, according period or event.
- Events: Action to trigger a Workflows
- Jobs: Collection steps to run in one runner.
- Steps: Commands in job
- Action: ?
- Runners: Server Github put your app to run: Ubuntu Linux, Window, macOS.

-------------------------
Detailed instructions

Reference:
- https://docs.github.com/en/actions

Thank you.

Monday, 1 April 2024

Git tutorial from basic to advanced

Git Basics:
Introduction to version control and Git
Installing Git and configuring user settings
Initializing a Git repository (git init)
Basic Git commands: git add, git commit, git status, git log
Working with Branches:
Understanding branches in Git
Creating and switching branches (git branch, git checkout)
Merging branches (git merge)
Resolving merge conflicts
Collaborating with Remotes:
Adding remote repositories (git remote add)
Cloning remote repositories (git clone)
Fetching and pulling changes from remote (git fetch, git pull)
Pushing changes to remote (git push)
Collaborative workflows (Forking, Pull Requests)
Advanced Branching and Merging:
Branching strategies (Feature branching, Gitflow)
Rebasing branches (git rebase)
Interactive rebase (git rebase -i)
Cherry-picking commits (git cherry-pick)
Git Tools and Customization:
Git aliases for custom commands
Configuring Git settings globally and per repository
Git GUI tools and IDE integrations
Rewriting History:
Amending commits (git commit --amend)
Squashing commits (git rebase -i)
Rewriting commit history (git rebase -i, git filter-branch)
Advanced Collaboration:
Collaborating with multiple remotes
Submodules and subtrees
Git hooks for automating tasks
Managing large repositories with Git LFS (Large File Storage)
Git Internals:
Understanding Git objects (blobs, trees, commits)
Git references and the reflog
Exploring Git repository structure
Git Workflow Best Practices:
Commit message conventions
Code review best practices
Continuous Integration and Deployment (CI/CD) with Git
Troubleshooting and Recovery:
Undoing changes (git checkout, git reset)
Recovering lost commits (git reflog)
Restoring deleted branches or commits
Git in Real-World Scenarios:
Git branching and merging strategies in a team environment
Handling project dependencies with Git submodules
Versioning configuration files and environments
Git Security and Access Control:
Securing Git repositories (SSH, HTTPS, authentication)
Access control with Git hosting platforms (GitHub, GitLab, Bitbucket)
Git security best practices and avoiding common pitfalls

Thank you

Friday, 23 February 2024

Install Git

* Install Git

$ sudo apt install git-all

* Connect to github.com using ssh in linux

- generate ssh key

$ ssh-keygen -t ed25519 -C "your_email@example.com"

+ enter ssh file name: id_ed25519

+ enter password, re password

- Start the ssh-agent in the background

$ eval "$(ssh-agent -s)"

- add your SSH private key to the ssh-agent.

$ ssh-add ~/.ssh/id_ed25519

- add config file

$ sudo nano config

Host github.com
  IdentityFile ~/.ssh/
id_ed25519

- add key in id_ed25519.pub to ssh key in guthub

- check ssh success

$ ssh -T git@github.com

* >> some error can happen

Refference: 

> https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

Thursday, 22 February 2024

Errors when ssh to github but don't has permission

1. not add config for authentication

~/.ssh/config

Host github.com
  IdentityFile ~/.ssh/ssh_github_vps

Host bitbucket.org
  IdentityFile ~/.ssh/ssh_bitbucket_vps

2. Wrong user authentiaction

- you access to user by user user_xxx

- you don't have permission to call git clone then you use sudo => you is root user

=> wrong user in config of ssh => error don't has permission

Fix:

- whoami => user_xxx

- groups => groups u in => user_xxx

sudo chow -R user_xxx:user_xxx www

(www is folder has source)

Thank you


Tuesday, 5 September 2023

Git merge, reset, commit --amend, rebase, pick cherry, handle conflict

There are two branch
commit on branch A: 123|4.......5
commit on branch B: 123|...678 (checkout from branh A from commit 3, after add commit 678)
---------------------------------------------------
* Git merge: 
add commits from branch B to branch A follow time create commit
- git checkout A
- git merge B
- handle conflict commits: 3 5 8
- git add .
- git commit -m"M"
=> A: 123|4|678|5|M
--------------------------------------------------
* Git rebase: 
add all commits from branch B to branch A at left side of branch A from postion of commit of both branch
- git checkout A
- git rebase B
- handle conflict commit: 8 to 4 -> git add . -> git rebase --continue => A: 123|678|4
- handle conflict commit: 4 to 5 -> git add . -> git rebase --continue => A: 123|678|45
- alert result rebase, handle commits with p,s,.. can change name commit in nano, ex 4 => 4'
- ctrl+x -> y to save
=> A: 123|678|4'5
* git rebase itself: select commit want to keep
- git rebase -i idCommit
- git checkout A (A: 12345)
- git rebsae -i 2
- handle conflict commit: 2 to 3 -> git add . -> git rebase --continue => A: 12|3
- handle conflict commit: 3 to 4 -> git add . -> git rebase --continue => A: 12|34
- handle conflict commit: 4 to 5 -> git add . -> git rebase --continue => A: 12|345
- alert result rebase, handle commits with p,s,.. can change name commit in nano, ex 3 => 3', 4->4'
- ctrl+x -> y to save
=> A: 12|3'4'5
---------------------------------------------------
* Git reset: remove commit from a branch 
+ git reset --soft idCommit  # Removes from HEAD to idCommit, keeps changed staged
+ git reset --hard idCommit # Removes from HEAD to idCommit, DONT keeps changed staged
---------------------------------------------------
* Git commit --amend: to change a last commit message.
+ git commit --amend -m "........"
+ git push origin BranchName -f
* Note: 
 
- Have branh A, PR A to DEV, PM agree merge
- if you commit --amend branch A, after PR A to DEV -> Conflict
---------------------------------------------------

* Git pick cherry: pick commit from a Branch to add for orther branch
- git checkout A
- git cherry-pick -e 7 (git cherry-pick -e idCommit)
-> A: 12345|7
or
- git checkout B
- git cherry-pick -e 5 (git cherry-pick -e idCommit)
-> B: 123678|5
---------------------------------------------------
* Git handle conflict when PR
- git checkout A
- git add .
- git commit -m"..."
- git push origin A
- PR: from A to DEV -> conflict
=> handle
- git checkout A
- git checkout -b A-fix-conflict // create this branch to fix-conflict // because dont want get code from DEV to merge to A
- git checkout DEV
- git pull origin DEV
- git checkout A-fix-conflict
- git merge DEV
- handle fix conflict in local
- git push origin A-fix-conflict
- PR from A-fix-conflict to DEV 
- PM aggree merge
=> now A can PR to DEV


Thank you


 

Publish npm package

  Để publish   pav-kit  lên NPM, bạn hãy làm theo các bước dưới đây. Tôi đã tạo thêm file  index.js  để đảm bảo gói tin hợp lệ. Bước 1: Tạo ...