Skip to content

GitLabCI:Yarn

yarn용 GitLabCI적용 방법.

node:18 이미지에 yarn은 이미 설치되어 있더라.

Yarn Deploy

GitLab CI에서 직접 배포할 경우 .yarnrc.yml파일에 다음과 같이 추가하면 된다.

nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-3.4.1.cjs

npmScopes:
  bogonets-steel:
    npmPublishRegistry: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/npm/"
    npmAlwaysAuth: true
    npmAuthToken: "${CI_JOB_TOKEN}"

그리고 다음 명령 중 하나로 배포하면 된다.

  • Yarn v1: yarn publish
  • Yarn v2+: yarn npm publish

Yarn v2+로 배포해야 yarn berry로 패키지를 다운받을 수 있다.

.yarnrc.yml 충돌 문제 해결

위의 파일을 직접 사용하면 평소에 환경변수 설정문제가 발생된다:

Usage Error: Environment variable not found (CI_API_V4_URL) in /home/user/Project/charging/.yarnrc.yml (in /home/user/Project/charging/.yarnrc.yml)

━━━ Yarn Package Manager - 3.4.1 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  $ yarn <command>

You can also print more details about any of these commands by calling them with
the `-h,--help` flag right after the command name.

YARN_RC_FILENAME환경변수를 사용하여 .yarnrc.yml파일명을 직접 변경할 수 있다.

example:

YARN_RC_FILENAME=.yarnrc-ci.yml yarn npm publish

Yarn Install

사설 저장소로 등록되어 있는 Gitlab npm package 를 다운받는 방법:

INFORMATION

배포시 반드시 Yarn v2+ (yarn npm publish) 로 배포해야 한다.

npm 패키지의 스코프가 존재한다면 .yarnrc.yml파일에 스코프의 저장소 정보를 추가한다:

nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-3.4.1.cjs

npmScopes:
  bogonets-steel:
    npmRegistryServer: "https://git.bogohitec.com/api/v4/packages/npm"

# 아래 코드는 실패한 케이스
#npmRegistries:
#  //git.bogohitec.com/api/v4/projects/123/packages/npm:
#    npmAlwaysAuth: true
#    npmAuthToken: 7Wo5YPC9zsCqrTsZ1tga

npmRegistries:
  //git.bogohitec.com/api/v4/packages/npm:
    npmAlwaysAuth: true
    npmAuthToken: 7Wo5YPC9zsCqrTsZ1tga

그리고 나서 yarn install하면 된다.

Example

.yarnrc-ci.yml:

nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-3.4.1.cjs

npmScopes:
  bogonets-steel:
    npmPublishRegistry: "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/npm/"
    npmAlwaysAuth: true
    npmAuthToken: "${CI_JOB_TOKEN}"

.gitlab-ci.yml:

image: node:18

cache:
  paths:
    - .yarn

stages:
  - test
  - deploy

lint_and_test:
  stage: test
  cache:
    paths:
      - .yarn
  before_script:
    - yarn install
  script:
    - yarn lint
    - yarn test

build_and_deploy:
  stage: deploy
  cache:
    paths:
      - .yarn
  rules:
    - if: $CI_COMMIT_REF_NAME == "release"
  before_script:
    - yarn install
  script:
    - yarn build
    - yarn decl
    - YARN_RC_FILENAME=.yarnrc-ci.yml yarn npm publish

NPM 배포 방법

GitLabCI:Npm#NPM Deploy 항목 참조.

See also

Favorite site