敏捷开发 持续集成与发布_如何自动化持续集成和开发的版本控制和发布

敏捷开发 持续集成与发布

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.

自动化流程还确保我们可以快速实施和测试新功能,并多次修复错误。 自动化的构建过程使我们可以就代码的外观和工作方式获得唯一的真理来源。

Image for post
Photo by Jess Bailey on Unsplash
杰西·贝利( Jess Bailey)Unsplash拍摄的照片

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:

有一些工具可帮助简化此过程,我将重点介绍如何使用以下工具:

Image for post
Ricardo Gomez Angel on Ricardo Gomez AngelUnsplash Unsplash拍摄

持续集成和开发,版本控制和发布(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流程,可以使我们自动化语义发行版本设置过程并设置所有默认配置。

Image for post
semantic-release-cli setup
语义释放CLI设置

With semantic-release-cli , we can do the following:

使用semantic-release-cli 我们可以执行以下操作:

Image for post
steps steps

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的更多信息。

Image for post
CircleCI Project setup
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
Image for post
CircleCI Project setup
CircleCI项目设置

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_TOKENNPM_TOKEN ,在本例中为CircleCI 。 阅读“创建个人访问令牌” ,了解如何在GitHub上创建访问令牌。 另外,您必须在NPM上拥有一个帐户才能创建访问令牌。

Image for post
CircleCI Project settings
CircleCI项目设置

To add environmental variables, click on the Add Variable button.

要添加环境变量,请单击“ Add Variable按钮。

Image for post
Adding Environmental variables in CircleCI
在CircleCI中添加环境变量

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 Xfix: 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.jsonscripts属性添加一个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 tests

    yarn test - 运行功能测试

  • yarn build runs the build process of compiling our code from Typescript to JavaScript

    yarn build - 运行将我们的代码从Typescript编译为JavaScript的构建过程

  • yarn semantic-release creates a version tag and publishes the code to NPM

    yarn semantic-release - 创建一个版本标签并将代码发布到NPM

If any of the steps fail, then the entire build process will fail and nothing is released to production.

如果任何步骤失败,则整个构建过程将失败,并且不会将任何内容发布到生产中。

Image for post
CircleCI Build Error
CircleCI构建错误

You can also drill down on the error to identify what caused the build to fail

您还可以深入研究错误,以识别导致构建失败的原因

Image for post

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
Image for post
CircleCI Workflow Success
CircleCI工作流程成功

This will trigger a new release in NPM:

这将触发NPM中的新版本:

Image for post
CircleCI
CircleCI

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)

翻译自: https://medium.com/swlh/how-to-automate-continuous-integration-and-development-versioning-and-publishing-a7d4b3311f3

敏捷开发 持续集成与发布

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值