From ca10766118d9e41266c46973e4e805c272f45d27 Mon Sep 17 00:00:00 2001
From: Jan Loewe <jan.loewe@ptb.de>
Date: Thu, 29 Jun 2023 07:30:10 +0000
Subject: [PATCH] [CI] add release and publish pipeline

---
 .gitlab-ci.yml              |  54 ++++++++++++++++++++++++++++++++++++
 .yarnrc.yml                 |   7 +++--
 package.json                |   3 +-
 scripts/createReleaseEnv.sh |   9 ++++++
 yarn.lock                   | Bin 146319 -> 146353 bytes
 5 files changed, 69 insertions(+), 4 deletions(-)
 create mode 100755 scripts/createReleaseEnv.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 5a71494..ec412d5 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -3,6 +3,8 @@ image: node:18
 
 stages:
   - test
+  - release
+  - deploy
 
 
 # === Caches ===
@@ -78,3 +80,55 @@ lint:
     - yarn run test:prettier
   cache:
     - <<: *yarn-cache
+
+
+# === Job: Prepare Release
+# Description: this job prepares an env file for an upcoming release
+prepare_release:
+  stage: .pre
+  rules:
+    - if: $CI_COMMIT_TAG =~ /v.*/
+  script:
+    - ./scripts/createReleaseEnv.sh >> release.env
+  artifacts:
+    paths:
+      - release.description
+    reports:
+      dotenv: release.env
+
+
+# === Job: Release
+# Description: this job creates a new release using the provided env file
+# === Variables ===
+# NEXT_VER: the version of the upcoming release
+# === Files ===
+# release.description: multi-line description of the release
+release_job:
+  stage: release
+  image: registry.gitlab.com/gitlab-org/release-cli:latest
+  needs:
+    - job: prepare_release
+      artifacts: true
+  rules:
+    - if: $CI_COMMIT_TAG =~ /v.*/
+  script:
+    - echo "running release_job for $NEXT_VER"
+  release:
+    name: '$NEXT_VER'
+    description: ./release.description
+    tag_name: '$NEXT_VER'
+    ref: '$CI_COMMIT_SHA'
+
+
+# === Job: Deploy
+# Description: this job creates publishes the package to the gitlab package registry
+# === Variables ===
+# NPM_AUTH_TOKEN: a deploy token with read_package_registry and write_package_registry scopes
+deploy:
+  stage: deploy
+  needs: ["proxy"]
+  rules:
+    - if: $CI_COMMIT_TAG =~ /v.*/
+  script:
+    - *yarn-install
+    - yarn npm publish
diff --git a/.yarnrc.yml b/.yarnrc.yml
index be07e74..0c5ea34 100644
--- a/.yarnrc.yml
+++ b/.yarnrc.yml
@@ -1,7 +1,8 @@
 nodeLinker: node-modules
 
-npmPublishRegistry: "https://gitlab1.ptb.de/api/v4/projects/105/packages/npm/"
-
 npmScopes:
   d-ptb:
-    npmRegistryServer: "https://gitlab1.ptb.de/api/v4/packages/npm/"
+    npmPublishRegistry: "${CI_API_V4_URL:-https://gitlab1.ptb.de/api/v4}/projects/${CI_PROJECT_ID:-105}/packages/npm/"
+    npmRegistryServer: "${CI_API_V4_URL:-https://gitlab1.ptb.de/api/v4}/packages/npm/"
+    npmAlwaysAuth: true
+    npmAuthToken: "${NPM_AUTH_TOKEN:-}"
diff --git a/package.json b/package.json
index cf2231e..226e100 100644
--- a/package.json
+++ b/package.json
@@ -17,7 +17,7 @@
     "test": "concurrently -m 1  \"npm:test:*\"",
     "test:lint": "eslint src --ext .ts",
     "test:prettier": "prettier \"src/**/*.ts\" --list-different",
-    "test:unit": "vitest",
+    "test:unit": "vitest run",
     "prepack": "npx rimraf lib && yarn run build",
     "prepublish": "yarn run test"
   },
@@ -39,6 +39,7 @@
     "ts-node": "^10.9.1",
     "typescript": "^5.1.3",
     "unplugin-auto-import": "^0.16.4",
+    "vite": "^4.3.9",
     "vitest": "^0.32.2",
     "vitest-environment-custom": "link:./tests/vitest-environment-custom",
     "xpath-ts": "^1.3.13"
diff --git a/scripts/createReleaseEnv.sh b/scripts/createReleaseEnv.sh
new file mode 100755
index 0000000..a9705e3
--- /dev/null
+++ b/scripts/createReleaseEnv.sh
@@ -0,0 +1,9 @@
+#!/usr/bin/env bash
+
+mapfile -t TAGS < <(git tag --sort=creatordate --list "v*") # Read tags as array
+NEXT_VER=${TAGS[-1]}
+
+echo "NEXT_VER=${NEXT_VER}"
+echo "# dcc-js ${NEXT_VER}
+
+Support vx.y.z of the DCC" > release.description
diff --git a/yarn.lock b/yarn.lock
index 73ae83801f85034343d96fab25c7da0d9b2a337a..45dfc44bfa7cd1119f701207c1ee206de6119862 100644
GIT binary patch
delta 51
zcmeDG&av@3$A*&{999Z(CVIwtmdyt=wja=7EO;TPqfnMvlIoCGkZT2z+HN4o6t4vU
D|CSN9

delta 29
lcmdn^oumId$A*&{&9^kR-_l@Ae=*%{CZoXiBta&BEdbl244MD{

-- 
GitLab