paint-brush
如何使用 GitHub Actions、Node.js、CouchDB 和 Aptible 自动部署博客文章应用程序经过@wise4rmgod
3,172 讀數
3,172 讀數

如何使用 GitHub Actions、Node.js、CouchDB 和 Aptible 自动部署博客文章应用程序

经过 Wisdom Nwokocha19m2023/12/04
Read on Terminal Reader

太長; 讀書

欢迎使用有关使用 GitHub Actions、Node.js、自动化博客文章应用程序部署的指南 CouchDB 和 Aptible。 这个综合教程将指导您使用上述工具和技术构建、部署和管理博客文章应用程序。 但首先,让我简要概述一下 Blogpost 应用程序、它的功能及其主要组件。 Blogpost 应用程序是一个 Web 应用程序,允许用户创建和共享博客文章。 用户可以撰写、编辑、删除和查看其他用户的帖子。该应用程序使用 Node.js 作为后端,CouchDB 作为数据库,GitHub Actions 作为持续集成和部署工具。
featured image - 如何使用 GitHub Actions、Node.js、CouchDB 和 Aptible 自动部署博客文章应用程序
Wisdom Nwokocha HackerNoon profile picture

欢迎阅读有关使用 GitHub Actions、Node.js、自动化博客文章应用程序部署的指南

CouchDB 和 Aptible。


这个综合教程将指导您使用上述工具和技术构建、部署和管理博客文章应用程序。


但首先,让我简要概述一下Blogpost应用程序、它的功能及其主要组件。 Blogpost应用程序是一个 Web 应用程序,允许用户创建和共享博客文章。


用户可以撰写、编辑、删除和查看其他用户的帖子。该应用程序使用 Node.js 作为后端,CouchDB 作为数据库,GitHub Actions 作为持续集成和部署工具。


我为什么选择这些?原因有很多,但以下是一些主要的原因:


  • Node.js是一个快速、可扩展且易于使用的 JavaScript 运行时环境,可以在各种平台上运行。它有许多用于 Web 开发的库和框架,例如 Express,我将在这个项目中使用的一个简约且灵活的 Web 应用程序框架。


  • CouchDB是一个可靠、安全且功能强大的开源 NoSQL 数据库系统。它是一个面向文档的数据库,使用JSON来存储数据。


  • GitHub Actions是一个灵活、方便的集成工具,可让您自动化 GitHub 存储库的工作流程。它还具有许多预构建的操作,您可以根据需要使用或自定义这些操作,例如 Aptible Deploy 操作,我将在本项目中使用它来将应用程序部署到 Aptible。


  • Aptible是一个基于云的平台,用于部署和管理容器化应用程序,提供轻松的配置、扩展和监控功能。

先决条件

在开始开发之旅之前,设置必要的工具和技术至关重要。


  • Node.js:确保您的计算机上安装了 Node.js。


  • CouchDB:验证 CouchDB 是否已安装并在您的系统上运行。


  • Node.js 和 JavaScript 专业知识:对 Node.js 和 JavaScript 基础知识有深入的了解。


  • Docker:在您的计算机上安装并运行 Docker。


  • Aptible 帐户:创建一个 Aptible 帐户,并熟悉部署基本应用程序。


  • Docker 云帐户:获取 Docker 云帐户来托管您的应用程序。

开发 Blogpost 应用程序

1:设置您的 Node.js 项目

  • 为您的博客文章应用程序创建一个项目目录。


  • 使用 npm 初始化 Node.js 项目:
 npm init -y


  • 安装 Express.js 框架,它将作为后端的基础:
 npm install express nano

第 2 步:设置 CouchDB

  • 确保 CouchDB 已安装并正在运行。您可以通过浏览器访问 CouchDB: http://127.0.0.1:5984/_utils/ / 。


CouchDB 仪表板

第 3 步:创建 Node.js 应用程序

  • 在项目目录中创建blog.js文件。


  • 初始化 Express,并将其连接到 CouchDB:
 const express = require("express"); const nano = require("nano")("http://admin:[email protected]:5984"); const app = express(); const port = 3000; // Middleware to parse JSON data app.use(express.json()); // async function asyncCall() { // // await nano.db.destroy("alice"); // await nano.db.create("blogposts"); // const db = nano.use("blogposts"); // return response; // } // asyncCall(); const db = nano.use("blogposts");


  • 此代码定义了用于使用 CouchDB 数据库创建、检索、更新和删除博客文章的 API 端点。


 // Create a new blog post app.post("/posts", async (req, res) => { const { title, description, author } = req.body; try { const doc = await db.insert({ title, description, author, createdAt: new Date(), updatedAt: new Date(), }); res.json({ id: doc.id, rev: doc.rev }); } catch (err) { console.error(err); res.status(500).send("Error creating post"); } }); // Get all blog posts app.get("/posts", async (req, res) => { try { const docs = await db.list({ include_docs: true }); res.json(docs.rows); } catch (err) { console.error(err); res.status(500).send("Error retrieving posts"); } }); // Get a specific blog post app.get("/posts/:id", async (req, res) => { const { id } = req.params; try { const doc = await db.get(id); res.json(doc); } catch (err) { console.error(err); res.status(404).send("Post not found"); } }); // Update a blog post app.put("/posts/:id", async (req, res) => { const { id } = req.params; const { title, description } = req.body; try { await db.insert({ _id: id, title, description, updatedAt: new Date(), }); res.json({ message: "Post updated successfully" }); } catch (err) { console.error(err); res.status(500).send("Error updating post"); } }); // Delete a blog post app.delete("/posts/:id", async (req, res) => { const { id } = req.params; try { await db.destroy(id); res.json({ message: "Post deleted successfully" }); } catch (err) { console.error(err); res.status(500).send("Error deleting post"); } }); app.listen(port, () => { console.log(`Blogpost app listening on port ${port}`); });


本地测试:

彻底的测试对于确保项目的功能和稳健性至关重要。以下是指导您的测试过程的示例 API 文档:

API文档

基本网址

假设您的服务器在端口 3000 本地运行,您的 API 的基本 URL 将为:

 http://localhost:3000

API端点:

创建新博客文章

  • 端点POST /posts

  • 描述:创建新博客文章。

  • 请求正文

     { "title": "String", "description": "String", "author": "String" }
  • 例子

     POST http://localhost:3000/posts { "title": "Sample Title", "description": "Sample Description", "author": "John Doe" }

获取所有博客文章

  • 端点GET /posts

  • 描述:检索所有博客文章。

  • 回复

     [ { "id": "String", "key": "String", "value": { "rev": "String" }, "doc": { "_id": "String", "_rev": "String", "title": "String", "description": "String", "author": "String", "createdAt": "Date", "updatedAt": "Date" } } ]
  • 例子

     GET http://localhost:3000/posts

获取特定的博客文章

  • 端点GET /posts/:id

  • 描述:通过 ID 检索特定博客文章。

  • 参数

    • id :博客文章的 ID。
  • 回复

     { "_id": "String", "_rev": "String", "title": "String", "description": "String", "author": "String", "createdAt": "Date", "updatedAt": "Date" }
  • 例子

     GET http://localhost:3000/posts/your-post-id

更新博客文章

  • 端点PUT /posts/:id

  • 描述:通过 ID 更新特定博客文章。

  • 参数

    • id :博客文章的 ID。
  • 请求正文

     { "title": "String", "description": "String" }
  • 例子

     PUT http://localhost:3000/posts/your-post-id { "title": "Updated Title", "description": "Updated Description" }

删除博客文章

  • 端点DELETE /posts/:id

  • 描述:按 ID 删除特定博客文章。

  • 参数

    • id :博客文章的 ID。
  • 例子

     DELETE http://localhost:3000/posts/your-post-id


测试GETPUTDELETE请求时,请将your-post-id替换为博客文章的实际 ID。

第 4 步:Docker 化您的 Node.js 应用程序

您需要有一个 Docker Hub 帐户。如果您尚未创建,请在 Docker Hub 注册。

确保您已在本地计算机上安装并运行 Docker。

将 Docker 化应用程序推送到 Docker Hub 的步骤:

  • 在 Node.js 项目的根目录中创建一个Dockerfile
 # Use an official Node.js runtime as the base image FROM node:16 # Set the working directory in the container WORKDIR /usr/src/app # Copy package.json and package-lock.json to the working directory COPY package*.json ./ # Install app dependencies RUN npm install # Copy the rest of the application files to the working directory COPY . . # Expose the port the app runs on EXPOSE 3000 # Define the command to run the app CMD ["node", "blog.js"]
  • 标记您的 Docker 映像:打开终端/命令提示符并导航到 Dockerfile 所在的 Node.js 应用程序的根目录。


运行以下命令来构建 Docker 映像,并使用您的 Docker Hub 用户名和所需的存储库名称对其进行标记:


 docker build -t your-docker-username/blogpost-app:latest .

your-docker-username替换为您的 Docker Hub 用户名,将blogpost-app替换为您所需的存储库名称。


您将得到类似的响应,如下所示:

 [+] Building 1.1s (10/10) FINISHED docker:desktop-linux => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 489B 0.0s => [internal] load metadata for docker.io/library/node:16 1.0s => [1/5] FROM docker.io/library/node:16@sha256:f77a1aef2da8d83e45ec990f45df50f1a286c5fe8bbfb8c6e4246c6389705c0b 0.0s => [internal] load build context 0.0s => => transferring context: 45.31kB 0.0s => CACHED [2/5] WORKDIR /usr/src/app 0.0s => CACHED [3/5] COPY package*.json ./ 0.0s => CACHED [4/5] RUN npm install 0.0s => CACHED [5/5] COPY . . 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:c5f046a9b99389aea6bf3f503e9b05cce953daf1b3f77ee5fb3f7469dc36c709 0.0s => => naming to docker.io/wise4rmgod/blogpost-app:latest


  • 登录 Docker Hub:通过执行以下命令,使用 Docker Hub 帐户对 Docker 客户端进行身份验证:
 docker login


出现提示时输入您的 Docker Hub 用户名和密码。

 Authenticating with existing credentials... Login Succeeded


  • 将 Docker 映像推送到 Docker Hub:登录后,使用以下命令将标记的 Docker 映像推送到 Docker Hub 存储库:
 docker push your-docker-username/blogpost-app:latest

此命令将本地镜像上传到指定存储库下的 Docker Hub。


  • 验证推送:转到您在网络上的 Docker Hub 帐户,然后导航到您的存储库以确认您的 Docker 映像已成功推送。

    Docker 云仪表板



 aptible deploy --app reactgame --docker-image wise4rmgod/blogpost-app --private-registry-username wise4rmgod --private-registry-password H$(_tS+W~ZBST63

第5步:适当部署

本教程假设您对在 Aptible 平台上设置环境应用程序端点数据库有基本的了解。本教程使用 CouchDB 作为数据库,并使用 Direct Docker 进行部署。


  • 使用以下命令通过 CLI 登录 Aptible:
 aptible login


系统将提示您输入电子邮件和密码。如果成功,您将收到类似于以下内容的响应:

 Token written to /Users/wisdomnwokocha/.aptible/tokens.json This token will expire after 6 days, 23 hrs (use --lifetime to customize)


  • 现在,使用以下命令部署您的应用程序:

句法:

 aptible deploy --app <app name> --docker-image <docker image in cloud>


这是一个示例命令:

 aptible deploy --app reactgame --docker-image wise4rmgod/blogpost-app


您将收到类似于以下内容的回复:

 INFO -- : Starting App deploy operation with ID: 61135861 INFO -- : Deploying without git repository INFO -- : Writing .aptible.env file... INFO -- : Fetching app image: wise4rmgod/blogpost-app... INFO -- : Pulling from wise4rmgod/blogpost-app INFO -- : 26ee4ff96582: Pulling fs layer INFO -- : 446eab4103f4: Pulling fs layer INFO -- : 2e3c22a0f840: Pulling fs layer INFO -- : a7ab8ad9b408: Pulling fs layer INFO -- : 3808fdf0c601: Pulling fs layer INFO -- : ab9e4075c671: Pulling fs layer INFO -- : 362360c8cef6: Pulling fs layer INFO -- : 928b5d11ac66: Pulling fs layer INFO -- : dc87e077ac61: Pulling fs layer INFO -- : f108e80f4efc: Pulling fs layer INFO -- : 84ac53840ac8: Pulling fs layer INFO -- : e81f21b79a1f: Pulling fs layer INFO -- : 2e3c22a0f840: Downloading: 523 KB / 49.8 MB INFO -- : 446eab4103f4: Downloading: 173 KB / 16.6 MB INFO -- : 26ee4ff96582: Downloading: 483 KB / 47 MB INFO -- : 2e3c22a0f840: Downloading: 25.7 MB / 49.8 MB INFO -- : a7ab8ad9b408: Downloading: 528 KB / 175 MB INFO -- : ab9e4075c671: Downloading: 355 KB / 33.4 MB INFO -- : a7ab8ad9b408: Downloading: 35.3 MB / 175 MB INFO -- : 26ee4ff96582: Pull complete INFO -- : 446eab4103f4: Pull complete INFO -- : 2e3c22a0f840: Pull complete INFO -- : a7ab8ad9b408: Downloading: 71.2 MB / 175 MB INFO -- : a7ab8ad9b408: Downloading: 106 MB / 175 MB INFO -- : a7ab8ad9b408: Downloading: 142 MB / 175 MB INFO -- : a7ab8ad9b408: Pull complete INFO -- : 3808fdf0c601: Pull complete INFO -- : ab9e4075c671: Pull complete INFO -- : 362360c8cef6: Pull complete INFO -- : 928b5d11ac66: Pull complete INFO -- : dc87e077ac61: Pull complete INFO -- : f108e80f4efc: Pull complete INFO -- : 84ac53840ac8: Pull complete INFO -- : e81f21b79a1f: Pull complete INFO -- : Digest: sha256:de9d04d069ca89ebdb37327365a815c88cd40d90cbc5395cc31c351fff1206dd INFO -- : Status: Downloaded newer image for wise4rmgod/blogpost-app:latest INFO -- : No Procfile found in git directory or /.aptible/Procfile found in Docker image: using Docker image CMD INFO -- : No .aptible.yml found in git directory or /.aptible/.aptible.yml found in Docker image: no before_release commands will run INFO -- : Pushing image dualstack-v2-registry-i-0a5ec8cff8e775b34.aptible.in:46022/app-63213/72184c41-7dc6-4313-b10e-749125f72577:latest to private Docker registry... INFO -- : The push refers to repository [dualstack-v2-registry-i-0a5ec8cff8e775b34.aptible.in:46022/app-63213/72184c41-7dc6-4313-b10e-749125f72577] INFO -- : dd387bc6b362: Pushed INFO -- : 586bd9d5efcf: Pushed INFO -- : 8ae0c889ca84: Pushed INFO -- : c91ec53bcc27: Pushing: 522 KB / 93.6 MB INFO -- : aec897bac4f0: Pushed INFO -- : 0ead224631d3: Pushed INFO -- : ad3b30eb29d3: Pushing: 542 KB / 444 MB INFO -- : 2a7587eb01b6: Pushing: 544 KB / 137 MB INFO -- : be36d2a441aa: Pushed INFO -- : 03f6e3800bbe: Pushed INFO -- : a10e482288d1: Pushing: 338 KB / 30.7 MB INFO -- : f9cfc9f6b603: Pushing: 513 KB / 103 MB INFO -- : c91ec53bcc27: Pushing: 31.3 MB / 93.6 MB INFO -- : c91ec53bcc27: Pushing: 62.7 MB / 93.6 MB INFO -- : ad3b30eb29d3: Pushing: 44.5 MB / 444 MB INFO -- : 2a7587eb01b6: Pushing: 34.4 MB / 137 MB INFO -- : a10e482288d1: Pushed INFO -- : ad3b30eb29d3: Pushing: 88.9 MB / 444 MB INFO -- : f9cfc9f6b603: Pushing: 34.6 MB / 103 MB INFO -- : 2a7587eb01b6: Pushing: 68.9 MB / 137 MB INFO -- : ad3b30eb29d3: Pushing: 133 MB / 444 MB INFO -- : f9cfc9f6b603: Pushing: 70.2 MB / 103 MB INFO -- : c91ec53bcc27: Pushed INFO -- : 2a7587eb01b6: Pushing: 103 MB / 137 MB INFO -- : ad3b30eb29d3: Pushing: 178 MB / 444 MB INFO -- : ad3b30eb29d3: Pushing: 224 MB / 444 MB INFO -- : 2a7587eb01b6: Pushed INFO -- : f9cfc9f6b603: Pushed INFO -- : ad3b30eb29d3: Pushing: 270 MB / 444 MB INFO -- : ad3b30eb29d3: Pushing: 312 MB / 444 MB INFO -- : ad3b30eb29d3: Pushing: 355 MB / 444 MB INFO -- : ad3b30eb29d3: Pushing: 401 MB / 444 MB INFO -- : ad3b30eb29d3: Pushed INFO -- : latest: digest: sha256:de9d04d069ca89ebdb37327365a815c88cd40d90cbc5395cc31c351fff1206dd size: 2841 INFO -- : Pulling from app-63213/72184c41-7dc6-4313-b10e-749125f72577 INFO -- : Digest: sha256:de9d04d069ca89ebdb37327365a815c88cd40d90cbc5395cc31c351fff1206dd INFO -- : Status: Image is up to date for dualstack-v2-registry-i-0a5ec8cff8e775b34.aptible.in:46022/app-63213/72184c41-7dc6-4313-b10e-749125f72577:latest INFO -- : Image app-63213/72184c41-7dc6-4313-b10e-749125f72577 successfully pushed to registry. INFO -- : STARTING: Register service cmd in API INFO -- : COMPLETED (after 0.28s): Register service cmd in API INFO -- : STARTING: Derive placement policy for service cmd INFO -- : COMPLETED (after 0.15s): Derive placement policy for service cmd INFO -- : STARTING: Create new release for service cmd INFO -- : COMPLETED (after 0.24s): Create new release for service cmd INFO -- : STARTING: Schedule service cmd .. INFO -- : COMPLETED (after 13.49s): Schedule service cmd INFO -- : STARTING: Stop old app containers for service cmd INFO -- : COMPLETED (after 0.0s): Stop old app containers for service cmd INFO -- : STARTING: Start app containers for service cmd INFO -- : WAITING FOR: Start app containers for service cmd INFO -- : WAITING FOR: Start app containers for service cmd INFO -- : COMPLETED (after 18.4s): Start app containers for service cmd INFO -- : STARTING: Delete old containers for service cmd in API INFO -- : COMPLETED (after 0.0s): Delete old containers for service cmd in API INFO -- : STARTING: Commit app containers in API for service cmd INFO -- : COMPLETED (after 0.26s): Commit app containers in API for service cmd INFO -- : STARTING: Commit service cmd in API INFO -- : COMPLETED (after 0.13s): Commit service cmd in API INFO -- : STARTING: Cache maintenance page INFO -- : COMPLETED (after 0.28s): Cache maintenance page INFO -- : STARTING: Commit app in API INFO -- : COMPLETED (after 0.19s): Commit app in API INFO -- : App deploy successful.


  • 访问 Aptible 仪表板以确认部署成功。

  • 单击仪表板中的端点选项卡,然后保存端点。这将允许您将数据库公开到公共互联网。

  • 单击下一个屏幕中的“添加端点”以创建新端点。

结论

这个综合教程将帮助您使用 Node.js、CouchDB 和 Aptible 构建、部署和管理博客文章应用程序。


您已经掌握了设置基本工具和技术、构建博客文章应用程序的后端、对应用程序进行 docker 化、将 Docker 映像推送到 Docker Hub 以及将应用程序部署到 Aptible 的基础知识。


祝贺您完成本综合教程并开启使用 Aptible 进行云原生应用程序开发的旅程!