Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2976d59b authored by theronakpatel's avatar theronakpatel
Browse files

ci: add GitLab pipeline to build and publish artifacts

parent 4a24a4d0
Loading
Loading
Loading
Loading
Loading
+111 −0
Original line number Diff line number Diff line
stages:
  - build
  - publish

default:
  interruptible: true
  before_script:
    - 'echo "Running on $(uname -a)"'

.rules_default: &rules_default
  rules:
    - if: '$CI_COMMIT_TAG'
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
    - when: manual
      allow_failure: true

.extract_version: &extract_version
  - |
    set -euo pipefail
    echo "Deriving package version from appinfo/info.xml ..."
    RAW_VERSION=$(xmllint --xpath 'string(/info/version)' appinfo/info.xml 2>/dev/null || true)
    if [ -z "${RAW_VERSION:-}" ]; then
      RAW_VERSION=$(grep -oP '<version>\K[^<]+' appinfo/info.xml | head -n1 || true)
    fi
    if [ -z "${RAW_VERSION:-}" ]; then
      echo "Could not extract version from appinfo/info.xml" >&2
      exit 1
    fi
    RAW_VERSION=${RAW_VERSION#v}
    if [ -n "${CI_COMMIT_TAG:-}" ]; then
      PACKAGE_VERSION="${CI_COMMIT_TAG#v}"
    else
      # Append short SHA to make version unique for non-tag builds
      PACKAGE_VERSION="${RAW_VERSION}-${CI_COMMIT_SHORT_SHA}"
    fi
    echo "PACKAGE_VERSION=${PACKAGE_VERSION}" | tee -a version.env

variables:
  NODE_ENV: production
  npm_config_ci: 'true'

cache:
  paths:
    - .npm/
    - vendor/cache/

build:vendor:
  stage: build
  image: composer:2
  needs: []
  <<: *rules_default
  before_script:
    - composer --version
    - php -v
  script:
    - *extract_version
    - composer install --no-dev --prefer-dist --no-progress --optimize-autoloader --classmap-authoritative
    - tar -czf vendor.tar.gz vendor
  artifacts:
    name: "vendor-$CI_COMMIT_REF_SLUG"
    paths:
      - vendor.tar.gz
      - version.env
    expire_in: 7 days

build:frontend:
  stage: build
  image: node:16-bullseye
  needs: []
  <<: *rules_default
  before_script:
    - node -v
    - npm -v
  script:
    - *extract_version
    - npm ci --cache .npm --prefer-offline
    - npm run build
    - tar -czf assets.tar.gz js css
  artifacts:
    name: "assets-$CI_COMMIT_REF_SLUG"
    paths:
      - assets.tar.gz
      - version.env
    expire_in: 7 days

publish:generic-registry:
  stage: publish
  image: curlimages/curl:8.10.1
  needs:
    - job: build:vendor
      artifacts: true
    - job: build:frontend
      artifacts: true
  <<: *rules_default
  script:
    - |
      set -euo pipefail
      source version.env
      echo "Publishing vendor and assets to Generic Package Registry (version: $PACKAGE_VERSION)"
      BASE_URL="$CI_API_V4_URL/projects/$CI_PROJECT_ID/packages/generic"
      VENDOR_PKG_NAME="ecloud-accounts-vendor"
      ASSETS_PKG_NAME="ecloud-accounts-assets"
      echo "Uploading vendor.tar.gz ..."
      curl --fail --show-error --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file vendor.tar.gz "$BASE_URL/$VENDOR_PKG_NAME/$PACKAGE_VERSION/vendor.tar.gz"
      echo "Uploading assets.tar.gz ..."
      curl --fail --show-error --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file assets.tar.gz "$BASE_URL/$ASSETS_PKG_NAME/$PACKAGE_VERSION/assets.tar.gz"
  artifacts:
    when: always
    reports:
      dotenv: version.env

stages:
  - test
  - build