DEV Community

Perm Chao
Perm Chao

Posted on • Edited on

3 3

Basic in .gitlab-ci.yml config

ขั้นตอนการใช้งาน

1) ลง Gitlab runner ในเครื่องที่เราต้องการให้มันทำงานด้วย

  • download ตัวโปรแกรมมาก่อน โดยที่ ${arch} ให้แทนที่ด้วย
    • amd64, arm หรือ arm64
curl -LJO "https://gitlab-runner-downloads.s3.amazonaws.com/latest/deb/gitlab-runner_${arch}.deb"
Enter fullscreen mode Exit fullscreen mode
  • ลงโปรแกรม
dpkg -i gitlab-runner_${arch}.deb
Enter fullscreen mode Exit fullscreen mode
  • เพิ่มสิทธิ์ให้กับโปรแกรม gitlab-runner
sudo chmod +x /usr/local/bin/gitlab-runner
Enter fullscreen mode Exit fullscreen mode
  • สั่งให้ gitlab-runner ทำงานบน user gitlab-runner และเก็บ build cache ไว้ที่ /home/gitlab-runner
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start
Enter fullscreen mode Exit fullscreen mode

2) ทำการผูก repository เข้ากับโปรแกรม gitlab-runner

  • ไปเอาข้อมูล token ของ repository มาจากเว็บโดยเข้าที่เมนูด้านข้างของ repository บนเว็บที่ชื่อว่า Settings > CI / CD

ถ้าใครไม่เห็นเมนู Settings อาจจะเกี่ยวข้องกับ User ของท่านมีสิทธิ์เข้าไม่ถึง

  • เข้าไปเมนู Runners เพื่อคัดลอก Token มา
  • กลับมาในโปรแกรม gitlab-runner เพื่อทำการ register และใส่ข้อมูลตาม

ควรสั่งคำสั่งด้วย sudo เพื่อให้ gitlab-runner เราทำงานใน user ของ gitlab-runner เอง

sudo gitlab-runner register
Enter fullscreen mode Exit fullscreen mode

3) เขียนไฟล์ .gitlab-ci.yml

# ขั้นตอนในการสั่งงาน
stages:
  - dev-build
  - dev-run

# ก่อนการทำงานในขั้นตอน dev-build และ dev-run
before_script:
  - echo hello world
  - $(pwd)

dev-build:
  # stage ในการทำงาน
  stage: dev-build
  # enviroment ในการทำงานซึ่งมีผลกับการใช้งานตัวแปรใน Settings > CI / CD > Variables
  environment:
    name: dev
  # tag หรือ branch ที่อยากให้ทำงานขั้นตอน dev-build
  only:
    - dev
  # ต้องใส่ให้ตรงและครบถ้วนกับตอนที่เรา register ด้วย token
  tags:
    - dev
  before_script:
    - echo this is before script
  after_script:
    - this is after script
  script:
    - ./myshell.sh $CI_PROJECT_DIR $GITLAB_USER_EMAIL $MY_FIRST_KEY
    - echo ending

dev-run:
  stage: dev-run
  environment:
    name: staging
  only:
    - dev
  tags:
    - dev
  before_script:
    - echo this is before script
  after_script:
    - this is after script
  script:
    - ./myshell.sh $CI_COMMIT_TAG $MY_FIRST_KEY $MY_PEM
    - cat $MY_PEM
    - echo ending
Enter fullscreen mode Exit fullscreen mode

4) หลังจากนั้นเราสามารถลอง push project เราขึ้นไปใน branch dev เพื่อทดลองการทำงานไฟล์ .gitlab-ci.yml ของเรา

ผลลัพท์จากการทำงานใน pipeline
1

ผลลัพท์จากขั้น dev-build
21

(highlight สีแดง)
variable ชื่อ $MY_FIRST_KEY แต่เป็นของ environment dev

ผลลัพท์จากขั้น dev-run
31

(highlight สีแดง)
variable ชื่อ $MY_FIRST_KEY แต่เป็นของ environment staging

(highlight สีเหลือง)
จะเห็นว่าหากใช้งาน variable แบบไฟล์ มันจะเก็บตัวแปรไว้ในไฟล์ และพอเรียกใช้สิ่งที่ได้คือ path ชั่วคราว (เมื่อ stage นี้ทำงานเสร็จแล้วมันก็จะหายไป)

41
จะเห็นว่า MY_FIRST_KEY มี 2 ค่า คนละ environment

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay