|
| 1 | +name: Update OADP Go Dependencies |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + # Run daily at 00:00 UTC |
| 6 | + - cron: '0 0 * * *' |
| 7 | + # Allow manual triggering |
| 8 | + workflow_dispatch: |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + pull-requests: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + update-oadp-go-dependencies: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout repository |
| 19 | + uses: actions/checkout@v4 |
| 20 | + with: |
| 21 | + fetch-depth: 0 |
| 22 | + |
| 23 | + - name: Extract Go version from go.mod |
| 24 | + id: go-version |
| 25 | + run: | |
| 26 | + GO_VERSION=$(grep -E "^go [0-9]+\.[0-9]+(\.[0-9]+)?" go.mod | awk '{print $2}') |
| 27 | + echo "version=$GO_VERSION" >> $GITHUB_OUTPUT |
| 28 | + echo "Extracted Go version: $GO_VERSION" |
| 29 | +
|
| 30 | + - name: Set up Go |
| 31 | + uses: actions/setup-go@v5 |
| 32 | + with: |
| 33 | + go-version: ${{ steps.go-version.outputs.version }} |
| 34 | + check-latest: true |
| 35 | + |
| 36 | + - name: Get branch list |
| 37 | + id: branch-list |
| 38 | + run: | |
| 39 | + # Get list of branches that match the pattern (master, main, oadp-*) |
| 40 | + # Filter out the "HEAD -> origin/master" line |
| 41 | + BRANCHES=$(git branch -r | grep -E 'origin/(master|main|oadp-.*)' | grep -v "HEAD ->" | sed 's/origin\///' | tr '\n' ' ') |
| 42 | + echo "branches=$BRANCHES" >> $GITHUB_OUTPUT |
| 43 | + echo "Found branches: $BRANCHES" |
| 44 | +
|
| 45 | + - name: Install GitHub CLI |
| 46 | + run: | |
| 47 | + sudo apt install gh -y |
| 48 | +
|
| 49 | + - name: Setup Git |
| 50 | + run: | |
| 51 | + git config --global user.email "action@github.com" |
| 52 | + git config --global user.name "GitHub Action" |
| 53 | +
|
| 54 | + - name: Authenticate GitHub CLI |
| 55 | + run: | |
| 56 | + echo "${{ secrets.GITHUB_TOKEN }}" | gh auth login --with-token |
| 57 | +
|
| 58 | + - name: Process each branch |
| 59 | + run: | |
| 60 | + # Store original branch to return to at the end |
| 61 | + ORIGINAL_BRANCH=$(git rev-parse --abbrev-ref HEAD) |
| 62 | + echo "Original branch: $ORIGINAL_BRANCH" |
| 63 | +
|
| 64 | + # Get the list of branches from previous step |
| 65 | + BRANCHES="${{ steps.branch-list.outputs.branches }}" |
| 66 | +
|
| 67 | + # Process each branch |
| 68 | + for BRANCH in $BRANCHES; do |
| 69 | + echo "==== Processing branch: $BRANCH ====" |
| 70 | +
|
| 71 | + # Create a new branch for the PR |
| 72 | + PR_BRANCH="update-deps-$BRANCH-$(date +%Y%m%d)" |
| 73 | + echo "Creating PR branch: $PR_BRANCH" |
| 74 | +
|
| 75 | + # Checkout the target branch first |
| 76 | + git checkout $BRANCH || { |
| 77 | + echo "Failed to checkout branch $BRANCH, skipping" |
| 78 | + continue |
| 79 | + } |
| 80 | +
|
| 81 | + # Create and checkout the PR branch |
| 82 | + git checkout -b $PR_BRANCH || { |
| 83 | + echo "Failed to create PR branch $PR_BRANCH, skipping" |
| 84 | + continue |
| 85 | + } |
| 86 | + # Determine Velero repository based on branch |
| 87 | + if [ "$BRANCH" == "master" ]; then |
| 88 | + VELERO_REPO="github.com/openshift/velero@konveyor-dev" |
| 89 | + else |
| 90 | + VELERO_REPO="github.com/openshift/velero@$BRANCH" |
| 91 | + fi |
| 92 | +
|
| 93 | + # Update dependencies |
| 94 | + echo "Updating dependencies for $BRANCH branch with openshift/oadp-operator@$BRANCH" |
| 95 | + go get github.com/openshift/oadp-operator@$BRANCH || echo "Warning: go get failed but continuing" |
| 96 | +
|
| 97 | + echo "Updating velero replacement to $VELERO_REPO" |
| 98 | + go mod edit -replace github.com/vmware-tanzu/velero=$VELERO_REPO |
| 99 | +
|
| 100 | + # Run go mod tidy |
| 101 | + go mod tidy |
| 102 | +
|
| 103 | + # Check if there are changes |
| 104 | + if git diff --quiet go.mod go.sum; then |
| 105 | + echo "No changes detected for branch $BRANCH, skipping PR creation" |
| 106 | + continue |
| 107 | + fi |
| 108 | +
|
| 109 | + # Show diff for logging purposes |
| 110 | + echo "Changes detected:" |
| 111 | + git diff --stat go.mod go.sum |
| 112 | +
|
| 113 | + # Commit changes |
| 114 | + git add go.mod go.sum |
| 115 | + git commit -m "Update dependencies for $BRANCH branch" |
| 116 | +
|
| 117 | + # Check if PR branch exists remotely |
| 118 | + echo "Checking if PR branch exists remotely..." |
| 119 | + if git ls-remote --heads origin $PR_BRANCH | grep -q $PR_BRANCH; then |
| 120 | + echo "PR branch exists remotely, checking if it's up to date..." |
| 121 | +
|
| 122 | + # Fetch the remote branch |
| 123 | + git fetch origin $PR_BRANCH |
| 124 | +
|
| 125 | + # Check if there are differences between local and remote |
| 126 | + if git diff --quiet HEAD origin/$PR_BRANCH; then |
| 127 | + echo "Local and remote branches are identical, no need to push" |
| 128 | + else |
| 129 | + echo "Local and remote branches differ, force pushing updates" |
| 130 | + git push -f -u origin $PR_BRANCH || { |
| 131 | + echo "Failed to push branch $PR_BRANCH, skipping PR creation" |
| 132 | + continue |
| 133 | + } |
| 134 | + fi |
| 135 | + else |
| 136 | + echo "PR branch doesn't exist remotely, pushing new branch" |
| 137 | + git push -u origin $PR_BRANCH || { |
| 138 | + echo "Failed to push branch $PR_BRANCH, skipping PR creation" |
| 139 | + continue |
| 140 | + } |
| 141 | + fi |
| 142 | +
|
| 143 | + # Check if PR already exists |
| 144 | + echo "Checking for existing PRs..." |
| 145 | + PR_EXISTS=$(gh pr list --head $PR_BRANCH --json number | jq length) |
| 146 | +
|
| 147 | + if [ "$PR_EXISTS" -eq "0" ]; then |
| 148 | + # Create PR if it doesn't exist |
| 149 | + echo "Creating new PR for branch $BRANCH" |
| 150 | + # VELERO_REPO is already defined above |
| 151 | +
|
| 152 | + gh pr create --base $BRANCH \ |
| 153 | + --head $PR_BRANCH \ |
| 154 | + --title "Update dependencies for $BRANCH branch" \ |
| 155 | + --body "Automated PR to update dependencies for $BRANCH branch. |
| 156 | +
|
| 157 | + Changes: |
| 158 | + - Updated github.com/openshift/oadp-operator to $BRANCH |
| 159 | + - Updated github.com/vmware-tanzu/velero replacement to $VELERO_REPO |
| 160 | + - Run go mod tidy" || echo "Warning: PR creation failed" |
| 161 | + else |
| 162 | + echo "PR already exists for branch $BRANCH, reusing PR" |
| 163 | + fi |
| 164 | +
|
| 165 | + echo "==== Completed processing for branch: $BRANCH ====" |
| 166 | + done |
| 167 | +
|
| 168 | + # Return to original branch |
| 169 | + echo "Returning to original branch: $ORIGINAL_BRANCH" |
| 170 | + git checkout $ORIGINAL_BRANCH || echo "Warning: Failed to return to original branch $ORIGINAL_BRANCH" |
| 171 | + env: |
| 172 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments