Skip to content

Commit

Permalink
ci: workflow to update snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
chintankavathia committed Nov 29, 2024
1 parent 857218c commit deae4ff
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 20 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/test_and_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,30 @@ jobs:
with:
node-version: lts/iron

- name: Set the branch name
id: set_branch
run: |
BRANCH_NAME=${{ github.head_ref }}
echo "Branch name is: $BRANCH_NAME"
# Check for remote branch
REMOTE_BRANCH=$(git ls-remote --heads https://[email protected]/chintankavathia/ngx-datatable-snapshots.git $BRANCH_NAME)
echo "Remote branch lookup result: $REMOTE_BRANCH"
if [ -n "$REMOTE_BRANCH" ]; then
echo "Branch $BRANCH_NAME exists"
echo "branch=$BRANCH_NAME" >> $GITHUB_ENV
else
echo "Branch $BRANCH_NAME does not exist, defaulting to main"
echo "branch=main" >> $GITHUB_ENV
fi
- name: Clone Snapshot Repository
run: |
git clone https://[email protected]/chintankavathia/ngx-datatable-snapshots.git temp_snapshots
cd temp_snapshots
git checkout ${{ env.branch }}
env:
SNAPSHOT_REPO_TOKEN: ${{ secrets.SNAPSHOT_REPO_TOKEN }}

Expand Down
51 changes: 31 additions & 20 deletions .github/workflows/update_snapshots.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,44 @@ jobs:
cd snapshot-repo
git config user.name "GitHub Actions"
git config user.email "[email protected]"
# Automatically generate branch name
BRANCH_NAME="snapshot-update-${{ github.run_id }}"
git checkout -b $BRANCH_NAME
# Ensure remote is set up correctly
git remote set-url origin https://${SNAPSHOT_REPO_TOKEN}@github.com/chintankavathia/ngx-datatable-snapshots.git
# Fetch remote branches to avoid the "no upstream branch" error
git fetch origin
BRANCH_NAME=${GITHUB_REF##refs/heads/}
# Check if the branch exists on the remote (origin)
if git ls-remote --heads origin "$BRANCH_NAME" | grep -q "$BRANCH_NAME"; then
# If the branch exists remotely, just checkout the branch
git checkout "$BRANCH_NAME"
else
# If the branch doesn't exist remotely, create and checkout the branch, then push
git checkout -b "$BRANCH_NAME"
git push --set-upstream origin "$BRANCH_NAME"
fi
- name: Copy generated snapshots
run: |
rsync -av --delete playwright/snapshots/ snapshot-repo/
rsync -av --delete playwright/snapshots/ snapshot-repo/snapshots
- name: Commit and push changes
- name: Commit and push changes to snapshot repo
working-directory: snapshot-repo
run: |
git add .
# Automatically generate commit message
COMMIT_MESSAGE="Update Playwright snapshots (Run ID: ${{ github.run_id }})"
git commit -m "$COMMIT_MESSAGE"
git commit -m "$COMMIT_MESSAGE" || echo "No changes to commit"
# Ensure remote is set up correctly
git remote set-url origin https://${{ secrets.SNAPSHOT_REPO_TOKEN }}@github.com/chintankavathia/ngx-datatable-snapshots.git
git push origin $BRANCH_NAME
- name: Create pull request
uses: peter-evans/create-pull-request@v5
with:
token: ${{ secrets.SNAPSHOT_REPO_TOKEN }}
repository: chintankavathia/ngx-datatable-snapshots.git
base: main
head: $BRANCH_NAME
# Automatically generate title and body
title: 'Update Playwright Snapshots (Run ID: ${{ github.run_id }})'
body: |
This PR updates the Playwright snapshots generated by workflow run ID: ${{ github.run_id }}.
- Base branch: ${{ github.ref_name }}
- Commit: ${{ github.sha }}
- name: Create a pull request
run: |
git remote set-url origin https://${{ secrets.SNAPSHOT_REPO_TOKEN }}@github.com/chintankavathia/ngx-datatable-snapshots.git
# We need to re-auth in order to create PR
echo "${{ secrets.SNAPSHOT_REPO_TOKEN }}" | gh auth login --with-token
gh pr create --base main --head ${{ BRANCH_NAME }} --title "Update Playwright snapshots" --body "Automated PR to update snapshots based on run ID ${{ github.run_id }}."

0 comments on commit deae4ff

Please sign in to comment.