Setup CI/CU using gitlab

How to setup you CI/CD using gitlab.

Before we go and setup a webserver, we need content for that server.

The following variables need to be set in gitlab, outside of any files in the CI/CD settings part of gitlab:

  • SSH_PRIVATE_KEY
  • SSH_HOST_KEY
  • WWW_ROOT, the file system root of where the files are copied to;
  • HTTP_ROOT, the base url where to get the firmware from

WWW_ROOT should be something like “/var/www/esp-hosting” (on a Linux system) HTTP_ROOT would be something like “http://esp-firmware.server.org

image: python:3.10

variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.pip-cache"
  GIT_SUBMODULE_STRATEGY: recursive
  GIT_SUBMODULE_FORCE_HTTPS: "true"
  
cache:
  paths:
    - .pip-cache/

stages:
   - build
   - deploy-dev
 
build-throttle:
  tags:
    - platformio
  stage: build
  before_script:
    - pip install -U platformio
    - export GIT_DESC=$(git describe)
    - export arrV=(${GIT_DESC//-/ })
    - export VERSION=${arrV[0]}-${arrV[1]}
  script:
    - pio run -e esp32doit-devkit-v1
    - cp .pio/build/esp32doit-devkit-v1/firmware.bin esp32-hjb-throttle.bin
  artifacts:
    paths:
    - esp32-hjb-throttle.bin
    expire_in: 1 week

deploy-dev:
  tags:
    - platformio
  stage: deploy-dev
  dependencies:
    - build-throttle
  except:
    - master
    - release
  before_script:
    - apt-get update -y && apt-get install openssh-client bash rsync -y
    - eval $(ssh-agent -s)
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
    - eval "$(ssh-agent -s)"
    - ssh-add ~/.ssh/id_rsa
    - echo "${SSH_HOST_KEY}" > ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
    - export GIT_DESC=$(git describe)
    - export arrV=(${GIT_DESC//-/ })
    - export VERSION=${arrV[0]}-${arrV[1]}
  script: 
    - if [ "$VERSION" == "-" ]; then export VERSION="0"; fi
    - scp esp32-hjb-throttle.bin ${WWW_ROOT}/esp/firmware/esp32-hjb-throttle-${VERSION}.bin
    - echo $VERSION # Do something with this variable
    - scp ${WWW_ROOT}/esp/firmware/manifest .
    - echo "esp32-hjb-throttle, "$VERSION", $HTTP_ROOT"esp/firmware/esp32-hjb-throttle-"$VERSION".bin >> ./manifest
    - scp manifest ${WWW_ROOT}/esp/firmware

After a success full run of the CI/CD build, the manifest file is updated, an example of the manifest file is:

esp32-hjb-throttle, D0.1-16, http://esp-firmware.server.org/esp/firmware/esp32-hjb-throttle-D0.1-16.bin

The contents of the firmware directory could look like:

esp32-hjb-throttle-D0.1-16.bin
manifest