Update artifacts after stash #75
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: ESPHome Build and Commit Manifest | |
on: | |
push: #TODO: revert to just tags once working | |
branches: | |
- main | |
# push: | |
# tags: | |
# - '[0-9]+.[0-9]+.[0-9]+*' | |
jobs: | |
check-tag: | |
runs-on: ubuntu-latest | |
outputs: | |
tag_on_main: ${{ steps.check_tag.outputs.tag_on_main }} | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Check if tag is on main branch | |
id: check_tag | |
run: | | |
TAG_NAME=${GITHUB_REF#refs/tags/} | |
TAG_COMMIT=$(git rev-list -n 1 $TAG_NAME) | |
MAIN_COMMIT=$(git rev-parse origin/main) | |
if git merge-base --is-ancestor $TAG_COMMIT $MAIN_COMMIT; then | |
echo "tag_on_main=true" >> $GITHUB_OUTPUT | |
echo "Tag is on main branch" | |
else | |
echo "tag_on_main=false" >> $GITHUB_OUTPUT | |
echo "Tag is not on main branch" | |
fi | |
build: | |
needs: check-tag | |
if: needs.check-tag.outputs.tag_on_main == 'true' | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
target: [esp32s2, esp32] | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
with: | |
token: ${{ secrets.PAT }} | |
fetch-depth: 0 # Fetch all history for all tags and branches | |
- name: Get version from git tag | |
id: get_version | |
run: | | |
VERSION=$(git describe --tags --abbrev=0 2>/dev/null || echo "0.0.0") | |
echo "VERSION=$VERSION" >> $GITHUB_ENV | |
echo "version=$VERSION" >> $GITHUB_OUTPUT | |
- name: Create empty secrets file | |
run: | | |
touch firmware/secrets.yaml | |
touch firmware/conf.d/secrets.yaml | |
- name: Set git sha | |
run: echo "RELEASE_SUMMARY=$(git log -1 --pretty=%B)" >> $GITHUB_ENV | |
- name: Build ESPHome firmware | |
uses: esphome/[email protected] | |
with: | |
yaml-file: ${{ github.workspace }}/firmware/config-${{ matrix.target }}.yaml | |
release-summary: ${{ env.RELEASE_SUMMARY }} | |
#release-url: "https://raw.githubusercontent.com/spuder/OpenSpool/main/artifacts/openspool-${{ matrix.target }}/manifest.json" | |
complete-manifest: false | |
- name: ls | |
run: | | |
ls -la | |
ls openspool-${{ matrix.target }}/* || true | |
- name: Generate md5 checksum | |
run: | | |
mkdir -p artifacts/openspool-${{ matrix.target }} | |
mv -f openspool-${{ matrix.target }}/*.bin artifacts/openspool-${{ matrix.target }}/ | |
for file in artifacts/openspool-${{ matrix.target }}/*.bin; do | |
md5sum "$file" | awk '{print $1}' > "${file}.md5" | |
md5_length=$(wc -c < "${file}.md5") | |
if [ "$md5_length" -ne 33 ]; then | |
echo "MD5 generation failed for $file (length: $md5_length)" | |
exit 1 | |
fi | |
done | |
- name: Upload artifacts | |
uses: actions/upload-artifact@v4 | |
with: | |
name: openspool-${{ matrix.target }} | |
path: artifacts/openspool-${{ matrix.target }} | |
retention-days: 5 | |
if-no-files-found: error | |
combine-manifests: | |
needs: [check-tag, build] | |
if: needs.check-tag.outputs.tag_on_main == 'true' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
with: | |
token: ${{ secrets.PAT }} | |
fetch-depth: 0 # Fetch all history for all tags and branches | |
- name: Get version from git tag | |
id: get_version | |
run: | | |
VERSION=$(git describe --tags --abbrev=0 2>/dev/null || echo "0.0.0") | |
echo "VERSION=$VERSION" >> $GITHUB_ENV | |
echo "version=$VERSION" >> $GITHUB_OUTPUT | |
- name: Download all artifacts | |
uses: actions/download-artifact@v4 | |
with: | |
path: downloaded-artifacts | |
- name: Combine manifests | |
run: | | |
python3 - <<EOF | |
import json | |
import sys | |
import os | |
print("Contents of downloaded-artifacts:") | |
for root, dirs, files in os.walk("downloaded-artifacts"): | |
for file in files: | |
print(os.path.join(root, file)) | |
version = "${{ env.VERSION }}" | |
combined_manifest = { | |
"name": "OpenSpool", | |
"version": version, | |
"builds": [], | |
"new_install_prompt_erase": True | |
} | |
for target in ['esp32s2', 'esp32']: | |
try: | |
with open(f'downloaded-artifacts/openspool-{target}/manifest.json', 'r') as f: | |
manifest = json.load(f) | |
# Add the entire manifest to the builds array | |
combined_manifest['builds'].append(manifest) | |
except FileNotFoundError: | |
print(f"Warning: manifest for {target} not found") | |
except json.JSONDecodeError: | |
print(f"Error: Invalid JSON in manifest for {target}") | |
with open('artifacts/manifest.json', 'w') as f: | |
json.dump(combined_manifest, f, indent=2) | |
print(f"Combined manifest created with version: {version}") | |
print("Combined manifest content:") | |
print(json.dumps(combined_manifest, indent=2)) | |
EOF | |
- name: Update manifest | |
run: | | |
# Stash all changes | |
git stash push -m "Stash before switching to main" | |
# Switch to the main branch | |
git checkout main | |
# Pull latest changes from main | |
git pull origin main | |
# Apply the stash | |
git stash apply | |
# Set Git username and email | |
git config user.name "GitHub Actions" | |
git config user.email "[email protected]" | |
# Add all changes in the artifacts/ folder | |
git add artifacts/ | |
git diff --staged || true | |
# Commit changes if there are any | |
if git diff --staged --quiet; then | |
echo "No changes to commit" | |
exit 1 | |
else | |
git commit -m "Update artifacts after stash" | |
# Push changes to main | |
git push origin main | |
fi | |
# Show final status | |
git status |