Skip to content

Commit 5730af0

Browse files
Fix GitHub Actions workflow for HACS distribution
- Handle repository initialization for first-time setup - Improve branch handling (main/master compatibility) - Add better error reporting and logging - Create setup documentation for HACS distribution The workflow now properly handles: - Non-existent distribution repositories - Branch creation and naming consistency - Robust error handling with detailed feedback Co-authored-by: Amp <amp@ampcode.com> Amp-Thread-ID: https://ampcode.com/threads/T-25786ba9-89f5-4416-ae1d-9fd85bde1c2d
1 parent bedf16b commit 5730af0

2 files changed

Lines changed: 153 additions & 17 deletions

File tree

.github/workflows/hacs-distribution.yml

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -140,28 +140,59 @@ jobs:
140140
This distribution repository is automatically updated when releases are tagged in the main repository.
141141
EOF
142142
143-
- name: Checkout distribution repository
144-
uses: actions/checkout@v4
145-
with:
146-
repository: ${{ env.DISTRIBUTION_REPO }}
147-
token: ${{ secrets.HACS_DEPLOY_TOKEN }}
148-
path: hacs-repo
149-
150-
- name: Update distribution repository
143+
- name: Checkout or initialize distribution repository
151144
run: |
152-
# Clear existing content (except .git)
153-
find hacs-repo -mindepth 1 -name '.git' -prune -o -type f -exec rm {} + 2>/dev/null || true
154-
find hacs-repo -mindepth 1 -name '.git' -prune -o -type d -exec rm -rf {} + 2>/dev/null || true
145+
echo "🔍 Attempting to checkout distribution repository: ${{ env.DISTRIBUTION_REPO }}"
155146
156-
# Copy new distribution files
157-
cp -r distribution/* hacs-repo/
158-
159-
cd hacs-repo
147+
# Try to checkout the existing repository
148+
if git clone https://${{ secrets.HACS_DEPLOY_TOKEN }}@github.com/${{ env.DISTRIBUTION_REPO }}.git hacs-repo 2>/dev/null; then
149+
echo "✅ Distribution repository exists, checked out successfully"
150+
cd hacs-repo
151+
152+
# Ensure we're on the main branch (GitHub's default)
153+
if git show-ref --verify --quiet refs/heads/main; then
154+
git checkout main
155+
echo "✅ Switched to main branch"
156+
elif git show-ref --verify --quiet refs/heads/master; then
157+
git checkout master
158+
# Rename master to main for consistency
159+
git branch -m master main
160+
echo "✅ Renamed master branch to main"
161+
else
162+
git checkout -b main
163+
echo "✅ Created main branch"
164+
fi
165+
else
166+
echo "⚠️ Distribution repository does not exist or is empty"
167+
echo "📝 Creating local repository structure for first-time setup..."
168+
169+
mkdir hacs-repo
170+
cd hacs-repo
171+
172+
# Initialize new repository
173+
git init --initial-branch=main
174+
git remote add origin https://${{ secrets.HACS_DEPLOY_TOKEN }}@github.com/${{ env.DISTRIBUTION_REPO }}.git
175+
176+
echo "✅ Initialized local repository with main branch"
177+
fi
160178
161179
# Configure git
162180
git config user.name 'HACS Distribution Bot'
163181
git config user.email 'actions@github.com'
164182
183+
echo "✅ Git configuration complete"
184+
185+
- name: Update distribution repository
186+
run: |
187+
cd hacs-repo
188+
189+
# Clear existing content (except .git)
190+
find . -mindepth 1 -name '.git' -prune -o -type f -exec rm {} + 2>/dev/null || true
191+
find . -mindepth 1 -name '.git' -prune -o -type d -exec rm -rf {} + 2>/dev/null || true
192+
193+
# Copy new distribution files
194+
cp -r ../distribution/* .
195+
165196
# Add all files
166197
git add .
167198
@@ -173,12 +204,26 @@ jobs:
173204
174205
# Commit and push
175206
git commit -m "Release v${{ steps.version.outputs.version }}
176-
207+
177208
Automated release from main repository
178209
Source: ${{ github.repository }}@${{ github.sha }}
179210
"
180211
181-
git push origin main
212+
echo "📤 Pushing to distribution repository..."
213+
214+
# Push to main branch, creating it if it doesn't exist on remote
215+
if git push -u origin main; then
216+
echo "✅ Successfully pushed to distribution repository"
217+
else
218+
echo "❌ Failed to push to distribution repository"
219+
echo "🔍 Checking remote repository status..."
220+
git remote -v
221+
echo "📋 Current branch:"
222+
git branch -a
223+
echo "📊 Repository status:"
224+
git status
225+
exit 1
226+
fi
182227
183228
- name: Create GitHub Release
184229
if: startsWith(github.ref, 'refs/tags/')

docs/setup-hacs-distribution.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# HACS Distribution Setup Guide
2+
3+
This guide explains how to set up the HACS distribution repository and GitHub Actions workflow.
4+
5+
## Prerequisites
6+
7+
Before running the automated distribution workflow, you need to:
8+
9+
### 1. Create the Distribution Repository
10+
11+
1. Go to GitHub and create a new **public** repository:
12+
- Repository name: `cgateweb-hacs`
13+
- Description: "C-Gate Web Bridge - Home Assistant Addon (HACS Distribution)"
14+
- Make it **public** (required for HACS)
15+
- **Do not** initialize with README, .gitignore, or license (the workflow will populate it)
16+
17+
### 2. Create a Personal Access Token
18+
19+
1. Go to GitHub Settings → Developer settings → Personal access tokens → Tokens (classic)
20+
2. Generate a new token with these permissions:
21+
- `repo` (Full control of private repositories)
22+
- `workflow` (Update GitHub Action workflows)
23+
3. Copy the token (you won't see it again!)
24+
25+
### 3. Add GitHub Secrets
26+
27+
In the main repository (`dougrathbone/cgateweb`):
28+
29+
1. Go to Settings → Secrets and variables → Actions
30+
2. Click "New repository secret"
31+
3. Add the secret:
32+
- Name: `HACS_DEPLOY_TOKEN`
33+
- Value: The personal access token from step 2
34+
35+
## How the Workflow Works
36+
37+
The GitHub Actions workflow (`.github/workflows/hacs-distribution.yml`) is triggered when:
38+
39+
- A version tag is pushed (e.g., `v1.0.0`)
40+
- Manually triggered via GitHub Actions UI
41+
42+
### Workflow Steps:
43+
44+
1. **Checkout & Test**: Gets the source code and runs tests
45+
2. **Build Distribution**: Creates the addon structure in `distribution/`
46+
3. **Initialize Repository**: Clones or initializes the HACS distribution repo
47+
4. **Deploy Content**: Copies distribution files to the HACS repo
48+
5. **Create Release**: Creates a GitHub release in the distribution repo
49+
50+
## First Run
51+
52+
When you run the workflow for the first time:
53+
54+
1. **Create the distribution repository** on GitHub (step 1 above)
55+
2. **Add the deploy token** as a secret (steps 2-3 above)
56+
3. **Tag a version** in the main repo:
57+
```bash
58+
git tag v1.0.0
59+
git push origin v1.0.0
60+
```
61+
4. **Monitor the workflow** in the Actions tab
62+
63+
The workflow will automatically:
64+
- Initialize the distribution repository
65+
- Set up the main branch
66+
- Populate it with the addon files
67+
- Create the first release
68+
69+
## Troubleshooting
70+
71+
### "Repository not found" error
72+
- Ensure the distribution repository exists and is public
73+
- Verify the `HACS_DEPLOY_TOKEN` has the correct permissions
74+
75+
### "Branch main doesn't exist" error
76+
- The workflow now handles this automatically
77+
- It will create the main branch if it doesn't exist
78+
79+
### Permission denied
80+
- Check that the personal access token has `repo` and `workflow` permissions
81+
- Ensure the token is added as `HACS_DEPLOY_TOKEN` in repository secrets
82+
83+
## Manual Testing
84+
85+
To test the workflow without creating a tag:
86+
87+
1. Go to Actions → "Build and Deploy HACS Distribution"
88+
2. Click "Run workflow"
89+
3. Select the branch and click "Run workflow"
90+
91+
This will create a development build with a timestamp-based version.

0 commit comments

Comments
 (0)