Skip to content

Commit bd6da90

Browse files
committed
Implement fixed branch strategy for workflows
Changes: - Replace timestamped branches with fixed branch (workflow-artifact-data) - Add automatic PR lifecycle management (close old, create new) - Make both fetch-sfs and upcoming-changes use same branch - Simplify html-export trigger to always use fixed branch Benefits: - Eliminates branch proliferation - Single coordination point for data updates - No manual cleanup needed - Temporal branches remain separate
1 parent ff60339 commit bd6da90

2 files changed

Lines changed: 56 additions & 16 deletions

File tree

.github/workflows/fetch-sfs-workflow.yml

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,22 @@ jobs:
9393
echo "Inga nya filer att committa"
9494
echo "has_changes=false" >> $GITHUB_OUTPUT
9595
else
96-
# Create a unique branch name for the commits
97-
TIMESTAMP=$(date +'%Y%m%d_%H%M%S')
98-
COMMIT_BRANCH="sfs_updates_${TIMESTAMP}"
99-
100-
# Create and switch to the new branch
101-
git checkout -b "$COMMIT_BRANCH"
96+
# Use fixed branch name from input (default: workflow-artifact-data)
97+
COMMIT_BRANCH="${{ inputs.branch_name }}"
98+
99+
# Check if branch exists remotely
100+
if git ls-remote --heads origin "$COMMIT_BRANCH" | grep -q "$COMMIT_BRANCH"; then
101+
echo "📥 Branch '$COMMIT_BRANCH' exists, checking out and merging with main..."
102+
# Branch exists - fetch and checkout
103+
git fetch origin "$COMMIT_BRANCH"
104+
git checkout "$COMMIT_BRANCH"
105+
# Merge latest main into fixed branch to keep it updated
106+
git merge origin/main --no-edit --strategy-option theirs || echo "Merge completed (conflicts auto-resolved)"
107+
else
108+
echo "🆕 Branch '$COMMIT_BRANCH' doesn't exist, creating new..."
109+
# Branch doesn't exist - create new
110+
git checkout -b "$COMMIT_BRANCH"
111+
fi
102112
103113
# Commit with multi-line message
104114
git commit -m "Automatisk uppdatering av SFS-författningar $(date +'%Y-%m-%d')" \
@@ -139,11 +149,28 @@ jobs:
139149
echo "::error::Git push misslyckades: ${PUSH_ERROR}"
140150
exit 1
141151
fi
142-
152+
153+
# Manage PR lifecycle: close old, create new
154+
echo "🔄 Hanterar PR för branch '$COMMIT_BRANCH'..."
155+
156+
# Close existing PR for this branch if it exists
157+
EXISTING_PR=$(gh pr list --head "$COMMIT_BRANCH" --json number --jq '.[0].number' 2>/dev/null || echo "")
158+
if [ -n "$EXISTING_PR" ]; then
159+
echo "Stänger befintlig PR #$EXISTING_PR"
160+
gh pr close "$EXISTING_PR" --comment "🔄 Stänger för att öppna ny PR med uppdaterad data från $(date +'%Y-%m-%d %H:%M')"
161+
fi
162+
163+
# Create new PR
164+
echo "📝 Skapar ny PR..."
165+
gh pr create --base main --head "$COMMIT_BRANCH" \
166+
--title "SFS Data Update - $(date +'%Y-%m-%d %H:%M')" \
167+
--body "Automatisk uppdatering av SFS-data. Inkluderar Käll-JSON, Markdown med selex-taggar och backup till R2. Workflow: fetch-sfs-workflow, Branch: $COMMIT_BRANCH, Run: ${{ github.run_id }}" \
168+
|| echo "⚠️ PR may already exist"
169+
143170
# Switch back to original branch
144171
git checkout "$CURRENT_BRANCH"
145-
146-
echo "Commits gjorda i branch '${COMMIT_BRANCH}' istället för '${CURRENT_BRANCH}'"
172+
173+
echo "Commits gjorda i branch '${COMMIT_BRANCH}'"
147174
fi
148175
149176
- name: Trigger HTML export workflow
@@ -152,10 +179,8 @@ jobs:
152179
with:
153180
github-token: ${{ secrets.GITHUB_TOKEN }}
154181
script: |
155-
const enableGitCommit = '${{ inputs.enable_git_commit }}' !== 'false';
156-
const sourceRef = enableGitCommit
157-
? '${{ steps.commit_changes.outputs.commit_branch }}'
158-
: '${{ inputs.branch_name }}';
182+
// Always use the fixed branch name
183+
const sourceRef = '${{ inputs.branch_name }}';
159184
160185
github.rest.actions.createWorkflowDispatch({
161186
owner: context.repo.owner,

.github/workflows/upcoming-changes-workflow.yml

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,28 @@ jobs:
5454
5555
- name: Commit and push kommande.yaml updates
5656
run: |
57-
# Lägg till den uppdaterade kommande.yaml filen
57+
# Checkout the same fixed branch as fetch-sfs uses
58+
WORKFLOW_BRANCH="workflow-artifact-data" # Match default from fetch-sfs
59+
60+
# Fetch and checkout the fixed branch
61+
echo "📥 Checking out fixed branch '$WORKFLOW_BRANCH'..."
62+
git fetch origin "$WORKFLOW_BRANCH" 2>/dev/null || echo "Branch doesn't exist yet"
63+
64+
if git ls-remote --heads origin "$WORKFLOW_BRANCH" | grep -q "$WORKFLOW_BRANCH"; then
65+
git checkout "$WORKFLOW_BRANCH"
66+
echo "✅ Checked out existing branch '$WORKFLOW_BRANCH'"
67+
else
68+
git checkout -b "$WORKFLOW_BRANCH"
69+
echo "🆕 Created new branch '$WORKFLOW_BRANCH'"
70+
fi
71+
72+
# Make changes
5873
git add output/kommande.yaml
5974
6075
if git diff --staged --quiet; then
6176
echo "Inga ändringar i kommande.yaml"
6277
else
6378
git commit -m "Uppdatera kommande ändringar - $(date +'%Y-%m-%d')"
64-
git push
65-
echo "✅ Kommande.yaml har uppdaterats och pushats"
79+
git push origin "$WORKFLOW_BRANCH"
80+
echo "✅ Kommande.yaml uppdaterat på branch $WORKFLOW_BRANCH"
6681
fi

0 commit comments

Comments
 (0)