該專案是一個快速範例項目,演示了應用程式在推送到主分支時自動建置、測試和部署到暫存環境的過程。
為了充分示範 CI/CD 管道,我們將使用最少的程式碼建立一個簡單的 Python 項目,然後將其整合到 GitHub Actions 中。
就像我之前所說的,我們將創建一個將在管道中使用的簡單項目。我選擇在 python 中執行此操作沒有特殊原因,您可以使用您選擇的任何其他程式語言,這個專案的主要目的是演示管道。
所以繼續建立專案資料夾並導航到該資料夾:
mkdir automated-testing cd automated-testing
現在我們將編寫簡單的Python應用程式。在專案資料夾中建立一個新檔案app.py。
touch app.py
將以下程式碼區塊加入檔案:
def hello(): return "Hello, World!" if __name__ == "__main__": print(hello())
這是一個非常簡單的 Python「Hello world」函數,作為可以在 CI 管道中測試的基本功能的範例。
def hello() 定義了一個名為 hello 的函數,它不帶任何參數。呼叫此函數時,它會傳回字串「Hello, World!」。
if __name__ == "__main__" 是一個標準的 Python 構造,用於確保某些程式碼僅在直接執行檔案時運行(而不是作為模組導入時)。它充當腳本的入口點。
直接執行 app.py 時(例如,透過執行 python app.py),腳本將呼叫 hello() 函數並列印結果,即「Hello, World!」。
典型的專案會有依賴項,在 python 專案中,它們通常在requirements.txt 檔案中定義。
建立新檔案requirements.txt
touch requirements.txt
將其新增至檔案:
pytest
現在我們將新增一個基本測試檔test_app.py來測試app.py中的功能。將以下內容新增至文件:
from app import hello def test_hello(): assert hello() == "Hello, World!"
現在我們已準備好建立管道。
要設定 GitHub 操作,我們需要在儲存庫中建立一個 .github/workflows 資料夾,這是我們向 GitHub 通知儲存庫中的 CI/CD 管道的方式。
建立一個新檔案:
mkdir -p .github/workflows
您可以在一個儲存庫中擁有多個管道,因此請在 .github/workflows 資料夾中建立一個檔案 proj.yml。我們將在這裡定義建置、測試和部署 Python 專案的步驟。
將以下程式碼加入您的檔案:
name: Build, Test and Deploy # Trigger the workflow on pushes to the main branch on: push: branches: - main jobs: build-and-test: runs-on: ubuntu-latest steps: # Checkout the code from the repository - name: Checkout repo uses: actions/checkout@v4 # Set up Python environment - name: Setup Python uses: actions/setup-python@v5 with: python-version: '3.x' # Install dependencies - name: Install Dependecies run: | python -m pip install --upgrade pip pip install -r requirements.txt # Build (this project deosn't require a build but we will simulate a build by creating a file) - name: Build Project run: | mkdir -p build # Simulate build output by creating a file touch build/output_file.txt # Run tests using pytest - name: Run tests run: pytest # Upload the build output as an artifact (we created a file in the build step to simulate an artifact) - name: Upload build artifact uses: actions/upload-artifact@v4 with: name: build-artifact path: build/ deploy: runs-on: ubuntu-latest needs: build-and-test if: success() steps: # Download the artifact from the build stage - name: Download build artifact uses: actions/download-artifact@v4 with: name: build-artifact path: build/ - name: Simulate Deployment run: | echo "Deploying to staging..." ls build/
If you haven't already, you need to initialize a git repository using the commands below:
git init git add . git commit -m "Create project as well as CI/CD pipeline"
Now we push to GitHub. Create a GitHub repository and push your code using the below commands:
git remote add origin <your-repo-url> git push -u origin main
After pushing the code, you can visit the Actions tab in your GitHub repository. You should see the pipeline triggered, running the steps defined in your proj.yml file.
If everything is set up correctly, the pipeline will build, test, and simulate deployment. You can changes things around in your project and make new pushes to see the the pipeline works, create errors intentional so you can see how the pipeline works when the tests fail.
On a successful run this is how your Actions tab should look.
And that's it, this setup provides a working example of a CI/CD pipeline for a very basic Python project. If you found this helpful, please share with your connection and if you have any questions, do not hesitate to drop the question in the comments.
以上是使用 GitHub Actions 自動化建置、測試和部署流程的詳細內容。更多資訊請關注PHP中文網其他相關文章!