From deae4ff1a69300bca96660131ab810c3e0a3a75c Mon Sep 17 00:00:00 2001 From: Chintan Kavathia Date: Tue, 26 Nov 2024 14:41:47 +0530 Subject: [PATCH] ci: workflow to update snapshots --- .github/workflows/test_and_deploy.yml | 21 +++++++++++ .github/workflows/update_snapshots.yml | 51 ++++++++++++++++---------- 2 files changed, 52 insertions(+), 20 deletions(-) diff --git a/.github/workflows/test_and_deploy.yml b/.github/workflows/test_and_deploy.yml index 1dc5bdebb..226515329 100644 --- a/.github/workflows/test_and_deploy.yml +++ b/.github/workflows/test_and_deploy.yml @@ -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://$SNAPSHOT_REPO_TOKEN@github.com/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://$SNAPSHOT_REPO_TOKEN@github.com/chintankavathia/ngx-datatable-snapshots.git temp_snapshots + cd temp_snapshots + git checkout ${{ env.branch }} env: SNAPSHOT_REPO_TOKEN: ${{ secrets.SNAPSHOT_REPO_TOKEN }} diff --git a/.github/workflows/update_snapshots.yml b/.github/workflows/update_snapshots.yml index 1cca5ce14..d78fc3029 100644 --- a/.github/workflows/update_snapshots.yml +++ b/.github/workflows/update_snapshots.yml @@ -44,33 +44,44 @@ jobs: cd snapshot-repo git config user.name "GitHub Actions" git config user.email "actions@github.com" - # 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 }}."