Giter Club home page Giter Club logo

record's Introduction

record's People

Contributors

cavendev avatar keyding avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

record's Issues

Verdaccio 搭建私有 npm 仓库

私有 npm 仓库的好处

  • 公司内部开发的私有的包,仅想在内部使用,并且不想使用git + ssh的方式
  • npm 上下载包很慢,想要把安装过的包缓存起来,下一次安装的时候,先检查更新,如果没更新,从缓存下载
  • npm 安装包时,私有包从私有仓库下载,公共包从 npm 仓库下载

搭建方式

  • sinopia(不推荐,已经不维护)
  • Verdaccio(推荐,简单)
  • cnpm(推荐,复杂)

Verdaccio

Verdaccio 中文文档

一个轻量的私有 npm proxy registry,是 sinopia 开源框架的一个分支,另外 sinopia 现在已经没人维护了。

verdaccio 全局安装

npm install --global verdaccio
# or
yarn global add verdaccio

运行 verdaccio

verdaccio

# warn --- config file  - /xxx/.config/verdaccio/config.yaml
# warn --- Plugin successfully loaded: htpasswd
# warn --- Plugin successfully loaded: audit
# warn --- http address - http://localhost:4873/ - verdaccio/3.12.0

成功运行,浏览器打开 http://localhost:4873 可以正常看到页面。

通过 pm2 持久化运行

全局安装 pm2

npm install -g pm2
# or
yarn global add pm2

运行

pm2 start verdaccio

[PM2] Applying action restartProcessId on app [verdaccio](ids: [ 0 ])
[PM2] [verdaccio](0) ✓
[PM2] Process successfully started
┌───────────┬────┬─────────┬──────┬─────┬────────┬─────────┬────────┬─────┬───────────┬──────┬──────────┐
│ App name  │ id │ version │ mode │ pid │ status │ restart │ uptime │ cpu │ mem       │ user │ watching │
├───────────┼────┼─────────┼──────┼─────┼────────┼─────────┼────────┼─────┼───────────┼──────┼──────────┤
│ verdaccio │ 0  │ N/A     │ fork │ 513 │ online │ 0       │ 0s     │ 0%  │ 20.7 MB   │ root │ disabled │
└───────────┴────┴─────────┴──────┴─────┴────────┴─────────┴────────┴─────┴───────────┴──────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app

pm2 中文文档

默认配置项说明

/xxx/.config/verdaccio/config.yaml 默认的配置文件,4873 为默认端口,也可以修改默认配置。

Verdaccio 配置选项说明

#
# This is the default config file. It allows all users to do anything,
# so don't use it on production systems.
#
# Look here for more config file examples:
# https://github.com/verdaccio/verdaccio/tree/master/conf
#

# path to a directory with all packages
# 所有包的缓存目录
storage: ./storage

# path to a directory with plugins to include
# 插件目录
plugins: ./plugins

# WebUI 配置
web:
  # WebUI is enabled as default, if you want disable it, just uncomment this line
  # enable: false
  title: Verdaccio

# 验证信息
auth:
  htpasswd:
    # 存储用户信息
    file: ./htpasswd
    # Maximum amount of users allowed to register, defaults to "+inf".
    # You can set this to -1 to disable registration.
    # 可通过设置 max_users: -1 来禁用注册用户
    # max_users: 1000

# a list of other known repositories we can talk to
# 公共仓库配置
uplinks:
  npmjs:
    url: https://registry.npmjs.org/

packages:
  '@*/*':
    # scoped packages
    access: $all
    publish: $authenticated
    proxy: npmjs

  '**':
    # allow all users (including non-authenticated users) to read and
    # publish all packages
    # 允许所有用户(包括未验证身份的用户)读取和发布所有包
    #
    # you can specify usernames/groupnames (depending on your auth plugin)
    # and three keywords: "$all", "$anonymous", "$authenticated"
    # 可以通过用户分组或用户名来控制访问权限
    # $all 所有人 $anonymous 匿名用户 $认证(登录)用户
    access: $all

    # allow all known users to publish packages
    # (anyone can register by default, remember?)
    # 允许登录用户发布包
    publish: $authenticated

    # if package is not available locally, proxy requests to 'npmjs' registry
    # 代理,表示如果本地没有,就向此代理服务器发起请求
    proxy: npmjs

# You can specify HTTP/1.1 server keep alive timeout in seconds for incomming connections.
# A value of 0 makes the http server behave similarly to Node.js versions prior to 8.0.0, which did not have a keep-alive timeout.
# WORKAROUND: Through given configuration you can workaround following issue https://github.com/verdaccio/verdaccio/issues/301. Set to 0 in case 60 is not enought.
server:
  keepAliveTimeout: 60

# To use `npm audit` uncomment the following section
middlewares:
  audit:
    enabled: true
    
# 监听端口,不配置默认只能本机访问
listen: 0.0.0.0:4873

# log settings
logs:
  - {type: stdout, format: pretty, level: http}
  #- {type: file, path: verdaccio.log, level: info}

发布私有包

本地配置 npm

# 当前npm源指向私有仓库
npm set registry http://localhost:4873

或推荐使用 nrm 来管理源

全局安装 nrm

npm install -g nrm
# or
yarn global add nrm

nrm 查看源列表

nrm ls

#  npm ---- https://registry.npmjs.org/
#* cnpm --- http://r.cnpmjs.org/
#  taobao - https://registry.npm.taobao.org/
#  nj ----- https://registry.nodejitsu.com/
#  npmMirror  https://skimdb.npmjs.com/registry/
#  edunpm - http://registry.enpmjs.org/

* 表示的为当前使用的源

切换源

nrm use cnpm

# Registry has been set to: http://r.cnpmjs.org/

添加私有仓库源

# 添加
nrm add local_npm http://localhost:4873

# 切换
nrm use local_npm 

注册用户

# 先检查并切换至私有仓库源
nrm use local_npm

# 注册用户
npm adduser

# Username: test-user
# Password:
# Email: (this IS public) [email protected]
# Logged in as test-user on http://localhost:4873/.

按提示输入Username、Password 和 Email 即可注册完成。

登录用户

npm login

# Username: test-user
# Password:
# Email: (this IS public)  [email protected]
# Logged in as  test-user on http://localhost:4873/.

输入刚注册时的 Username、Password 和 Email。

查看用户登录是否成功

npm who am i

# test-user

发布包

npm publish

# npm notice
# npm notice 📦  [email protected]
# npm notice === Tarball Contents ===
# npm notice 809B    package.json
# ... ...
# npm notice === Tarball Details ===
# npm notice name:          test-npm-lab
# npm notice version:       0.0.1
# npm notice package size:  1.9 MB
# npm notice unpacked size: 7.9 MB
# npm notice shasum:        45848464cfa85c019f38f61a[xxx]2beb3b5475500
# npm notice integrity:     sha512-UCT4t9q07cM1w[...]EGgAyTrzgC3dA==
# npm notice total files:   3
# npm notice
# + [email protected]

发布成功,此时,可通过 webui 查看详细信息。

安装包

# 切换至私有仓库源
nrm use local_npm

# 安装包
npm install test-npm-lab

如果私有仓库中不包含你要安装的包,会从代理的npmjs去下载,并且会缓存在 ./storage 文件夹下,再次下载会先检查包是否有更新,无更新直接从缓存下载。

Git 解决SSH端口非常规22的问题

  1. 编辑 ssh config 文件
$ vi ~/.ssh/config
  1. 增加配置
Host           git.xxxx.com # git host
Port           12345 # ssh 端口号
User           keyding # 用户名称
IdentityFile    ~/.ssh/id_rsa # id_rsa 路径
  1. 保存,测试是否连接成功
$ ssh -T [email protected]
Welcome to GitLab, @keyding! # 连接成功

log.progressEnabled = log.gauge.isEnabled()

错误提示

/Users/xxxxxx/.config/yarn/global/node_modules/npm/node_modules/npmlog/log.js:57
log.progressEnabled = log.gauge.isEnabled()
                                ^

TypeError: log.gauge.isEnabled is not a function
    at Object.<anonymous> (/Users/xxxxxx/.config/yarn/global/node_modules/npm/node_modules/npmlog/log.js:57:33)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (/Users/xxxxxx/.config/yarn/global/node_modules/npm/lib/utils/umask.js:2:14)
    at Module._compile (internal/modules/cjs/loader.js:701:30)

如何解决

$ rm -rf /Users/xxxxxx/.config/yarn/global/node_modules/npm/
$ yarn global upgrade

Mac 使用 SSH 登录(免密码) Linux

创建私钥和公钥

打开终端,输入如下命令:

ssh-keygen -t rsa -C  '[email protected]'

-t 指定密钥类型,默认即 rsa ,可以省略
-C 设置注释文字,比如你的邮箱,可以省略

生成过程中需要输入密码两次,如不使用密码,可回车跳过。

密钥默认保存位置在 ~/.ssh 目录下,包括私钥文件 id_rsa 和公钥文件 id_rsa.pub

复制公钥文件 id_rsa.pub至服务器

使用 scp 命令将本地的公钥文件 id_rsa.pub 复制到需要连接的 Linux 服务器:

scp ~/.ssh/id_rsa.pub <用户名>@<ip地址>:/home/id_rsa.pub

如果修改了 ssh 服务器的默认端口号,需要指定端口号:

scp -P <端口号> ~/.ssh/id_rsa.pub <用户名>@<ip地址>:/home/id_rsa.pub

在服务器上,把公钥文件 id_rsa.pub追加到服务器 ssh 认证文件中:

cat /home/id_rsa.pub >> ~/.ssh/authorized_keys

如果服务器 .ssh 目录不存在,可提前创建

mkdir .ssh

到此,在本地就可以免密码登录服务器了

ssh <用户名>@<ip地址>

如果修改 ssh 默认端口号,指定端口号

ssh -p <端口号> <用户名>@<ip地址>

快捷登录

每次输入用户名、IP地址、端口号比较麻烦,可以配置 ssh 快捷登录 Linux 服务器

在本地 ~/.ssh/config 文件中添加服务器信息

config 文件不存在可自行创建

touch config

服务器信息格式如下

Host            alias            #自定义别名
HostName        hostname         #ssh服务器ip或domain
Port            port             #ssh服务器端口,默认为22
User            user             #ssh服务器用户名
IdentityFile    ~/.ssh/id_rsa    #第一个步骤生成的公钥文件对应的私钥文件

到此,就可以使用自定义别名来登录服务器了

ssh alias

Git 去除 fetch / pull 代码每次都需要输入密码

设置记住密码(默认15分钟)

git config --global credential.helper cache

设置一小时后失效

git config credential.helper 'cache --timeout=3600'

长期存储密码

git config --global credential.helper store

增加远程仓库带上用户名和密码

http://yourname:[email protected]/name/project.git

Gitlab CI / CD 持续集成

Gitlab CI / CD 持续集成

持续集成

概念

简单说就是指,频繁的(一天多次的)的将代码集成到主干。

与持续集成相关的两个概念:

持续交付

指的是,可频繁地将软件的新版本交付给质量团队,以供评审。评审通过,即可部署到生产环境。

持续交付强调的是,不管怎么更新,软件都可以随时交付。

持续部署

持续交付的下一步,代码经过评审通过后,自动部署到生产环境。

持续部署的目标是,代码在任何时候都可以部署到生产环境。

目的

  • 持续集成的目的,让产品快速迭代,同时还能保证高质量。

  • 持续集成可以保证项目 CommitMerge Request 时,进行自动测试自动构建自动部署等一系列自动化操作,减少开发人员重复的操作步骤,以便更专注于项目的业务逻辑开发。

持续集成流程

如图

代码提交 -> 测试(单元测试、集成测试、端到端测试) -> 构建 -> 部署

常用的持续集成平台

Gitlab CI / CD

简介

Gitlab 自带的一套持续集成系统,通过根目录下的 .gitlab-ci.yml 文件配置的规则,调用对应的 Gitlab Runner 来执行相应的构建任务。

.gitlab-ci.yml 使用 YAML 语言,YAML 是一个可读性高,以数据做为中心,用来表达数据序列的格式。 -- YAML wikipedia

.gitlab-ci.yml

位于项目根目录,用来配置(记录) Gitlab CI/CD 持续集成的一系列阶段,每个阶段执行不同的任务。

Gitlab Runner

任务(脚本)执行的承载者,.gitlab-ci.yml 的 script 部分就是由 Gitlab Runner 来负责执行的。

Gitlab CI 根据项目里根目录的 .gitlab-ci.yml 文件的规则,将每个阶段的每个任务中的 script 脚分配给对应的 Gitlab Runner 来执行,这些脚本包括安装依赖的,测试的,部署的等。

Gitlab CI/CD 持续集成过程

Commit 或 Pull Request -> Gitlab -> Gitlab CI/CD -> Gitlab Runner 1/2/3/...

安装 Gitlab Runner

本机安装

macOS / linux / windows 官网下载安装包安装即可

Gitlab Runner Docker Image 本地安装(MacOS)

1. 安装 Docker

docker官网下载安装包安装即可。

2. 拉取 Gitlab Runner Docker Image

$ docker pull gitlab/gitlab-runner:latest

3. 运行 Gitlab Runner Container

$ docker run -d --name gitlab-runner --restart always \
   -v /Users/Shared/gitlab-runner/config:/etc/gitlab-runner \
   -v /var/run/docker.sock:/var/run/docker.sock \
   gitlab/gitlab-runner:latest

4. 注册 Runner

进入 gitlab-runner 容器内部

  1. 查看容器id
$ docker ps
  1. 进入容器内部
$ docker exec -it containerID /bin/bash

注册 Runner

  1. 注册
$ gitlab-runner register

# Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
# https://gitlab.com/ 
  1. 输入 token

可在Gitlab UI项目中的设置 -> CI/CD选项中查看

# Please enter the gitlab-ci token for this runner:
# 
  1. 输入 Runner 描述
#Please enter the gitlab-ci description for this runner:
# [4c554f0df65c]: locally gitlab-ci runner
  1. 给Runner指派tags
  • 相当于Runner的唯一id
  • 可指派多个,表示此 Runner 仅执行 tags 内的任务。在 .gitlab-ci.yml 的每个任务中设置 tags 选项来指派对应 Runner 来执行任务
  • 也可为空,表示此 Runner 可执行任何任务
# Please enter the gitlab-ci tags for this runner (comma separated):
# test,develop,feature
  1. 选择 Runner 的执行载体
# Please enter the executor: docker+machine, docker-ssh+machine, docker, ssh, virtualbox, kubernetes, docker-ssh, parallels, shell:
# docker

在此选择docker,下一步设置默认镜像,用于 .gitlab-ci.yml 中未指定镜像的任务

# Please enter the default Docker image (e.g. ruby:2.1):
# node:alpine

# Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
  1. 查看 Runner 是否启动并注册成功

可在 Gitlab UI 中查看

$ gitlab-runner verify

# Verifying runner... is alive         

编辑 .gitlab-ci.yml 配置文件

CI/CD 关键词

Pipeline

即流水线,一次 pipeline 相当于一次构建任务。包含多个阶段和任务,如安装依赖、测试、部署等。

Stages

一次构建任务中的构建阶段。

一次 Pipeline 中可定义多个 Stages,每个 Stage 可以包含多个任务。

所有阶段会按定义顺序的执行,即当一个阶段的任务全部成功执行完毕后,下一个阶段才开始.

Jobs

构建任务,某个 Stage 里需要执行的任务,可以在 Stage 中定义多个 Jobs。

Jobs 特点:

  • 相同 Stage 中的 Jobs 并行执行
  • 相同 Stage 中的所有 Jobs 都执行成功时,该 Stage 才会标记成功
  • 任何一个 Job 执行失败,那么该 Stage 失败,该构建任务 Pipeline 失败

Pipeline、Stages、Jobs 关系

pipeline-stages-jobs

任何 commit 或 pull request 都会触发 pipeline

.gitlab-ci.yml 示例

一个vue cli 3默认生成的项目的部署脚本

# 指定 CI 默认使用的 docker 镜像
image: node:alpine

# 构建阶段
stages:
  - init
  - test
  - build
  - deploy

# init 阶段任务
initJob:
  stage: init
  # 缓存 node_modules 目录
  cache:
    # Gitlab CI 预定义变量
    key: ${CI_BUILD_REF_NAME}
    paths:
      - node_modules
  # 指定 Runner Tags
  tags: 
    - test
  script:
    # 设置淘宝镜像地址
    - npm set registry https://registry.npm.taobao.org 
    - npm install --progress=false

# test 阶段任务
testJob:
  stage: test
  # 使用缓存
  cache:
    key: ${CI_BUILD_REF_NAME}
    paths:
      - node_modules
    # 跳过上传cache
    policy: pull
  tags: 
    - test
  script:
    # 由于 cache 并不保证每次都命中(即拿到的cache可能为空),如果为空,重新生成
    - if [ ! -d "node_modules" ]; then
    - npm install --progress=false
    - fi
    - npm run test

# build 阶段任务
buildJob:
  stage: build
  cache:
    key: ${CI_BUILD_REF_NAME}
    paths:
      - node_modules
    # 跳过上传cache
    policy: pull
  tags: 
    - test
  script:
    - if [ ! -d "node_modules" ]; then
    - npm install --progress=false
    - fi
    - npm run build
  # 缓存打包后的文件
  artifacts:  
    expire_in: 1 week
    paths:
      - dist

  # deploy 阶段任务(仅主干提交时执行此任务)
  master_deploy:
    stage: deploy
    tags:
      - test
    # 部署脚本
    script:
      - bash delop.sh
    # 设置依赖哪个任务传递来的 artifacts
    dependencies: 
      - build
    # 执行执行此任务的分支
    only:
      - master
    # 执行方式
    when: manual

Gitlab CI yaml官方配置文件翻译

配置自定义变量

可以在 Gitlab UI 中配置一些任务执行中需要用到的数据,比如 SSH 私钥,服务器地址、端口等敏感数据

配置自定义变量

cache 与 artifacts

在GitLab-CI中, cache与artifacts比较容易混淆。

其中 cache 指的是缓存, 常用于依赖安装中, 如几个jobs都需要安装相同的依赖, 可以使用依赖,此时可以加快依赖的安装进度;

对于 artifacts 则是将某个工件上传到 GitLab 提供下载或后续操作使用。

由于每个 job 启动时,都会自动删除 .gitignore 中指定的文件, 因此对于依赖安装目录, 即可以使用cache, 也可以使用 artifacts。

两个主要有以下几个区别:

  • 虽然定义了 cache, 但是如果 cache 和 .gitignore 中重复的这部分, 仍然需要重新安装
    重新安装时因为使用的是缓存, 所以很有可能不是最新的。

  • 特别是开发环境, 如果每次都希望使用最新的更新, 应当删除cache, 使用 artifacts, 这样可以保证确定的更新

  • artifacts 中定义的部分, 会自动生成, 并可以传到下面的 job 中解压使用, 避免了重复依赖安装等工作

  • 如果使用 Docker 运行 Gitlab-Runner, cache 会生成一些临时容器, 不容易清理, artifacts 可以设置自动过期时间, 过期自动删除

  • artifacts 会先传到 GitLab 服务器, 然后需要时再重新下载, 所以这部分也可以在 GitLab 下载和浏览

linux命令查看文件或文件夹个数

命令

# 查看某个文件夹下文件的个数
ls -l | grep "^-" | wc -l

# 查看某个文件夹下文件的个数,包括子文件夹下的文件个数
ls -lR | grep "^-" | wc -l

# 查看某个文件夹下文件夹的个数
ls -l | grep "^d" | wc -l

# 查看某个文件夹下文件夹的个数,包括子文件夹下的文件夹个数
ls -lR | grep "^d" | wc -l

# 查看文件夹下所有的文件和文件夹。也就是统计ls -l命令所输出的行数
ls -l | wc -l

说明:

ls -l

长列表输出该目录下文件信息(注意这里的文件,不同于一般的文件,可能是目录、链接、设备文件等)

grep "^-"

这里将长列表输出信息过滤一部分,只保留一般文件,如果只保留目录就是 ^d

wc -l

 统计输出信息的行数,因为已经过滤得只剩一般文件了,所以统计结果就是一般文件信息的行数,又由于一行信息对应一个文件,所以也就是文件的个数。

JS 快速生成指定长度的数组

ES 6 结解构

[...Array(100)].map(_=> 0)

Int8Array & Unit8Array

new Int8Array(100) 
// or
new Unit8Array(100)

fill

Array(100).fill(0)

from

Array.from({length: 100}, _ => 0)

MacOS彻底删除vscode

  • 退出 vscode
  • 删除配置文件
$ rm -rf ~/Library/Application\ Support/Code
$ rm -rf ~/Library/Application\ Support/Code\ -\ Insiders/
  • 删除扩展插件
$ rm -rf ~/.vscode
$ rm -rf ~/.vscode-insiders/
  • 系统应用中删除 vscode

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.