DEV Community

Anuchit Prasertsang
Anuchit Prasertsang

Posted on

1

การใช้ GitLab สำหรับแชร์ Configuration ให้คนในทีม โดยไม่ใช้แชท

ปัญหา

เมื่อเราทำงานในทีมที่มีคนหลายคน การแชร์ configuration หรือรหัสต่างๆของโปรเจคสำหรับ ระหว่างนักพัฒนานั้น
เป็นหนึ่งปัญหาที่อยากลำบาก ถ้าไม่มี Vault ส่วนใหญ่ก็จะส่งกันผ่านทางแชท ซึ่งไม่ค่อยปลอดภัยเท่าไหร่

ใช้ GitLab Secure files ในการแชร์ Configuration

สร้างไฟล์ที่จะแชร์ นำไปไว้ที่ GitLab Repository

  1. สร้างไฟล์ที่จะเก็บ configuration ขึ้นมาในโปรเจคของเรา กรณีนี้เราจะสร้างไฟล์ชื่อ env.backend ขึ้นมา ซึ่งเป็น key-value ของ environment ของ backend ตัวอย่างเช่น
ENV_BACKEND=production
DATABASE_URL=postgres://user:password@host:port/dbname
Enter fullscreen mode Exit fullscreen mode
  1. ไปที่ GitLab โปรเจคของเรา แล้วไปที่ Settings > CI/CD > Secure files แล้วอัพโหลดไฟล์ env.backend ขึ้นไป

ใช้ Secure Files API ในการดึงไฟล์จาก GitLab ลงมาที่เครื่องของเรา

  1. สร้าง Personal Access Token ใน GitLab โดยไปที่ User Settings > Access Tokens แล้วสร้าง Token ขึ้นมา
  2. ใช้ Token ที่สร้างขึ้นมาในการดึงไฟล์จาก GitLab โดยใช้ Secure Files API โดยใช้คำสั่ง
    curl --header "PRIVATE-TOKEN: <GITLAB_ACCESS_TOKEN>" \
        "https://gitlab.com/api/v4/projects/<PROJECT_ID>/secure_files/<FILE_ID>/download" \
        --output <OUTPUT_FILE_NAME>
Enter fullscreen mode Exit fullscreen mode

โดยที่

  • <GITLAB_ACCESS_TOKEN> คือ Personal Access Token ที่สร้างขึ้นมา
  • <PROJECT_ID> คือ ID ของโปรเจคที่เรา (อยู่ใกล้ปุ่ม Fork ของโปรเจค หรือไปที่ Settings > General แล้วดูที่ Project ID)
  • <FILE_ID> คือ ID ของไฟล์ที่เราอยากดาวน์โหลด เราสามารถใช้ Secure Files API ในการดูรายละเอียดของไฟล์ทั้งหมดได้
  • <OUTPUT_FILE_NAME> คือ ชื่อไฟล์ที่เราอยากจะบันทึกไฟล์ที่ดาวน์โหลดลงมาในเครื่องของเรา

ดูรายละเอียดของ Secure Files ทั้งหมด

curl --header "PRIVATE-TOKEN: <GITLAB_ACCESS_TOKEN>" \
    "https://gitlab.com/api/v4/projects/<PROJECT_ID>/secure_files"
Enter fullscreen mode Exit fullscreen mode

ตัวอย่างผลลัพธ์

[
        {
                "id": 2233783, // FILE_ID
                "filename": "env.backend", // FILE_NAME
                "filesize": 30,
                "filetype": "text/plain",
                ... // รายละเอียดอื่นๆ
        }
]
Enter fullscreen mode Exit fullscreen mode

เพียงเท่านี้เราก็สามารถแชร์ configuration ระหว่างนักพัฒนาได้โดยไม่ต้องส่งผ่านทางแชทอีกต่อไป

อ้างอิง

Secure File API

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more