Building and Packaging Code
Let’s introduce a build step to our GitHub Actions CI/CD workflow. Building is an essential part of many software projects as it compiles the source code, packages it into distributable formats, or performs other preparatory tasks needed before deployment. This step can range from compiling code, creating Docker images, or bundling scripts, depending on the type of application you're developing.
Building and Packaging
The build step transforms your source code into an artifact that can be deployed or distributed. For example, in web development, this might involve bundling JavaScript files, while in Python projects, it could mean creating a package or wheel distribution. The purpose of this step is to ensure that your application is in a deployable state and to catch any build-time errors before deploying the code.
Creating a Build Script
To keep things simple, we'll use a basic Python example for our build process. Suppose you have a Python project, and you want to package it as a source distribution and a wheel (if applicable).
1. Create a Build Script (Optional Step for Simplicity):
If you already have a setup.py script for your Python project, it can be used to create a source distribution and a wheel. If not, you can create a basic setup.py file:
# setup.py
from setuptools import setup, find_packages
setup(
name='simple-app',
version='0.1',
packages=find_packages(),
install_requires=[], # Add your project dependencies here
)
This setup file defines a minimal package structure for your project.
2. Commit the Build Script:
Add and commit the setup.py file to your repository:
git add setup.py
git commit -m "Add basic setup script for building"
git push origin main
Modifying the GitHub Actions Workflow to Include a Build Step
Next, we will extend our workflow file (ci.yml) to include the build step. This step will execute commands to create a build artifact (e.g., a source distribution and wheel for Python projects).
Edit the Workflow File:
Open .github/workflows/ci.yml and modify it to include a build step:
name: CI Workflow
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest setuptools
- name: Run tests
run: pytest tests/
- name: Build application
run: |
python setup.py sdist bdist_wheel
Explanation of the Workflow Steps:
Checkout code: This step fetches the repository's contents.
Set up Python: Sets up the Python environment for the build process.
Install dependencies: Installs pytest and any other dependencies for testing and building.
Run tests: Ensures that all tests pass before proceeding to the build step.
Build application: Executes the python setup.py sdist bdist_wheel command to create a source distribution and a wheel. This step packages your code for distribution.
Commit and Push Changes:
Save the modified workflow file, and commit and push the changes:
git add .github/workflows/ci.yml
git commit -m "Add build step to CI workflow"
git push origin main
Triggering and Viewing the Build Workflow Execution
Go to the GitHub Actions Tab:
Open your GitHub repository and navigate to the "Actions" tab. You should see a new workflow run triggered by the push event.
Inspecting the Build Step:
Click on the workflow run to view its progress. Expand the "Build application" step to see the output. If successful, you should see messages indicating that the source distribution and wheel have been created.
Handling Build Artifacts (Optional)
Storing Build Artifacts:
If you want to store the built artifacts as part of the workflow, you can use the actions/upload-artifact action to upload the files:
- name: Upload build artifacts
uses: actions/upload-artifact@v3
with:
name: build-artifacts
path: dist/
This step will upload the contents of the dist/ directory, which contains the built artifacts, to be available for download from the Actions tab.
Downloading Build Artifacts:
Artifacts can be downloaded from the Actions tab in GitHub after the workflow has completed. This is useful for sharing builds or using them in further stages, like deployment.
Considerations for Building Code in CI/CD Pipelines
Reproducibility: Ensure that the build process is reproducible across different environments by defining dependencies and configurations explicitly.
Caching Dependencies: Caching dependencies can speed up the build process in CI/CD pipelines, reducing the time spent downloading and installing libraries.
Build Errors: Any build errors encountered should be addressed promptly, as they can block further stages in the pipeline.
You have successfully added a build step to your CI/CD pipeline using GitHub Actions. This step packages your application for deployment, ensuring that it is in a deployable state and ready for further testing or deployment. In the next part, we'll focus on deploying the application to a test environment if all previous steps are successful. This process highlights the value of automation and consistency in modern software development workflows.