This document provides a comprehensive guide for setting up, configuring, and using the Scaffold-Py repository. It includes all the steps, code, and configuration changes made during the project. This guide is designed for future reference and to ensure you can easily replicate the setup or troubleshoot any issues.
Jorge Trivilin.
The Scaffold-Py repository provides a structured starting point for Python projects, with a focus on continuous integration (CI) across multiple cloud platforms: GitHub Actions, GCP Cloud Build, and Azure Pipelines. The scaffold includes configurations and tools for linting, testing, and formatting Python code.
scaffold-py/
├── .github/
│ └── workflows/
│ └── python-app.yml
├── .git/
├── logs_26487738085/
├── azure-pipelines.yml
├── azure-pipelines-log.txt
├── buildspec.yml
├── cloudbuild.yaml
├── Dockerfile
├── gcp-cloud-build-log.txt
├── hello.py
├── Makefile
├── README.md
├── requirements.txt
├── test_hello.py
└── test.txt
.github/workflows/python-app.yml
: Configuration for GitHub Actions to automate testing, linting, and formatting..git/
: Git repository metadata.logs_26487738085/
: Log files for debugging.azure-pipelines.yml
: Configuration for Azure Pipelines.azure-pipelines-log.txt
: Logs from Azure Pipelines builds.buildspec.yml
: Configuration for AWS CodeBuild.cloudbuild.yaml
: Configuration for GCP Cloud Build.Dockerfile
: Docker image configuration.gcp-cloud-build-log.txt
: Logs from GCP Cloud Build.hello.py
: Example Python script.Makefile
: Makefile to simplify common development tasks.README.md
: This documentation.requirements.txt
: Python dependencies.test_hello.py
: Example test script forhello.py
.test.txt
: Temporary test file.
Ensure you have Python 3.8 or higher installed. You also need to have make
installed on your system.
-
Clone the repository:
git clone https://github.com/jorge-trivilin/scaffold-py.git cd scaffold-py
-
Create and activate a virtual environment:
python3 -m venv venv source venv/bin/activate
-
Install dependencies:
make install
The Makefile simplifies common development tasks. Here is the complete Makefile used in the project:
install:
pip install --upgrade pip
pip install -r requirements.txt
lint:
pylint hello.py
test:
pytest
format:
autopep8 --in-place --aggressive --aggressive hello.py
The requirements.txt file lists the dependencies needed for the project:
pytest==6.2.5
pylint==2.10.2
autopep8==1.5.7
A simple script to demonstrate basic functionality.
"""
This module provides a simple addition function.
"""
def add(num1, num2):
"""This is an add function"""
return num1 + num2
if __name__ == "__main__":
print(add(1, 1))
Basic tests for the add
function using pytest.
from hello import add
def test_add():
assert add(1, 1) == 2
The .github/workflows/python-app.yml
file configures GitHub Actions to automate testing, linting, and formatting.
name: Python application
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Check out the repository
uses: actions/checkout@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Install dependencies
run: |
make install
- name: Lint code
run: |
make lint
- name: Test code
run: |
make test
- name: Format code
run: |
make format
- name: Commit changes
run: |
git config --global user.email "jorge.trivilin@gmail.com"
git config --global user.name "Jorge Trivilin"
git add hello.py
git commit -m "Auto-format code" || echo "No changes to commit"
- name: Push changes
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git push origin main
To activate GitHub Actions for this repository:
- Ensure that the repository has GitHub Actions enabled.
- Push any changes to the repository, and the GitHub Actions workflow will automatically run.
- Navigate to Repository Settings:
- Go to the repository on GitHub.
- Click on Settings.
- Configure Actions Settings:
- In the left sidebar, click on Actions.
- Then click on General.
- Set Workflow Permissions:
- Under Workflow permissions, select Read and write permissions.
- Click Save to save the changes.
The cloudbuild.yaml
file defines the steps to build and test the application.
steps:
- name: 'python:3.8'
entrypoint: 'bash'
args: ['-c', 'pip install --upgrade pip && pip install -r requirements.txt']
- name: 'python:3.8'
entrypoint: 'bash'
args: ['-c', 'make lint']
- name: 'python:3.8'
entrypoint: 'bash'
args: ['-c', 'make test']
- name: 'python:3.8'
entrypoint: 'bash'
args: ['-c', 'make format']
options:
logging: CLOUD_LOGGING_ONLY
machineType: 'N1_HIGHCPU_8'
timeout: '1200s'
logsBucket: 'gs://YOUR-BUCKET-NAME'
- Create a GCP project and enable the Cloud Build API.
- Create a bucket for logs.
- Connect your GitHub repository to Cloud Build.
- Push changes to trigger the build.
The azure-pipelines.yml
file defines the steps to build, lint, test, and format the application.
trigger:
branches:
include:
- main
pool:
name: 'scaffold-py'
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '3.11'
addToPath: true
- script: |
python -m venv myenv
source myenv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
displayName: 'Setup Python environment and install dependencies'
- script: |
source myenv/bin/activate
pylint hello.py
displayName: 'Run lint'
- script: |
source myenv/bin/activate
pytest
displayName: 'Run tests'
- Navigate to Azure DevOps and create a new project.
- Create a new pipeline and select your repository.
- Configure the pipeline using
azure-pipelines.yml
. - Save and run the pipeline.
- Download the agent package and extract it.
- Configure the agent:
./config.sh
- Run the agent:
./run.sh
- Create an access token on Docker Hub.
- Store the token in Azure DevOps as a pipeline secret.
- Update your pipeline to use the token for Docker operations.
- Lint the code:
make lint
- Run tests:
make test
- Format the code:
make format
-
Permission Denied for GitHub Actions Push:
- Ensure that the
GITHUB_TOKEN
has read and write permissions. - Verify that the
GITHUB_TOKEN
is correctly referenced in the workflow file.
- Ensure that the
-
Dependency Installation Issues:
- Ensure that the virtual environment is activated.
- Make sure
requirements.txt
lists all necessary dependencies.
Check the logs of the GitHub Actions, GCP Cloud Build, and Azure Pipelines run for detailed information about failures. The logs can be accessed from the respective cloud platform's interface.