Jika kamu merasa tulisan ini bermanfaat & membantu kamu, kamu bisa berdonasi lewat saweria

If you feel this website help you, you can donate at saweria


Sedikit Cerita

Request dari Stranger

Request dari Stranger

Ada user minta tolong untuk setup gitlab ci/cd di telegram, simple setup aja yang penting memenuhi 5 stage di atas.


Setup Unit Test Gitlab CI/CD Nodejs

Untuk project nya kita bakal pake punya codeathome-dev/unit-testing-blog dan untuk penjelasan detailnya cek di buildwithangga.com - test node dengan mocha & chai .

Jadi yang bakal saya setup urutannya adalah

  1. Setup stage -> stage ini hanya cek gitlab runner private kita jalan atau ga, sekalian kasih notif

  2. Unit-test stage -> unit test stage

  3. Build job stage -> build docker image

  4. Deploy job stage -> deploy di server

  5. Notification job stage -> notif pake telegram lagi

Catatan:

  1. Notif pake apprise ke group telegram

  2. Deploy nya sederhana banget, cuma run docker-compose up -d

Copy saja file .gitlab-ci.yml ke project kamu

stages:
   - setup
   - build-image
   - unit-test
   - deploy
   - notify
   #this stage list must be correct as we setup below
   

setup:
    # first stage
    # it will check if our private runner was running or not
    # and it will send notif to our telegram group
    stage: setup
    image: python:3.9-slim # or :3.9-alpine if you prefer a smaller image
    before_script:
      - pip install apprise # consider caching PIP_CACHE_DIR for performance
    script:
      apprise -vv --body="Update start!" \
      tgram://Bot:Token/-TelegramGroupID \
      #slack://token_a/token_b/token_c \

unit-test:
    # second stage
    stage: unit-test
    # change image to your prefered one 
    # if you need to test use nodejs 14 and node 16, just create another stage
    image: node:alpine
    needs: ["setup"]
    script:
    - npm install
    - npm run test

build-image:
   # third stage
   # if pass unit test, then build docker image 
   stage: build-image
   services:
      - docker:dind
   variables:
      IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
   script:
      - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
      - docker build -t $IMAGE_TAG .
      - docker push $IMAGE_TAG
   needs: ["unit-test"]

deploy:
    # 4th, simple deploy using docker-compose
    stage: deploy
    before_script:
        - apk update
        - apk add openssh-client
        - eval $(ssh-agent -s)
        - echo "$server1" | tr -d '\r' | ssh-add -
        - mkdir -p ~/.ssh
        - chmod 700 ~/.ssh
        - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

    script:
        - ssh userkamu@userkamu.com
          "cd /tmp/todo &&
           docker compose up -d"
    needs: ["unit-test"]

notify:
    # 5th, notify group with success notif
    stage: notify
    image: python:3.9-slim # or :3.9-alpine if you prefer a smaller image
    before_script:
      - pip install apprise # consider caching PIP_CACHE_DIR for performance
    script: |
      apprise -vv --body="Update finish!" \
      tgram://Bot:Token/-TelegramGroupID \
      #slack://token_a/token_b/token_c \      
    needs: ["deploy"]
    environment:
        name: production
        url: https://test.ipang.my.id
1 Stage Gagal Maka Tidak Akan Dilanjutkan

1 Stage Gagal Maka Tidak Akan Dilanjutkan

Unit Test Gagal, Maka Tidak Akan Dilanjutkan

Unit Test Gagal, Maka Tidak Akan Dilanjutkan

Referensi:

stackoverflow - How to send notification to Telegram from GitLab pipeline