From 11ce26a4b5e09c8323c0d6407586c6f9b58b3ec2 Mon Sep 17 00:00:00 2001 From: Huy Mai Date: Tue, 17 Dec 2024 11:15:06 +0200 Subject: [PATCH] Add release github workflow Now that the release workflow is working in IPAM, we can also add it to BMO repo. Signed-off-by: Huy Mai --- .github/workflows/build-images-action.yml | 23 +++++ .github/workflows/release.yaml | 111 ++++++++++++++++++---- 2 files changed, 118 insertions(+), 16 deletions(-) diff --git a/.github/workflows/build-images-action.yml b/.github/workflows/build-images-action.yml index 1bc6ed21eb..e1cf79dd44 100644 --- a/.github/workflows/build-images-action.yml +++ b/.github/workflows/build-images-action.yml @@ -10,20 +10,42 @@ on: - 'release-*' tags: - 'v*' + workflow_run: + workflows: + - "Create Release" + types: + - completed jobs: + set_ref: + runs-on: ubuntu-latest + outputs: + github_ref: ${{ steps.set_ref.outputs.github_ref }} + steps: + - name: Set ref + id: set_ref + run: | + if [[ "${{ github.event_name }}" == "workflow_run" ]] && [[ -n "${{ needs.create-release.outputs.release_tag }}" ]]; then + echo "GITHUB_REF=refs/heads/${{ needs.create-release.outputs.release_tag }}" >> "${GITHUB_OUTPUT}" + else + echo "GITHUB_REF=${{ github.ref }}" >> "${GITHUB_OUTPUT}" + fi + build_bmo: + needs: set_ref name: Build BMO container image if: github.repository == 'metal3-io/baremetal-operator' uses: metal3-io/project-infra/.github/workflows/container-image-build.yml@main with: image-name: 'baremetal-operator' pushImage: true + ref: ${{ needs.set_ref.outputs.github_ref }} secrets: QUAY_USERNAME: ${{ secrets.QUAY_USERNAME }} QUAY_PASSWORD: ${{ secrets.QUAY_PASSWORD }} SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }} build_keepalived: + needs: set_ref name: Build keepalived container image if: github.repository == 'metal3-io/baremetal-operator' uses: metal3-io/project-infra/.github/workflows/container-image-build.yml@main @@ -31,6 +53,7 @@ jobs: image-name: 'keepalived' dockerfile-directory: resources/keepalived-docker pushImage: true + ref: ${{ needs.set_ref.outputs.github_ref }} secrets: QUAY_USERNAME: ${{ secrets.QUAY_USERNAME }} QUAY_PASSWORD: ${{ secrets.QUAY_PASSWORD }} diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 68c64163eb..a337147214 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -1,39 +1,118 @@ -name: release +# This code is borrowed from https://github.com/kubernetes-sigs/cluster-api/blob/main/.github/workflows/release.yaml +name: Create Release on: push: - # Sequence of patterns matched against refs/tags - tags: - - "v*" + branches: + - main + paths: + - 'releasenotes/*.md' permissions: {} jobs: - build: - name: tag release + push_release_tags: + permissions: + contents: write runs-on: ubuntu-latest - + outputs: + release_tag: ${{ steps.release-version.outputs.release_version }} + if: github.repository == 'metal3-io/ip-address-manager' + steps: + - name: Checkout code + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Get changed files + id: changed-files + uses: tj-actions/changed-files@4edd678ac3f81e2dc578756871e4d00c19191daf # tag=v45.0.4 + - name: Get release version + id: release-version + run: | + if [[ ${{ steps.changed-files.outputs.all_changed_files_count }} != 1 ]]; then + echo "1 release notes file should be changed to create a release tag, found ${{ steps.changed-files.outputs.all_changed_files_count }}" + exit 1 + fi + for changed_file in ${{ steps.changed-files.outputs.all_changed_files }}; do + export RELEASE_VERSION=$(echo "${changed_file}" | grep -oP '(?<=/)[^/]+(?=\.md)') + echo "RELEASE_VERSION=${RELEASE_VERSION}" >> ${GITHUB_ENV} + echo "RELEASE_VERSION=${RELEASE_VERSION}" >> ${GITHUB_OUTPUT} + if [[ "${RELEASE_VERSION}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?(\+[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$ ]]; then + echo "Valid semver: ${RELEASE_VERSION}" + else + echo "Invalid semver: ${RELEASE_VERSION}" + exit 1 + fi + done + - name: Determine the release branch to use + run: | + if [[ ${RELEASE_VERSION} =~ beta ]] || [[ ${RELEASE_VERSION} =~ alpha ]]; then + export RELEASE_BRANCH=main + echo "RELEASE_BRANCH=${RELEASE_BRANCH}" >> ${GITHUB_ENV} + echo "This is a beta or alpha release, will use release branch ${RELEASE_BRANCH}" + else + export RELEASE_BRANCH=release-$(echo ${RELEASE_VERSION} | sed -E 's/^v([0-9]+)\.([0-9]+)\..*$/\1.\2/') + echo "RELEASE_BRANCH=${RELEASE_BRANCH}" >> ${GITHUB_ENV} + echo "This is not a beta or alpha release, will use release branch ${RELEASE_BRANCH}" + fi + - name: Create or checkout release branch + run: | + if git show-ref --verify --quiet "refs/remotes/origin/${RELEASE_BRANCH}"; then + echo "Branch ${RELEASE_BRANCH} already exists" + git checkout "${RELEASE_BRANCH}" + else + git checkout -b "${RELEASE_BRANCH}" + git push origin "${RELEASE_BRANCH}" + echo "Created branch ${RELEASE_BRANCH}" + fi + - name: Validate tag does not already exist + run: | + if [[ -n "$(git tag -l "${RELEASE_VERSION}")" ]]; then + echo "Tag ${RELEASE_VERSION} already exists, exiting" + exit 1 + fi + - name: Create Release Tag + run: | + git config user.name "${GITHUB_ACTOR}" + git config user.email "${GITHUB_ACTOR}@users.noreply.github.com" + git tag -a ${RELEASE_VERSION} -m ${RELEASE_VERSION} + git tag api/${RELEASE_VERSION} + git push origin ${RELEASE_VERSION} + git push origin api/${RELEASE_VERSION} + echo "Created tags ${RELEASE_VERSION} and api/${RELEASE_VERSION}" + release: + name: create draft release + runs-on: ubuntu-latest + needs: push_release_tags permissions: contents: write - - if: github.repository == 'metal3-io/baremetal-operator' steps: - - name: Export RELEASE_TAG var - run: echo "RELEASE_TAG=${GITHUB_REF:10}" >> $GITHUB_ENV + - name: Set env + run: echo "RELEASE_TAG=${RELEASE_TAG}" >> ${GITHUB_ENV} + env: + RELEASE_TAG: ${{needs.push_release_tags.outputs.release_tag}} - name: checkout code uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 with: fetch-depth: 0 - - name: Install go + ref: ${{ env.RELEASE_TAG }} + - name: Calculate go version + run: echo "go_version=$(make go-version)" >> ${GITHUB_ENV} + - name: Set up Go uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 with: - go-version: '1.22' - - name: Generate release notes + go-version: ${{ env.go_version }} + - name: generate release artifacts + run: | + make release + - name: get release notes run: | - make release-notes + curl -L "https://raw.githubusercontent.com/${{ github.repository }}/main/releasenotes/${{ env.RELEASE_TAG }}.md" \ + -o "${{ env.RELEASE_TAG }}.md" - name: Release uses: softprops/action-gh-release@01570a1f39cb168c169c802c3bceb9e93fb10974 # v2.1.0 with: draft: true files: out/* - body_path: releasenotes/${{ env.RELEASE_TAG }}.md + body_path: ${{ env.RELEASE_TAG }}.md + tag_name: ${{ env.RELEASE_TAG }}