Setup dev branch with platformio.
pio project init --board esp32-s3-devkitc-1 --project-option "[email protected]" --project-option "framework=arduino"
SimpleLibrary
is an Arduino-compatible library designed for seamless use in Arduino IDE and PlatformIO. This repository employs a structured development workflow with three branches:
dev
for library development and testing.main
for production-ready releases.test
for verifying the library when installed as a dependency vialib_deps
.
The automated workflow ensures the main
branch always contains a clean, production-ready version of the library, while the test
branch verifies the library's usability as a dependency.
- Used for library development and testing.
- Contains:
- A complete PlatformIO project structure.
- The library code under
lib/SimpleLibrary
. - Test sketches in the
src
folder.
- Developers work exclusively in this branch.
- Used for production-ready library releases.
- Contains only the necessary files for the Arduino IDE or PlatformIO.
- Automatically updated via a GitHub Action whenever changes are pushed to the
dev
branch.
- Used for end-to-end testing of the library as an external dependency.
- Contains:
- A minimal PlatformIO project.
- A
platformio.ini
file referencing the library vialib_deps
from GitHub. - Allows developers to verify the library works correctly as an external dependency.
The dev
branch has the following structure:
SimpleLibrary/
├── lib/
│ └── SimpleLibrary/
│ ├── src/
│ │ ├── SimpleLibrary.cpp
│ │ └── SimpleLibrary.h
│ ├── examples/
│ │ ├── HelloWorld/
│ │ │ └── HelloWorld.ino
│ │ └── SimpleCalc/
│ │ └── SimpleCalc.ino
│ ├── library.json
│ ├── library.properties
│ └── LICENSE.txt
├── src/
│ └── main.cpp # Test code for the library
├── platformio.ini # PlatformIO configuration file
├── README.md
└── .github/
└── workflows/
└── update-main.yml # GitHub Action to update main branch
After automation, the main
branch contains the production-ready library:
SimpleLibrary/
├── src/
│ ├── SimpleLibrary.cpp
│ └── SimpleLibrary.h
├── examples/
│ ├── HelloWorld/
│ │ └── HelloWorld.ino
│ └── SimpleCalc/
│ └── SimpleCalc.ino
├── library.json
├── library.properties
├── LICENSE.txt
└── README.md
The test
branch is minimal, referencing the library as an external dependency:
SimpleLibrary/
├── src/
│ └── main.cpp # Test sketch using the library
├── platformio.ini # References the library from GitHub
└── README.md
-
Switch to the
test
Branch:git checkout test
-
Structure of
platformio.ini
: Ensureplatformio.ini
references the library from themain
branch:[env:esp32] platform = espressif32 board = esp32dev framework = arduino lib_deps = https://github.com/mdelgert/SimpleLibrary.git
-
Write Test Code: In
src/main.cpp
, include and use the library:#include <SimpleLibrary.h> SimpleLibrary lib; void setup() { Serial.begin(115200); lib.printHello(); int result = lib.addNumbers(3, 7); Serial.print("Result of 3 + 7: "); Serial.println(result); } void loop() { // Do nothing }
-
Run the Test:
- Build and upload the test sketch:
pio run --target upload
- Verify that the library works as expected.
- Build and upload the test sketch:
A GitHub Action automates the process of updating the main
branch whenever changes are pushed to the dev
branch. The workflow is saved as .github/workflows/update-main.yml
:
name: Update Main Branch
on:
push:
branches:
- dev
jobs:
update-main:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Configure Git User
run: |
git config user.name "GitHub Actions"
git config user.email "[email protected]"
- name: Switch to dev Branch
run: |
echo "Switching to dev branch..."
git checkout dev
- name: Delete Local Main Branch if Exists
run: |
echo "Deleting main branch if it exists locally..."
git branch -D main || true
- name: Create and Switch to Main Branch
run: |
echo "Creating and switching to main branch..."
git checkout -B main
- name: Check if Library Exists
run: |
LIBRARY_NAME="SimpleLibrary"
echo "Checking if lib/$LIBRARY_NAME exists in dev branch..."
if [ ! -d "lib/$LIBRARY_NAME" ]; then
echo "Error: lib/$LIBRARY_NAME does not exist in the dev branch."
exit 1
fi
- name: Remove Unnecessary Files
run: |
echo "Removing everything except files specified..."
find . -mindepth 1 \
-name '.git' -prune -o \
-name 'README.md' -prune -o \
-name '.gitignore' -prune -o \
-path './lib' -prune -o \
-exec rm -rf {} +
- name: Move Library Contents to Root Directory
run: |
LIBRARY_NAME="SimpleLibrary"
echo "Moving library contents to the root directory..."
mv lib/"$LIBRARY_NAME"/* . || { echo "Error: Failed to move files from lib"; exit 1; }
rm -rf lib
- name: Stage and Commit Changes
run: |
echo "Staging and committing changes..."
git add .
git commit -m "Update main branch" || echo "No changes to commit"
- name: Push Main Branch to Origin
run: |
echo "Pushing main branch to origin..."
git push origin main --force
-
Cleaner Production (
main
) Branch:- Contains only the necessary files for end users.
- Compatible with Arduino IDE and PlatformIO.
-
Efficient Development:
dev
branch provides a complete development environment with PlatformIO.
-
End-to-End Testing:
test
branch allows developers to verify the library as an external dependency.
-
Automation:
- GitHub Actions keeps
main
up-to-date automatically.
- GitHub Actions keeps