敏捷开发 持续集成与发布
One of the most important tools in developing great software is the ability to automate. Imagine every time you need to release code to production, you have to repeat the same manual steps such as running tests, tagging/versioning your code changes and then manually provisioning or updating software or the configuration of your infrastructure, which can easily become the stuff of nightmares and create several horrific scenarios.
开发出色软件的最重要工具之一就是自动化能力。 想象一下,每次需要将代码发布到生产环境中时,都必须重复相同的手动步骤,例如运行测试,标记/版本化代码更改,然后手动配置或更新软件或基础结构的配置,这些很容易就变成了东西。噩梦,并创造出几种恐怖的场景。
Imagine the cost in developer time, resources and also the fact that manual testing can become very complicated as code becomes more complex which can increase the chances of bugs and errors slipping through into production. Automating the process will save time and improve code quality, as you only need to focus on the quality of your code and errors can be detected before they get into production. Running automated tests every time code is deployed, ensures that bugs are caught quickly and fixed promptly.
想象一下开发人员在时间,资源上的成本,以及随着代码变得更加复杂而导致手动测试变得非常复杂的事实,这会增加错误和错误进入生产环境的机会。 自动化过程将节省时间并提高代码质量,因为您只需要关注代码的质量,就可以在错误投入生产之前对其进行检测。 每次部署代码时都运行自动化测试,以确保快速发现并Swift修复错误。
An automated process also ensures that we can implement and test new features quickly and also fix bugs multiple times. An automated build process allows us to have a single source of truth in terms of how our code looks and how it works.
自动化流程还确保我们可以快速实施和测试新功能,并多次修复错误。 自动化的构建过程使我们可以就代码的外观和工作方式获得唯一的真理来源。
When we create and publish packages to NPM, we want to be able to automate how we track GIT changes and publishing of our package. This saves time from manually managing versions of the package.
当我们创建软件包并将其发布到NPM时,我们希望能够自动化跟踪GIT更改和发布软件包的方式。 这样可以节省手动管理软件包版本的时间。
There are a few tools that help simplify this process and I will be focusing on how I use the following tools:
有一些工具可帮助简化此过程,我将重点介绍如何使用以下工具:
Semantic release — to automate version management and package publishing to NPM.
持续集成和开发,版本控制和发布(Continuous Integration and Development, Versioning and Publishing)
Being able to automate how we track GIT changes, versioning and publishing of our package aids our Continuous Integration and Continuous Development process.
能够自动跟踪GIT变更的方式,软件包的版本控制和发布有助于我们的持续集成和持续开发过程。
Semantic Release provides us with the ability to automate version management and the publishing of our package to NPM.
语义发布使我们能够自动执行版本管理以及将软件包发布到NPM 。
Installing semantic-release-cli
as a dev dependency and running semantic-release-cli setup
to configure our CI/CD process allows us to automate the Semantic release setup process and sets up all default configurations.
通过将semantic-release-cli
安装为dev依赖项并运行semantic-release-cli setup
来配置我们的CI / CD流程,可以使我们自动化语义发行版本设置过程并设置所有默认配置。
With semantic-release-cli
, we can do the following:
使用semantic-release-cli
,我们可以执行以下操作:
Next, we can use an existing an GitHub repository or create a remote repository in GitHub and push our project to the remote repository. You can read more about getting started with GitHub and how to create a repository.
接下来,我们可以使用现有的GitHub存储库或在GitHub中创建远程存储库,并将我们的项目推送到远程存储库。 您可以阅读有关GitHub入门以及如何创建存储库的更多信息。
I usually opt to manually setup my CI by setting up a new project in the CircleCI Dashboard. You can read more about how to set up CircleCI.
我通常选择通过在CircleCI仪表板中设置新项目来手动设置CI。 您可以阅读有关如何设置CircleCI的更多信息。
Next, CircleCI requires a configuration file in the project. We have to create a .circleci
folder and config.yml
which will include our jobs and workflows:
接下来,CircleCI需要项目中的配置文件。 我们必须创建一个.circleci
文件夹和config.yml
,其中将包括我们的工作和工作流程:
// Create CircleCI configuration folder
$ mkdir .circleci// Create configuration file
$ touch .circleci/config.yml
Set up all authentication tokens and environmental variables for your project such as GH_TOKEN
/ GITHUB_TOKEN
and NPM_TOKEN
in the CI or in this case, CircleCI. Read Creating a personal access token on how to create an access token on GitHub. Also, you must have an account on NPM to create an access token.
为您的项目设置所有身份验证令牌和环境变量,例如CI中的GH_TOKEN
/ GITHUB_TOKEN
和NPM_TOKEN
,在本例中为CircleCI 。 阅读“创建个人访问令牌” ,了解如何在GitHub上创建访问令牌。 另外,您必须在NPM上拥有一个帐户才能创建访问令牌。
To add environmental variables, click on the Add Variable
button.
要添加环境变量,请单击“ Add Variable
按钮。
Install semantic-release
as a dev dependency, which automates the package release workflow including: determining the next version number, generating the release notes and publishing the package to NPM.
将semantic-release
安装为dev依赖项,这将自动执行软件包发行流程,包括:确定下一个版本号,生成发行说明并将软件包发布到NPM。
// Install Semantic release
$ yarn add -D semantic-release
Semantic Release expects commits to be in the conventional GIT commit format likefeat: add feature X
or fix: fix bug Y
that perform minor
and patch
version bumps respectively.
语义发布期望提交采用传统的GIT提交格式,例如feat: add feature X
或fix: fix bug Y
分别执行minor
版本和patch
版本更改的bugY。
Next, because Semantic Release will set the version automatically, we set the version
property to 0.0.0-development
and add a semantic-release
command to the scripts
property in package.json
接下来,由于语义发布将自动设置版本,因此我们将version
属性设置为0.0.0-development
,并向package.json
的scripts
属性添加一个semantic-release
命令package.json
// package.json"version": "0.0.0-development",
...
"scripts": {
...
"semantic-release": "semantic-release"
},
All the manual steps above can be set up automatically running semantic-release-cli setup
command.
上面的所有手动步骤都可以通过运行semantic-release-cli setup
命令来自动semantic-release-cli setup
。
Next, you update your CircleCI config.yml
file as follows:
接下来,按如下所示更新CircleCI config.yml
文件:
version: 2.1
orbs:
node: circleci/node@1.1.6
jobs:
test-build-and-publish:
executor:
name: node/default
steps:
- checkout
- node/with-cache:
steps:
- run: yarn install # Install dependencies
- run: yarn lint # Test Code Style
- run: yarn test # Run tests
- run: yarn build # Create production build
- run: yarn semantic-release # Release to npm
workflows:
test-build-and-publish:
jobs:
- test-build-and-publish
The CircleCI configuration is triggered on every push to our remote repository on GitHub. These steps are run in sequence, so this means that every time we trigger a deploy it runs the following steps as part of our workflow -
每次推送到GitHub上的远程存储库时,都会触发CircleCI配置。 这些步骤是按顺序运行的,因此这意味着,每次触发部署时,它都会在工作流程中运行以下步骤-
yarn install
— installs our dependencies;yarn install
—安装我们的依赖项;yarn lint
— lints and tests our code style;yarn lint
- 减少并测试我们的代码风格;yarn test
— runs feature testsyarn test
- 运行功能测试yarn build
— runs the build process of compiling our code from Typescript to JavaScriptyarn build
- 运行将我们的代码从Typescript编译为JavaScript的构建过程yarn semantic-release
— creates a version tag and publishes the code to NPMyarn semantic-release
- 创建一个版本标签并将代码发布到NPM
If any of the steps fail, then the entire build process will fail and nothing is released to production.
如果任何步骤失败,则整个构建过程将失败,并且不会将任何内容发布到生产中。
You can also drill down on the error to identify what caused the build to fail
您还可以深入研究错误,以识别导致构建失败的原因
To trigger a deployment, create a GIT commit and push to the master branch:
要触发部署,请创建一个GIT提交并推送到master分支:
$ git commit -m "fix: xxx"$ git push origin master
This will trigger a new release in NPM:
这将触发NPM中的新版本:
To use, import the latest version of the library into any of your projects.
要使用该库,请将最新版本的库导入到您的任何项目中。
结语 (Wrap Up)
Automating our code release process ensures that we ship quickly, save on costs and time, and improve our code quality and it should always be our core focus when architecting a new system / product.
自动化代码发布流程可确保我们快速发货,节省成本和时间并提高代码质量,并且在设计新系统/产品时,它始终应是我们的核心重点。
I hope this article gives you a good foundation to automate your workflow, manage versioning and publish your package to NPM using the tools outlined above.
我希望本文为您提供了一个良好的基础,可以使用上面概述的工具来自动化您的工作流程,管理版本控制并将软件包发布到NPM。
I have included a Github repository with the implementation described in this article. Feel free to clone, fork or star it if it helps you.
我在本文描述的实现中包括一个Github存储库。 如果有帮助,请随时克隆,复制或加注星标。
进一步阅读 (Further Reading)
敏捷开发 持续集成与发布