|
| 1 | +name: Update Calico Version Nightly |
| 2 | +on: |
| 3 | + schedule: |
| 4 | + - cron: '0 2 * * *' # Every day at 2am UTC |
| 5 | + workflow_dispatch: |
| 6 | +jobs: |
| 7 | + update-calico-version: |
| 8 | + runs-on: ubuntu-latest |
| 9 | + steps: |
| 10 | + - name: Checkout repository |
| 11 | + uses: actions/checkout@v4 |
| 12 | + |
| 13 | + - name: Get latest Calico release version |
| 14 | + id: get_calico_version |
| 15 | + run: | |
| 16 | + # Function to get Calico version with retries |
| 17 | + get_calico_version() { |
| 18 | + local retries=3 |
| 19 | + local delay=5 |
| 20 | +
|
| 21 | + for ((i=1; i<=retries; i++)); do |
| 22 | + echo "Attempt $i to fetch Calico version..." |
| 23 | +
|
| 24 | + # Make API call with timeout |
| 25 | + response=$(curl -s --max-time 30 https://api.github.com/repos/projectcalico/calico/releases/latest) |
| 26 | +
|
| 27 | + if [ $? -eq 0 ] && [ -n "$response" ]; then |
| 28 | + # Extract tag_name and validate it's not null/empty |
| 29 | + version=$(echo "$response" | jq -r '.tag_name // empty') |
| 30 | +
|
| 31 | + if [ -n "$version" ] && [ "$version" != "null" ] && [[ "$version" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 32 | + echo "Successfully fetched Calico version: $version" |
| 33 | + echo "calico_version=$version" >> $GITHUB_OUTPUT |
| 34 | + return 0 |
| 35 | + else |
| 36 | + echo "Invalid version format: '$version'" |
| 37 | + fi |
| 38 | + else |
| 39 | + echo "API call failed or returned empty response" |
| 40 | + fi |
| 41 | +
|
| 42 | + if [ $i -lt $retries ]; then |
| 43 | + echo "Retrying in $delay seconds..." |
| 44 | + sleep $delay |
| 45 | + fi |
| 46 | + done |
| 47 | +
|
| 48 | + echo "Failed to fetch valid Calico version after $retries attempts" |
| 49 | + exit 1 |
| 50 | + } |
| 51 | +
|
| 52 | + get_calico_version |
| 53 | +
|
| 54 | + - name: Update default calicoVersion in action.yml |
| 55 | + id: update_version |
| 56 | + run: | |
| 57 | + version=${{ steps.get_calico_version.outputs.calico_version }} |
| 58 | +
|
| 59 | + # Double-check the version is valid before updating |
| 60 | + if [ -z "$version" ] || [ "$version" = "null" ] || ! [[ "$version" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 61 | + echo "Error: Invalid Calico version '$version' - aborting update" |
| 62 | + exit 1 |
| 63 | + fi |
| 64 | +
|
| 65 | + echo "Updating Calico version to: $version" |
| 66 | +
|
| 67 | + # Extract current default under the calicoVersion block |
| 68 | + current_version=$(sed -n "/^ calicoVersion:$/,/^ [a-zA-Z]\+:/p" action.yml | grep -o "default: '[^']*'" | sed "s/default: '\(.*\)'/\1/") |
| 69 | +
|
| 70 | + if [ -z "$current_version" ]; then |
| 71 | + echo "Could not determine current Calico version from action.yml" |
| 72 | + exit 1 |
| 73 | + fi |
| 74 | +
|
| 75 | + if [ "$current_version" = "$version" ]; then |
| 76 | + echo "Calico version is already up to date ($version)" |
| 77 | + echo "updated=false" >> $GITHUB_OUTPUT |
| 78 | + exit 0 |
| 79 | + fi |
| 80 | +
|
| 81 | + # Update only within the calicoVersion block |
| 82 | + sed -i.bak -E "/^ calicoVersion:$/,/^ [a-zA-Z]+:/{s/(default: ')[^']+(')/\1$version\2/}" action.yml |
| 83 | + rm action.yml.bak |
| 84 | +
|
| 85 | + echo "Successfully updated Calico version from $current_version to $version" |
| 86 | + echo "updated=true" >> $GITHUB_OUTPUT |
| 87 | +
|
| 88 | + - name: Create Pull Request |
| 89 | + if: steps.update_version.outputs.updated == 'true' |
| 90 | + uses: peter-evans/create-pull-request@v6 |
| 91 | + with: |
| 92 | + commit-message: "Update Calico version to ${{ steps.get_calico_version.outputs.calico_version }}" |
| 93 | + title: "Update Calico version to ${{ steps.get_calico_version.outputs.calico_version }}" |
| 94 | + body: "Automated PR to update default calicoVersion in action.yml to the latest release." |
| 95 | + branch: "update-calico-version-${{ steps.get_calico_version.outputs.calico_version }}" |
| 96 | + delete-branch: true |
| 97 | + |
0 commit comments