<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
    <channel>
        <title>QPod Blog</title>
        <link>https://qpod.github.io/blog</link>
        <description>QPod Blog</description>
        <lastBuildDate>Fri, 13 Dec 2024 00:00:00 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <language>en</language>
        <item>
            <title><![CDATA[🀄️ 基于容器化的开发和DevOps的实践和经验]]></title>
            <link>https://qpod.github.io/blog/2024-container-practice</link>
            <guid>https://qpod.github.io/blog/2024-container-practice</guid>
            <pubDate>Fri, 13 Dec 2024 00:00:00 GMT</pubDate>
            <description><![CDATA[本文简述我们在进行开发和DevOps工作中使用容器化的实践、经验和理念。]]></description>
            <content:encoded><![CDATA[<p>本文简述我们在进行开发和DevOps工作中使用容器化的实践、经验和理念。</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="a-我们对容器使用的理念">A. 我们对容器使用的理念<a href="https://qpod.github.io/blog/2024-container-practice#a-%E6%88%91%E4%BB%AC%E5%AF%B9%E5%AE%B9%E5%99%A8%E4%BD%BF%E7%94%A8%E7%9A%84%E7%90%86%E5%BF%B5" class="hash-link" aria-label="Direct link to A. 我们对容器使用的理念" title="Direct link to A. 我们对容器使用的理念">​</a></h2>
<p>容器技术在软件开发和应用部署等领域广泛应用以来，在下面几方面相较于传统的开发和部署模式带来了新的改变。</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="a1-提供一致的环境">A1. 提供一致的环境<a href="https://qpod.github.io/blog/2024-container-practice#a1-%E6%8F%90%E4%BE%9B%E4%B8%80%E8%87%B4%E7%9A%84%E7%8E%AF%E5%A2%83" class="hash-link" aria-label="Direct link to A1. 提供一致的环境" title="Direct link to A1. 提供一致的环境">​</a></h3>
<p>无论开发者是在本地的开发环境，如Linux（ubuntu/centos）、Windows(WSL)、macOS（Intel/M系列芯片）等平台上，还是在构建环境（如CI/CD流水线）、还是运行环境（如生产环境），
都可以使用容器，确保使用相同的环境，消除各种因为环境、版本、配置等的差异，带来的协调困难，减少“在我机器上没问题”的情况。</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="a2-降低复杂环境管理和配置难度">A2. 降低复杂环境管理和配置难度<a href="https://qpod.github.io/blog/2024-container-practice#a2-%E9%99%8D%E4%BD%8E%E5%A4%8D%E6%9D%82%E7%8E%AF%E5%A2%83%E7%AE%A1%E7%90%86%E5%92%8C%E9%85%8D%E7%BD%AE%E9%9A%BE%E5%BA%A6" class="hash-link" aria-label="Direct link to A2. 降低复杂环境管理和配置难度" title="Direct link to A2. 降低复杂环境管理和配置难度">​</a></h3>
<p>通过容器镜像的统一管理、团队在各种环境均使用相同的镜像，可以大幅减少每位开发者花费大量时间，在自己的环境中安装软件、进行配置的时间。</p>
<p>一些常见的痛点包括：</p>
<ul>
<li>对AI和算法类工程师：在不同的环境中安装配置NVIDIA cuda系列套件，并维护cuda版本的更新，十分花费时间，尤其是在企业局域网内网环境；</li>
<li>对前端开发者：需要处理NodeJS，以及npm/yarn/pnpm自身版本的管理和更新问题，每次版本的更新均需要花费一定的时间来更新配置；</li>
<li>对后端开发者：同样对于Golang/Java/Python/Rust/C++等环境的程序包安装、配置、更新，需要花费较大精力进行更新、维护和管理；</li>
<li>对数据科学家：在使用Python/R等数据科学工具包时，跨平台版工具包版本的管十分头疼，例如：Windows/Linux/macOS上不同版本的pytorch等包，有一些包需要进行C++编译才能安装，还有一些包通过rJava/py4j调用Java程序时候又涉及JRE/JDK的管理。</li>
</ul>
<p>而如果通过维护一套统一的容器镜像栈，就可以消除这些困难。当然，对这些容器镜像的管理，也需要通过“代码定义的配置（Code defined configuration）”来确保这些镜像的可维护性。具体来说：</p>
<ul>
<li>需要使用Dockerfile而不是通过docker commit来管理镜像，保证镜像构建的可重复性和可维护性；</li>
<li>建议把Dockerfile中进行的操作抽象提取到shell脚本中，用shell脚本函数来定义大部分操作，而在Dockerfile中只执行函数的调用，这样的脚本更易于维护、易读（相较于Dockerfile）、并且可以在其他平台环境执行。</li>
</ul>
<p>经过多年的迭代，我们不断打磨我们的实践，形成了<a href="https://hub.docker.com/r/qpod" target="_blank" rel="noopener noreferrer">QPod Stack</a>系列镜像，其源代码也完全开源：<a href="https://github.com/QPod/lab-foundation" target="_blank" rel="noopener noreferrer">https://github.com/QPod/lab-foundation</a> 。这一些列的镜像是我们的理念的实践，并且使用GitHub Actions（这一免费的资源）进行构建和管理。</p>
<p>当然您也完全可以构建自己的镜像栈，维护自己的软件供应链体系。</p>
<p>这一实践带来的好处显而易见：</p>
<ol>
<li>当开发者需要某一个系列的开发环境时（例如Golang，或者包含了pytorch的环境），那么只要选取一个镜像，docker pull到自己的环境就可以进行开发，只需要把源代码通过docker volume mount到启动的容器中即可。</li>
<li>当开发者需要更新环境中的软件或package（例如NVIDIA cuda）版本时，只需要拉取最新版本（latest）或指定版本的镜像即可，无需从主机上下载、安装、配置环境。</li>
<li>将镜像、环境的管理交由专人（或者可信任的开源社区）管理、维护、开发，一方面可以确保代码定义的环境的可追溯管理和软件供应链安全，另一方面这在在该领域有专长的开发者更能够进行合理的安装和配置（例如：当你在你的笔记本上运行pytorch程序时，如果配置不当则会安装GPU版本的pytorch，无端占用更多存储空间）。</li>
</ol>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="a3-简化部署和运维">A3. 简化部署和运维<a href="https://qpod.github.io/blog/2024-container-practice#a3-%E7%AE%80%E5%8C%96%E9%83%A8%E7%BD%B2%E5%92%8C%E8%BF%90%E7%BB%B4" class="hash-link" aria-label="Direct link to A3. 简化部署和运维" title="Direct link to A3. 简化部署和运维">​</a></h3>
<p>对开发人员来说，在开发测试环境的环境搭建工作中，希望尽可能简单的操作，例如：</p>
<ul>
<li>快速搭建启动一个数据库；</li>
<li>快速启动一个中间件（例如Nginx）而且只需要关心其中最核心的配置部分（例如Nginx的server和location部分）。</li>
</ul>
<p>对运维人员来说：在生产环境的运维工作，希望有尽可能简单的操作，例如通过docker-compose文件或helm chart等，来实现：</p>
<ul>
<li>拉取最新版本的artifact（构建，例如docker images）；</li>
<li>不必关心各种软件包的安装、更新等管理、不必关心环境变量等的配置；</li>
<li>仅仅通过一两个简单的命令操作实现服务的停止、启动、重启；</li>
<li>将生产环境密码、secrets、certificates等独立管理，不让未授权的人员访问（如开发人员）。</li>
</ul>
<p>这些需求和痛点都可以通过编写不同环境的docker-compose文件或helm chart来实现。</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="a5-其他的好处">A5. 其他的好处<a href="https://qpod.github.io/blog/2024-container-practice#a5-%E5%85%B6%E4%BB%96%E7%9A%84%E5%A5%BD%E5%A4%84" class="hash-link" aria-label="Direct link to A5. 其他的好处" title="Direct link to A5. 其他的好处">​</a></h3>
<ul>
<li>资源利用率提升：与传统虚拟机相比，容器更轻量级，能够在单一主机上运行更多实例，有效利用计算资源。</li>
<li>应用隔离与安全：容器为每个应用提供隔离环境，降低了应用之间的相互影响风险，同时通过镜像控制与权限管理增强了安全性。</li>
</ul>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="b-一些具体的实践细节">B. 一些具体的实践细节<a href="https://qpod.github.io/blog/2024-container-practice#b-%E4%B8%80%E4%BA%9B%E5%85%B7%E4%BD%93%E7%9A%84%E5%AE%9E%E8%B7%B5%E7%BB%86%E8%8A%82" class="hash-link" aria-label="Direct link to B. 一些具体的实践细节" title="Direct link to B. 一些具体的实践细节">​</a></h2>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="b1-统一镜像栈的代码管理和cicd">B1. 统一镜像栈的代码管理和CI/CD<a href="https://qpod.github.io/blog/2024-container-practice#b1-%E7%BB%9F%E4%B8%80%E9%95%9C%E5%83%8F%E6%A0%88%E7%9A%84%E4%BB%A3%E7%A0%81%E7%AE%A1%E7%90%86%E5%92%8Ccicd" class="hash-link" aria-label="Direct link to B1. 统一�镜像栈的代码管理和CI/CD" title="Direct link to B1. 统一镜像栈的代码管理和CI/CD">​</a></h3>
<p>我们的源代码完全公开在GitHub上：<a href="https://github.com/QPod/lab-foundation" target="_blank" rel="noopener noreferrer">https://github.com/QPod/lab-foundation</a></p>
<p>镜像的构建完全通过（免费的）GitHub Actions来构建：<a href="https://github.com/QPod/lab-foundation/blob/main/.github/workflows/build-docker.yml" target="_blank" rel="noopener noreferrer">https://github.com/QPod/lab-foundation/blob/main/.github/workflows/build-docker.yml</a></p>
<p>我们也针对几个不同的开发领域构建几个代码库来分别管理各个系列的镜像栈：</p>
<ul>
<li><a href="https://github.com/QPod/lab-foundation" target="_blank" rel="noopener noreferrer">lab-foundation</a>：维护了一些列基础镜像，例如各前后端编程语言环境，以及NVIDIA cuda和torch等基础环境；</li>
<li><a href="https://github.com/QPod/lab-data" target="_blank" rel="noopener noreferrer">lab-data</a>：维护了一些列常用数据库（Postgresql及附加插件）、大数据处理组件（spark/flink）等；</li>
<li><a href="https://github.com/QPod/lab-dev" target="_blank" rel="noopener noreferrer">lab-dev</a>：维护了一些列开发和线上应用中常用的组件，例如：nginx、网页内IDE（如VSCode和Jupyter）等；</li>
<li><a href="https://github.com/QPod/lab-media" target="_blank" rel="noopener noreferrer">lab-media</a>：维护了一些列多媒体/多模态内容处理的组件，如OpenCV、PaddleOCR、HuggingFace Model等。</li>
</ul>
<p>现有代码中，对各种软件包的安装，都会默认选取最新的release/stable版本，因此当有软件更新后，只需重新执行一下GitHub Actions，版本最新版本软件包的镜像就会被构建并推送到镜像库当中。</p>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="b2-不同运行环境下的差异化处理">B2. 不同运行环境下的差异化处理<a href="https://qpod.github.io/blog/2024-container-practice#b2-%E4%B8%8D%E5%90%8C%E8%BF%90%EF%BF%BD%EF%BF%BD%E8%A1%8C%E7%8E%AF%E5%A2%83%E4%B8%8B%E7%9A%84%E5%B7%AE%E5%BC%82%E5%8C%96%E5%A4%84%E7%90%86" class="hash-link" aria-label="Direct link to B2. 不同运行环境下的差异化处理" title="Direct link to B2. 不同运行环境下的差异化处理">​</a></h3>
<p>镜像会在不同的环境下使用，例如：不同的时区、不同的数据中心（例如北美、中国）、不同云服务提供商（例如Azure、AWS、aliyun）。
为了更好的在不同环境下使用这些镜像，仍需要在容器启动后，执行特定的个性化配置。</p>
<p>为此，我们定义了一系列脚本，用于处理在不同环境时需要执行的个性化配置操作。
例如，您的镜像运行在中国大陆地区的互联网上（或者你的个人电脑连接了中国大陆的互联网），<a href="https://github.com/QPod/lab-foundation/blob/main/docker_atom/work/localize/run-config-mirror-aliyun-pub.sh" target="_blank" rel="noopener noreferrer">这个脚本</a>执行的个性化的配置将会：</p>
<ul>
<li>在镜像内进行恰当的时区配置；</li>
<li>检测镜像内操作系统环境（Ubuntu/Debian），并把其镜像源修改为aliyun面向互联网的镜像；</li>
<li>检测是否有Python安装，如果有则：将Python的pip下载安装源配置到：<a href="http://mirrors.aliyun.com/pypi/simple/" target="_blank" rel="noopener noreferrer">http://mirrors.aliyun.com/pypi/simple/</a></li>
<li>检测是否有npm/yarn/pnpm的存在，如果有则：配置镜像源为：registry <a href="https://registry.npmmirror.com/" target="_blank" rel="noopener noreferrer">https://registry.npmmirror.com</a></li>
<li>检测是否有Golang开发环境存在，如果有则：配置GOPROXY=<a href="https://mirrors.aliyun.com/goproxy/" target="_blank" rel="noopener noreferrer">https://mirrors.aliyun.com/goproxy/</a></li>
<li>检测是否有R语言运行环境存在，如果有则：配置CRAN源为：<a href="https://mirrors.aliyun.com/CRAN/" target="_blank" rel="noopener noreferrer">https://mirrors.aliyun.com/CRAN/</a></li>
</ul>
<h3 class="anchor anchorWithStickyNavbar_LWe7" id="b3-在开发环境进行代码开发时的一些实践">B3. 在开发环境进行代码开发时的一些实践<a href="https://qpod.github.io/blog/2024-container-practice#b3-%E5%9C%A8%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83%E8%BF%9B%E8%A1%8C%E4%BB%A3%E7%A0%81%E5%BC%80%E5%8F%91%E6%97%B6%E7%9A%84%E4%B8%80%E4%BA%9B%E5%AE%9E%E8%B7%B5" class="hash-link" aria-label="Direct link to B3. 在开发环境进行代码开发时的一些实践" title="Direct link to B3. 在开发环境进行代码开发时的一些实践">​</a></h3>
<p>如A2小节中提及的，开发者在自己的Windows(WSL)、macOS、Linux环境像，使用这些基础环境进行开发时候，可以：</p>
<ul>
<li>
<p>通过<code>docker run</code>来启动一个镜像，启动时用<code>-v</code>来把源代码的目录挂载到容器内，然后<code>docker exec -it</code>进入容器中，在容器内启动程序（例如：<code>python main.py</code>、<code>npm run dev</code>、<code>go run</code>等）。</p>
</li>
<li>
<p>编写一个docker-compose文件来实现上述逻辑，其中容器启动的command设置为：<code>tail -f /dev/null</code>，这样容器就会保持启动而不会运行即逝，然后再通过<code>docker exec -it</code>进入容器启动代码。</p>
</li>
<li>
<p>如果您希望在网页IDE（包括VSCode、JupyterLab、RStudio）中进行开发，还可以使用我们维护的这个容器来进行开发：<a href="https://github.com/QPod/lab-dev/tree/main/docker_devbox" target="_blank" rel="noopener noreferrer">https://github.com/QPod/lab-dev/tree/main/docker_devbox</a></p>
</li>
</ul>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="c-总结">C. 总结<a href="https://qpod.github.io/blog/2024-container-practice#c-%E6%80%BB%E7%BB%93" class="hash-link" aria-label="Direct link to C. 总结" title="Direct link to C. 总结">​</a></h2>
<p>经过多年的开发、DevOps、运维实践，我们通过实践积累和打磨了上面的开源项目，形成了自己的实践来提升开发运维效率。</p>
<p>希望这些构建好的镜像、用于构建的源代码、我们的开发实践能对您和您的团队的开发有帮助，如果您有更多的需求想要交流、或者想要对这一项目进行贡献，也欢迎您到对应的GitHub repo当中提PR/issue。</p>]]></content:encoded>
            <category>QPod</category>
            <category>docs</category>
        </item>
        <item>
            <title><![CDATA[📝 QPod CI/CD Toolkits and Practices]]></title>
            <link>https://qpod.github.io/blog/2024-qpod-stack-ci-cd-practices-and-philosophy-en</link>
            <guid>https://qpod.github.io/blog/2024-qpod-stack-ci-cd-practices-and-philosophy-en</guid>
            <pubDate>Thu, 28 Mar 2024 00:00:00 GMT</pubDate>
            <description><![CDATA[In this brief blog, we will introduct the CI/CD philosophy, and our practices.]]></description>
            <content:encoded><![CDATA[<p>In this brief blog, we will introduct the CI/CD philosophy, and our practices.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="our-cicd-philosophy">Our CI/CD Philosophy<a href="https://qpod.github.io/blog/2024-qpod-stack-ci-cd-practices-and-philosophy-en#our-cicd-philosophy" class="hash-link" aria-label="Direct link to Our CI/CD Philosophy" title="Direct link to Our CI/CD Philosophy">​</a></h2>
<p>Back to years ago, our team has tried CI/CD tools including but not limited to travis, GitLab runners, etc. Until recently, we adopted GitHub actions as our current choice.</p>
<p>As these tools evolves (or your choice changes), the code CI/CD pipelines, in the form of YAML files or manual configure pipelines, has to be refactored to fit new tools.</p>
<p>Our philosophy is to keep the CI/CD as simple as possible, and de-couple with the CI/CD tools.
As such, it's a nature choice to <strong>put more function implementations in scripts/modules in source code, instead of relying on tools provided functionalities</strong> (e.g.: Github Actions).</p>
<p>Based on this philosophy, we established our CI/CD practice along with correspond toolkits.</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="the-toolkits-and-the-practices">The toolkits and the practices<a href="https://qpod.github.io/blog/2024-qpod-stack-ci-cd-practices-and-philosophy-en#the-toolkits-and-the-practices" class="hash-link" aria-label="Direct link to The toolkits and the practices" title="Direct link to The toolkits and the practices">​</a></h2>
<p>Here is an example of our toolkits for CI/CD, which include the following parts:</p>
<ul>
<li>
<p><a href="https://github.com/QPod/lab-foundation/blob/main/tool.sh" target="_blank" rel="noopener noreferrer">tool.sh</a>: a shell script which include several functions to build code or project.</p>
</li>
<li>
<p><a href="https://github.com/QPod/lab-foundation/blob/main/.github/workflows/build-docker.yml" target="_blank" rel="noopener noreferrer">github workflow YAML</a>: a CI/CD platform specific configuration which use the <code>tool.sh</code> above, and use simple linux shell commands to finish most build tasks. As you can see, each job starts with <code>source ./tool.sh</code> to use the functions defined in our toolkits.</p>
</li>
</ul>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="other-choices">Other Choices<a href="https://qpod.github.io/blog/2024-qpod-stack-ci-cd-practices-and-philosophy-en#other-choices" class="hash-link" aria-label="Direct link to Other Choices" title="Direct link to Other Choices">​</a></h2>
<p>As built artifacts changes in developing / testing (stagging) / release (production) stages, we found it's important to manage the artificats in two perspective:</p>
<ol>
<li>
<p>Each artifact should be properly versioned and tagged, especially when a testing (stagging) or production environment needs a rollback.</p>
</li>
<li>
<p>The release artifact repo (such as Docker registry) should be a seperated one with the developing / testing (stagging) one(s), in order to better manage the artifiacts for production environment, and use proper resources. In many cases, the production environment requires an aritifact repo that is high-available, and better to be in the same VPC/Zone/Region with the computing resources, or with different push/pull permissions.</p>
</li>
</ol>
<p>As such, it became our choice to put artifacts of developing/testing stage and release/production artifiacts into different repo, at least differnt registry namespaces. In the <code>tool.sh</code> file above, you can see we use a variable <code>DOCKER_IMG_NAMESPACE</code> to decide which name space the artifact should be pushed into. Furthermore, this variable is decided by the <strong>Git branch name prefix</strong>:</p>
<div class="language-shell codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-shell codeBlock_bY9V thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><span class="token plain">if [ "${CI_PROJECT_BRANCH}" = "main" ] ; then</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">    # If on the main branch, docker images namespace will be same as CI_PROJECT_NAME's name space</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">    export CI_PROJECT_NAMESPACE="$(dirname ${CI_PROJECT_NAME})" ;</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">else</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">    # not main branch, docker namespace = {CI_PROJECT_NAME's name space} + "0" + {1st substr before / in CI_PROJECT_SPACE}</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">    export CI_PROJECT_NAMESPACE="$(dirname ${CI_PROJECT_NAME})0${CI_PROJECT_SPACE}" ;</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">fi</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_y97N"><path fill="currentColor" d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg viewBox="0 0 24 24" class="copyButtonSuccessIcon_LjdS"><path fill="currentColor" d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div>
<p>In this way:</p>
<ul>
<li>when code is pushed or merged to <code>main</code> branch, artifiact repo namespace will be <code>qpod</code> in our example.</li>
<li>when code is pushed to a branch name in the pattern of <code>dev/*</code> and a PR is created to merge this branch <code>dev/*</code> into <code>main</code>, the artifact will be pushed to the namespace <code>qpod0dev</code> (the suffix <code>0dev</code> can be customized).</li>
<li>when code is pushed to a branch without a PR into <code>main</code>, the code will not be built (pipeline will not be triggerd).</li>
</ul>]]></content:encoded>
            <category>QPod</category>
            <category>docs</category>
        </item>
        <item>
            <title><![CDATA[🀄️ QPod Stack的CI/CD的思路、实践、理念]]></title>
            <link>https://qpod.github.io/blog/2024-qpod-stack-ci-cd-practices-and-philosophy-cn</link>
            <guid>https://qpod.github.io/blog/2024-qpod-stack-ci-cd-practices-and-philosophy-cn</guid>
            <pubDate>Thu, 28 Mar 2024 00:00:00 GMT</pubDate>
            <description><![CDATA[本文简单介绍了我们进行CI/CD的思路、实践、理念。]]></description>
            <content:encoded><![CDATA[<p>本文简单介绍了我们进行CI/CD的思路、实践、理念。</p>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="我们的-cicd-理念">我们的 CI/CD 理念<a href="https://qpod.github.io/blog/2024-qpod-stack-ci-cd-practices-and-philosophy-cn#%E6%88%91%E4%BB%AC%E7%9A%84-cicd-%E7%90%86%E5%BF%B5" class="hash-link" aria-label="Direct link to 我们的 CI/CD 理念" title="Direct link to 我们的 CI/CD 理念">​</a></h2>
<p>过去几年中，我们的团队尝试了包括但不限于 Travis、GitLab 运行器等 CI/CD 工具。直到最近，我们选择了 GitHub Actions 作为当前的选择。</p>
<p>随着这些工具的发展（或团队选择的变化），代码的 CI/CD 流水线，无论是以 YAML 文件的形式还是手动配置的流水线，都需要重构以适应新工具。</p>
<p>我们的理念是尽可能保持 CI/CD 的简单性，并与 CI/CD 工具解耦。</p>
<p>因此，很自然的选择是 <strong>将更多的功能实现放在源代码中的脚本/模块里，而不是依赖于工具提供的功能</strong>（例如：GitHub Actions）。</p>
<p>基于这一理念，我们建立了我们的 CI/CD 实践以及相应的工具包。</p>
<p>以下是我们 CI/CD 的工具包示例，包括以下部分：</p>
<ul>
<li>
<p><a href="https://github.com/QPod/lab-foundation/blob/main/tool.sh" target="_blank" rel="noopener noreferrer">tool.sh</a>：一个包含几个用于构建代码或项目的函数的 shell 脚本。</p>
</li>
<li>
<p><a href="https://github.com/QPod/lab-foundation/blob/main/.github/workflows/build-docker.yml" target="_blank" rel="noopener noreferrer">github workflow YAML</a>：一个特定于 CI/CD 平台的配置，使用上述的 <code>tool.sh</code>，并使用简单的 Linux shell 命令完成大部分构建任务。如你所见，每个作业都以 <code>source ./tool.sh</code> 开始，以使用我们工具包中定义的函数。</p>
</li>
</ul>
<h2 class="anchor anchorWithStickyNavbar_LWe7" id="其他选择">其他选择<a href="https://qpod.github.io/blog/2024-qpod-stack-ci-cd-practices-and-philosophy-cn#%E5%85%B6%E4%BB%96%E9%80%89%E6%8B%A9" class="hash-link" aria-label="Direct link to 其他选择" title="Direct link to 其他选择">​</a></h2>
<p>随着构建工件在开发/测试（暂存）/发布（生产）阶段的变化，我们发现从两个角度管理工件（artificats）很重要：</p>
<ol>
<li>
<p>每个工件都应该正确地进行版本控制和标记，特别是在测试（暂存）或生产环境需要回滚时。</p>
</li>
<li>
<p>用于发布（生产环境）的artifact仓库（例如 Docker registry）应与开发/测试（暂存）仓库分开，以便更好地管理生产环境的工件，并使用适当的资源。在许多情况下，生产环境需要一个高可用的工件仓库，并且最好与计算资源位于同一 VPC/区域，或者具有不同的推送/拉取权限。</p>
</li>
</ol>
<p>因此，我们选择将开发/测试阶段的工件和发布/生产工件放入不同的仓库，至少是不同的registry命名空间。在上面的 <code>tool.sh</code> 文件中，你可以看到我们使用一个变量 <code>DOCKER_IMG_NAMESPACE</code> 来决定工件应该被推送到哪个命名空间。此外，这个变量由 <strong>Git 分支名称前缀</strong> 决定：</p>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><span class="token plain">if [ "${CI_PROJECT_BRANCH}" = "main" ] ; then</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">    # 如果在主分支上，Docker 镜像命名空间将与 CI_PROJECT_NAME 的命名空间相同</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">    export CI_PROJECT_NAMESPACE="$(dirname ${CI_PROJECT_NAME})" ;</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">else</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">    # 不是主分支，Docker 命名空间设置为 {CI_PROJECT_NAME 的命名空间} + "0" + {CI_PROJECT_SPACE 中 / 之前的第一段}</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">    export CI_PROJECT_NAMESPACE="$(dirname ${CI_PROJECT_NAME})0${CI_PROJECT_SPACE}" ;</span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">fi</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_y97N"><path fill="currentColor" d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg viewBox="0 0 24 24" class="copyButtonSuccessIcon_LjdS"><path fill="currentColor" d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div>
<p>通过这种方式：</p>
<ul>
<li>
<p>当代码被推送或合并到 <code>main</code> 分支时，工件仓库命名空间将是我们示例中的 <code>qpod</code>。</p>
</li>
<li>
<p>当代码被推送到分支名称为 <code>dev/*</code> 的模式，并且创建了一个 PR 将这个分支 <code>dev/*</code> 合并到 <code>main</code>，工件将被推送到命名空间 <code>qpod0dev</code>（后缀 <code>0dev</code> 可以自定义）。</p>
</li>
<li>
<p>当代码被推送到没有 PR 合并到 <code>main</code> 的分支时，代码将不会被构建（流水线不会被触发执行）。</p>
</li>
</ul>]]></content:encoded>
            <category>QPod</category>
            <category>docs</category>
        </item>
        <item>
            <title><![CDATA[👏 Welcome | 欢迎]]></title>
            <link>https://qpod.github.io/blog/2024-welcome</link>
            <guid>https://qpod.github.io/blog/2024-welcome</guid>
            <pubDate>Tue, 13 Feb 2024 00:00:00 GMT</pubDate>
            <description><![CDATA[Welcome to QPod!]]></description>
            <content:encoded><![CDATA[<p>Welcome to QPod!</p>
<hr>
<p>We aim at providing a set of foundation building blocks helping AI and Software Engineers for their AI/Data and development work.</p>
<p>Please visit our GitHub Repo and read the README files in each sub-folder as we are updating the docs.</p>
<hr>
<p>我们的目标是提供一套基础构建模块，帮助人工智能和软件工程师进行他们的人工智能/数据和开发工作。</p>
<p>请访问我们的 GitHub 仓库，并阅读每个子文件夹中的 README 文件，因为我们正在更新文档。</p>]]></content:encoded>
            <category>QPod</category>
        </item>
    </channel>
</rss>