From bb6db1069c4d0b77c06b7c2e86bbbc5e081b7a66 Mon Sep 17 00:00:00 2001 From: Ayoub Nasr Date: Fri, 29 Nov 2024 11:53:21 +0100 Subject: [PATCH] Add a new workflow to create a dev branch --- .github/workflows/create-dev-branch.yaml | 145 +++++++++++++++++++++++ .github/workflows/crons.yaml | 4 + 2 files changed, 149 insertions(+) create mode 100644 .github/workflows/create-dev-branch.yaml diff --git a/.github/workflows/create-dev-branch.yaml b/.github/workflows/create-dev-branch.yaml new file mode 100644 index 0000000000..670cca7dfa --- /dev/null +++ b/.github/workflows/create-dev-branch.yaml @@ -0,0 +1,145 @@ +name: "Create Development Branch" +run-name: Create a new development branch + +on: + workflow_dispatch: + inputs: + bump-type: + description: "bump type" + required: true + type: choice + default: "major" + options: + - "major" + - "minor" + +jobs: + create-branch: + runs-on: ubuntu-24.04 + steps: + # validate branch + - name: Validate running branch + run: | + # we want to stop if this is not running on development branch + [[ "${{github.ref_name}}" =~ ^development/[0-9.]+$ ]] || exit 1 + # create app token + - uses: actions/create-github-app-token@v1 + id: app-token + with: + app-id: ${{ vars.ACTIONS_APP_ID }} + private-key: ${{ secrets.ACTIONS_APP_PRIVATE_KEY }} + # checkout + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + token: ${{ steps.app-token.outputs.token }} + # calculate new version + - name: Calculate new version + run: | + source VERSION + + if [[ "${{ inputs.bump-type }}" == "major" ]]; then + NEW_VERSION_MAJOR=$(($VERSION_MAJOR+1)) + NEW_VERSION_MINOR=0 + else + NEW_VERSION_MAJOR=$VERSION_MAJOR + NEW_VERSION_MINOR=$(($VERSION_MINOR+1)) + fi + + NEW_SHORT_VERSION="$NEW_VERSION_MAJOR.$NEW_VERSION_MINOR" + + echo "NEW_SHORT_VERSION=$NEW_SHORT_VERSION" >> $GITHUB_ENV + echo "NEW_VERSION_MAJOR=$NEW_VERSION_MAJOR" >> $GITHUB_ENV + echo "NEW_VERSION_MINOR=$NEW_VERSION_MINOR" >> $GITHUB_ENV + # calculate new branch name + - name: Calculate new branch name + run: | + NEW_BRANCH_NAME="development/${{ env.NEW_SHORT_VERSION }}" + INIT_BRANCH_NAME="feature/init-dev-${{ env.NEW_SHORT_VERSION }}" + + echo "NEW_BRANCH_NAME=$NEW_BRANCH_NAME" >> $GITHUB_ENV + echo "INIT_BRANCH_NAME=$INIT_BRANCH_NAME" >> $GITHUB_ENV + # create development branch + - name: Create new branch + run: | + if git ls-remote --exit-code --heads origin refs/heads/${{ env.NEW_BRANCH_NAME }}; then + echo "${{ env.NEW_BRANCH_NAME }} already exists, exiting" + exit 1 + fi + git checkout -b "${{ env.NEW_BRANCH_NAME }}" + # push development branch + - name: Push new branch + run: | + git config --global user.email ${{ github.actor }}@scality.com + git config --global user.name ${{ github.actor }} + git push --set-upstream origin ${{ env.NEW_BRANCH_NAME }} + # create new branch + - name: Create an init branch + run: | + git checkout -b "${{ env.INIT_BRANCH_NAME }}" + # find mentions of new version: warn user + - name: Look for important messages + id: warn_user + run: | + link="https://github.com/${{ github.repository }}/blob/${{ env.NEW_BRANCH_NAME }}/" + { + echo 'COMMENT<> "$GITHUB_ENV" + # bump version + - name: Bump VERSION + run: | + cat << EOF > VERSION + VERSION_MAJOR=${{ env.NEW_VERSION_MAJOR }} + VERSION_MINOR=${{ env.NEW_VERSION_MINOR }} + VERSION_PATCH=0 + VERSION_SUFFIX=-dev + EOF + # edit CHANGELOG + - name: add new entry to changelog + run: | + sed -i "s/# CHANGELOG/# CHANGELOG\n\n## Release ${{ env.NEW_SHORT_VERSION }}.0 (in development)/" CHANGELOG.md + # edit cron jobs + - name: bump version in cron jobs + run: | + current=$(grep current .github/workflows/crons.yaml | sed -E "s/\s*# current=//") + old=$(grep old .github/workflows/crons.yaml | sed -E "s/\s*# old=//") + sed -i "s/$current/${{ env.NEW_SHORT_VERSION }}/g" .github/workflows/crons.yaml + sed -i "s/$old/$current/g" .github/workflows/crons.yaml + # push + - name: push to init branch + run: | + git add VERSION + git add CHANGELOG.md + git add .github/workflows/crons.yaml + git commit -m "Update version to ${{ env.NEW_SHORT_VERSION }}" + git push --set-upstream origin ${{ env.INIT_BRANCH_NAME }} + # create PR + - name: create PR + uses: actions/github-script@v7 + with: + script: | + const pr = await github.rest.pulls.create({ + owner: context.repo.owner, + repo: context.repo.repo, + head: "${{ env.INIT_BRANCH_NAME }}", + base: "${{ env.NEW_BRANCH_NAME }}", + title: "Initialize ${{ env.NEW_BRANCH_NAME }} branch" + }); + + await github.rest.issues.createComment({ + issue_number: pr.data.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: `/approve` + }); + + await github.rest.issues.createComment({ + issue_number: pr.data.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: `${{ env.COMMENT }}` + }); diff --git a/.github/workflows/crons.yaml b/.github/workflows/crons.yaml index 5b8225df09..1fa3fe38a2 100644 --- a/.github/workflows/crons.yaml +++ b/.github/workflows/crons.yaml @@ -17,10 +17,14 @@ jobs: fail-fast: false matrix: include: + # these helper comments are needed by the dev branch workflow + # please do not edit them unless you're changing the version as well + # current=129.0 - name: "Nightly for MetalK8s 129.0" cron: "0 1 * * 1-5" branch: "development/129.0" workflow: "nightly.yaml" + # old=128.0 - name: "Nightly for MetalK8s 128.0" cron: "0 2 * * 1-5" branch: "development/128.0"