From f29bf1d0124952d6537b18fe53a7af113d1b79d3 Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Tue, 31 May 2022 18:15:33 +0200 Subject: [PATCH] feat: added auto-update mechanism of kubectl --- .github/workflows/upgrader.yaml | 92 +++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 .github/workflows/upgrader.yaml diff --git a/.github/workflows/upgrader.yaml b/.github/workflows/upgrader.yaml new file mode 100644 index 0000000..bf675f3 --- /dev/null +++ b/.github/workflows/upgrader.yaml @@ -0,0 +1,92 @@ +name: SDK version upgrade + +on: + push: + schedule: + - cron: "0 */12 * * *" + +jobs: + check: + runs-on: ubuntu-latest + steps: + - name: Checkout repo + run: | + git config --global url."https://${{ secrets.GH_TOKEN }}:@github.com/".insteadOf "https://github.com/" + git clone https://github.com/actions-hub/kubectl . + + - name: Check if new version exist + id: check + run: | + SDK_VERSION=$(curl --silent "https://api.github.com/repos/kubernetes/kubernetes/releases" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/' | sort -V -r | head -n 1) + LATEST_VERSION=$(curl --silent "https://api.github.com/repos/actions-hub/kubectl/releases/latest" | tac | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/') + DOCKER_VERSION=$(cat $GITHUB_WORKSPACE/Dockerfile | grep "ARG KUBE_VERSION=" | sed -E 's/ARG KUBE_VERSION=//' | tr -d '"') + + echo "Provider tag: ${SDK_VERSION}" + echo "Current tag: ${LATEST_VERSION}" + echo "Docker tag: ${DOCKER_VERSION}" + + if [ "$SDK_VERSION" != "$LATEST_VERSION" ]; then + if [ "$LATEST_VERSION" = "`echo -e "$SDK_VERSION\n$LATEST_VERSION" | sort -V | head -n1`" ]; then + if [ "$DOCKER_VERSION" == "$LATEST_VERSION" ]; then + echo "New version detected: $SDK_VERSION. Latest in this repo: $LATEST_VERSION. Updating..." + echo ::set-output name=newest::"yes" + echo "SDK_VERSION=${SDK_VERSION}" >> $GITHUB_ENV + echo "LATEST_VERSION=${LATEST_VERSION}" >> $GITHUB_ENV + else + echo ::error::"Tag version in Dockerfile ($DOCKER_VERSION) is not the same as the latest release ($LATEST_VERSION)." + exit 1 + fi + else + echo ::set-output name=newest::"no" + fi + else + echo ::set-output name=newest::"no" + fi + + - name: Check if version tag is semver and if docker hub has it + id: test + if: steps.check.outputs.newest == 'yes' + run: | + if [[ $SDK_VERSION =~ ^v[0-9]+(\.[0-9]+){2,3}$ ]]; then + echo "$SDK_VERSION it's semver" + else + echo ::error::"$SDK_VERSION it's not semver!" + exit 1 + fi + echo "Checking if release with $SDK_VERSION tag is available" + status_code=$(curl --silent -I https://storage.googleapis.com/kubernetes-release/release/v1.24.0/bin/linux/amd64/kubectl | grep -E "^HTTP" | awk -F " " '{print $2}') + echo "Docker hub respond with $status_code" + if [ "$status_code" == "200" ] ; then + echo ::set-output name=exist::"yes" + echo "Newest version available" + else + echo ::set-output name=exist::"no" + echo "Docker hub returns $status_code" + fi + + - name: Modify Dockerfile + id: modify + if: success() && steps.check.outputs.newest == 'yes' && steps.test.outputs.exist == 'yes' + run: | + git config --global url."https://${{ secrets.GH_TOKEN }}:@github.com/".insteadOf "https://github.com/" + git config --global user.name "Serhiy Mytrovtsiy" + git config --global user.email "mitrovtsiy@ukr.net" + git checkout master + FROM_LINE="ARG KUBE_VERSION=\"$LATEST_VERSION\"" + TO_LINE="ARG KUBE_VERSION=\"$SDK_VERSION\"" + sed -i "s/${FROM_LINE}/${TO_LINE}/" $GITHUB_WORKSPACE/Dockerfile + git add $GITHUB_WORKSPACE/Dockerfile + git commit -m "updated kubectl to $SDK_VERSION" + git push + echo ::set-output name=tag::$SDK_VERSION + + - name: Create new release + if: success() && steps.modify.outputs.tag != '0' + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} + with: + tag_name: ${{ steps.modify.outputs.tag }} + release_name: ${{ steps.modify.outputs.tag }} + draft: false + prerelease: false \ No newline at end of file