Website Template Automation #21
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: Website Template Automation | |
on: | |
workflow_dispatch: | |
inputs: | |
git-url: | |
description: 'GitHub Repository URL to clone' | |
required: true | |
type: string | |
default: 'https://github.com/learning-zone/website-templates.git' | |
run-tests: | |
description: 'Run tests after modification' | |
required: false | |
type: boolean | |
default: true | |
jobs: | |
modify-templates: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 1 | |
- name: Set up Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: '3.11' | |
cache: 'pip' | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install gitpython beautifulsoup4 pytest requests | |
- name: Detect default branch and clone repository | |
id: clone-repo | |
run: | | |
# Remove any existing template directory | |
rm -rf website-templates | |
# Extract owner and repo name from URL | |
REPO_URL="${{ inputs.git-url }}" | |
REPO_OWNER=$(echo $REPO_URL | sed -E 's/.*github.com\/([^\/]+)\/([^\/]+)(\.git)?/\1/') | |
REPO_NAME=$(echo $REPO_URL | sed -E 's/.*github.com\/([^\/]+)\/([^\/]+)(\.git)?/\2/' | sed 's/.git$//') | |
# Use GitHub API to get default branch | |
DEFAULT_BRANCH=$(curl -s "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME" | grep -o '"default_branch": "[^"]*' | cut -d'"' -f4) | |
if [ -z "$DEFAULT_BRANCH" ]; then | |
echo "::error::Failed to detect default branch for repository: $REPO_URL" | |
exit 1 | |
fi | |
echo "Default branch is: $DEFAULT_BRANCH" | |
echo "default_branch=$DEFAULT_BRANCH" >> $GITHUB_OUTPUT | |
# Clone the repository with the detected default branch | |
git clone --depth 1 --branch "$DEFAULT_BRANCH" "$REPO_URL" website-templates | |
if [ $? -ne 0 ]; then | |
echo "::error::Failed to clone repository: $REPO_URL" | |
exit 1 | |
fi | |
- name: Run template modification | |
id: modify | |
run: | | |
echo "Starting template modification process..." | |
python clone_and_add_ads.py "website-templates" | |
env: | |
PYTHONUNBUFFERED: 1 | |
- name: Run tests | |
if: inputs.run-tests == true | |
run: | | |
echo "Running tests..." | |
python -m pytest test_clone_and_add_ads.py -v | |
continue-on-error: true | |
- name: Configure Git | |
run: | | |
git config --global user.name "Rekt-Developer" | |
git config --global user.email "[email protected]" | |
- name: Check for changes | |
id: git-check | |
run: | | |
if [[ -n "$(git status --porcelain)" ]]; then | |
echo "changes=true" >> $GITHUB_OUTPUT | |
else | |
echo "changes=false" >> $GITHUB_OUTPUT | |
fi | |
- name: Stage changes | |
if: steps.git-check.outputs.changes == 'true' | |
run: | | |
git add -A | |
git status | |
- name: Commit and push changes | |
if: steps.git-check.outputs.changes == 'true' | |
run: | | |
git commit -m "feat: Add advertisement placeholders to templates | |
- Added responsive ad containers | |
- Automated via GitHub Actions | |
- Repository: ${{ inputs.git-url }} | |
- Branch: ${{ steps.clone-repo.outputs.default_branch }} | |
- Timestamp: $(date -u +'%Y-%m-%d %H:%M:%S UTC')" | |
git push origin ${{ github.ref_name }} | |
if [ $? -ne 0 ]; then | |
echo "::error::Failed to push changes to repository" | |
exit 1 | |
fi | |
- name: Clean up | |
if: always() | |
run: | | |
echo "Cleaning up temporary files..." | |
rm -rf website-templates | |
find . -type d -name "__pycache__" -exec rm -rf {} + | |
- name: Create status report | |
if: always() | |
run: | | |
{ | |
echo "### Template Modification Report" | |
echo "- Repository: ${{ inputs.git-url }}" | |
echo "- Default Branch: ${{ steps.clone-repo.outputs.default_branch }}" | |
echo "- Run Date: $(date -u +'%Y-%m-%d %H:%M:%S UTC')" | |
echo "- Changes Made: ${{ steps.git-check.outputs.changes == 'true' && 'Yes' || 'No' }}" | |
if [[ "${{ steps.git-check.outputs.changes }}" == "true" ]]; then | |
echo "- Modified Files:" | |
git diff --name-only HEAD~1 HEAD | sed 's/^/ - /' | |
fi | |
if [[ "${{ inputs.run-tests }}" == "true" ]]; then | |
echo "- Tests Executed: Yes" | |
else | |
echo "- Tests Executed: No" | |
fi | |
} >> $GITHUB_STEP_SUMMARY |