在CentOS上為Rust項目配置CI/CD(持續集成和持續部署)可以通過多種方式實現,其中最常用的是使用GitLab CI/CD、GitHub Actions或Jenkins。以下是一個使用GitHub Actions的示例配置。
創建GitHub倉庫: 如果你還沒有一個GitHub倉庫,請先創建一個,并將你的Rust項目推送到該倉庫。
創建GitHub Actions工作流文件:
在你的Rust項目根目錄下創建一個名為 .github/workflows 的目錄,并在該目錄下創建一個新的YAML文件,例如 rust-ci.yml。
配置GitHub Actions工作流:
編輯 rust-ci.yml 文件,添加以下內容:
name: Rust CI/CD
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install Rust
run: rustup default stable
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- name: Check for vulnerabilities
run: cargo audit
- name: Deploy to Production (Optional)
if: github.ref == 'refs/heads/main'
run: |
# 這里可以添加部署腳本
echo "Deploying to production..."
這個配置文件定義了一個工作流,當有代碼推送到 main 分支或對 main 分支部發起Pull Request時,會觸發該工作流。工作流包括以下幾個步驟:
提交并推送配置文件:
將 .github/workflows/rust-ci.yml 文件提交到你的GitHub倉庫,并推送到遠程倉庫。
git add .github/workflows/rust-ci.yml
git commit -m "Add CI/CD configuration"
git push origin main
驗證CI/CD工作流: 推送代碼后,GitHub Actions會自動觸發工作流。你可以在GitHub倉庫的 “Actions” 標簽頁中查看工作流的執行情況。
如果你更喜歡使用GitLab CI/CD,可以按照以下步驟進行配置:
創建GitLab項目: 如果你還沒有一個GitLab項目,請先創建一個,并將你的Rust項目推送到該項目的倉庫。
創建 .gitlab-ci.yml 文件:
在你的Rust項目根目錄下創建一個名為 .gitlab-ci.yml 的文件。
配置GitLab CI/CD工作流:
編輯 .gitlab-ci.yml 文件,添加以下內容:
stages:
- build
- test
- deploy
build_job:
stage: build
image: rust:latest
script:
- rustup default stable
- cargo build --verbose
test_job:
stage: test
image: rust:latest
script:
- rustup default stable
- cargo test --verbose
deploy_job:
stage: deploy
image: rust:latest
script:
- echo "Deploying to production..."
# 這里可以添加部署腳本
這個配置文件定義了三個階段:構建、測試和部署。每個階段都有一個相應的任務。
提交并推送配置文件:
將 .gitlab-ci.yml 文件提交到你的GitLab項目倉庫,并推送到遠程倉庫。
git add .gitlab-ci.yml
git commit -m "Add CI/CD configuration"
git push origin main
驗證CI/CD工作流: 推送代碼后,GitLab CI/CD會自動觸發工作流。你可以在GitLab項目的 “CI/CD” 標簽頁中查看工作流的執行情況。
通過以上步驟,你可以在CentOS上為你的Rust項目配置CI/CD。根據你的需求,你可以進一步自定義和擴展這些配置。