No menu items!
No menu items!
More

    [Gitlab] Tạo 1 dự án và push code lên Gitlab

    Đầu tiên, hãy chắc chắn rằng máy tính của bạn đã được cài đặt Git, gõ lệnh git version để kiểm tra. Kết quả hiện ra sẽ cho bạn biết phiên bản mà Git được cài đặt, hoặc nếu chưa, nó sẽ báo git is an unknown command. Nếu git đã được cài đặt, thực hiện những bước dưới đây để thực hiện push code lên gitlab nhé (đối với github các bạn làm tương tự).

    1. Chuẩn bị dự án, ví dụ mình có 1 dự án như dưới

    ├── common
    │   ├── authorized_keys
    │   ├── id_rsa
    │   ├── kickstart.yaml
    │   └── metadata.yaml
    ├── main.tf
    ├── scripts
    │   └── generatevars.py
    ├── terraform.tfvars
    ├── variables.tf
    └── vsphere_data.tf

    2. Sử dụng lệnh git init để khởi tạo 1 dự án git

    root@terraform:~/terraform# git init
    Initialized empty Git repository in /root/terraform/.git/

    3. Sử dụng git add <đường dẫn dự án> để thêm toàn bộ các file có trong dự án vào danh sách chuẩn bị thực hiện commit.

    root@terraform:~/terraform# git add .

    4. Bạn có thể kiểm tra trạng thái của các file đã được đưa vào danh sách được git theo dõi.

    root@terraform:~/terraform# git status
    On branch master
    
    No commits yet
    
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
            new file:   common/authorized_keys
            new file:   common/id_rsa
            new file:   common/kickstart.yaml
            new file:   common/metadata.yaml
            new file:   main.tf
            new file:   scripts/generatevars.py
            new file:   terraform.tfvars
            new file:   variables.tf
            new file:   vsphere_data.tf

    5. Thực hiện commit đầu tiên

    root@terraform:~/terraform# git commit -m "The first commit"
    [master (root-commit) 945c3fe] The first commit
     9 files changed, 504 insertions(+)
     create mode 100644 common/authorized_keys
     create mode 100644 common/id_rsa
     create mode 100644 common/kickstart.yaml
     create mode 100644 common/metadata.yaml
     create mode 100644 main.tf
     create mode 100755 scripts/generatevars.py
     create mode 100644 terraform.tfvars
     create mode 100644 variables.tf
     create mode 100644 vsphere_data.tf

    6. Khi commit xong, kiểm tra lại status ta thấy các file đã biến mấy, đơn giản vì nó đã được commit thành công nên không cần theo dõi nữa

    root@terraform:~/terraform# git status
    On branch master
    nothing to commit, working tree clean

    7. Để push code được lên kho chứa như github hay gitlab ta cần thêm thông tin username và email vào git.

    root@terraform:~/terraform# git config --global user.name "hoanghd"
    root@terraform:~/terraform# git config --global user.email "hoanghd164@gmail.com"

    8. Tiếp theo ta thêm kho chứa đã tạo trên gitlab hoặc github

    root@terraform:~/terraform# git remote add vsphere-terraform git@gitlab.com:hoanghd164/vsphere-terraform.git

    Cách lấy link của kho chứa như hình dưới nhé, có thể sử dụng phương thức push code bằng https hoặc ssh tùy bạn.

    9. Verify git remote

    root@terraform:~/terraform# git remote -v
    vsphere-terraform       git@gitlab.com:hoanghd164/vsphere-terraform.git (fetch)
    vsphere-terraform       git@gitlab.com:hoanghd164/vsphere-terraform.git (push)

    10. Thêm sshkey vào cho gitlab hoặc github

    Chúng ta tạo 1 sshkey mới hoặc có thể xài sshkey cũ cũng được, lấy sshkey này thêm vào gitlab hoặc github để khi push code chúng ta không cần phải nhập lại thông tin username và password.

    root@terraform:~/terraform# cat /root/.ssh/authorized_keys
    ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC5jYRFel9+12q/xSSBf49Y2YkZ62MFzzcLr1D9QsnZ9w5jK+5e03sQA9pPKwJnCnbuEDGH1J0xZjQ3cZ03QQrLJ93lFSlR2vAcV5mXQttaRzaKKF8R6mlPXbwoQHysBH/03W72sBZ2WOLWzmYM4IIgoY/wJQodXmNTw96jkn8X+8cHodx55m1K82tWBhGdMc3OkiS98qyJR4nsYRq+3lAbd/aUP8v7N4hSP7zjhrgTaO2jOpnbDJjXYuK8WyT/OenQtbzPsdJcomPEbDN3vtd8Hme9GZ0fqS2RrmhpSzydShbGA+ytW5h82wrXAKQWIaiiS6zLihsLd4nAFjINYbxB

    Nếu chưa có key hãy tạo ra nó bằng command ssh-keygen -t rsa 4056.

    Trên gitlab chúng ta thao tác theo thứ tự như dưới để vào config sshkey.

    Ở bước dưới ta lưu ý bước thứ 4, bước này chúng ta quy định thời gian sshkey sẽ hết hạn, nếu không xác thực được bạn hãy vào đây để kiểm tra xem key của mình còn hạn sử dụng không nhé, nếu đã hết hạn thì bạn hãy gia hạn thêm cho nó.

    10. Và cuối cùng chúng ta push code thôi.

    root@terraform:~/terraform# git push -u --force vsphere-terraform master
    The authenticity of host 'gitlab.com (172.65.251.78)' can't be established.
    ECDSA key fingerprint is SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added 'gitlab.com,172.65.251.78' (ECDSA) to the list of known hosts.
    Enumerating objects: 13, done.
    Counting objects: 100% (13/13), done.
    Delta compression using up to 2 threads
    Compressing objects: 100% (12/12), done.
    Writing objects: 100% (13/13), 6.37 KiB | 6.37 MiB/s, done.
    Total 13 (delta 0), reused 0 (delta 0), pack-reused 0
    To gitlab.com:hoanghd164/vsphere-terraform.git
     * [new branch]      master -> master
    branch 'master' set up to track 'vsphere-terraform/master'.

    11. Verify lại dự án của chúng ta trên trang gitlab

    12. Như vậy quy trình của chúng ta được tóm tắt đơn giản bằng các lệnh như sau

    git init
    git add .
    git commit -m "The first commit"
    git config --global user.name "hoanghd"
    git config --global user.email "hoanghd164@gmail.com"
    git remote add vsphere-terraform git@gitlab.com:hoanghd164/vsphere-terraform.git
    git push -u --force vsphere-terraform master

    Chúc các bạn thành công

    Bài viết gần đây

    spot_img

    Related Stories

    Leave A Reply

    Please enter your comment!
    Please enter your name here

    Đăng ký nhận thông tin bài viết qua email