|
| 1 | +name: Component Release |
| 2 | + |
| 3 | +permissions: |
| 4 | + contents: write |
| 5 | + |
| 6 | +on: |
| 7 | + pull_request: |
| 8 | + types: [closed] |
| 9 | + branches: |
| 10 | + - develop |
| 11 | + |
| 12 | +jobs: |
| 13 | + release: |
| 14 | + if: github.event.pull_request.merged == true |
| 15 | + runs-on: ubuntu-latest |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout repository |
| 19 | + uses: actions/checkout@v3 |
| 20 | + |
| 21 | + - name: Set up Git |
| 22 | + run: | |
| 23 | + git config --global user.name "GitHub Actions" |
| 24 | + git config --global user.email "[email protected]" |
| 25 | +
|
| 26 | + - name: Install git-flow and auto-changelog |
| 27 | + run: | |
| 28 | + sudo apt-get update |
| 29 | + sudo apt-get install -y git-flow |
| 30 | + npm install -g auto-changelog |
| 31 | +
|
| 32 | + - name: Clone the project and start release |
| 33 | + run: | |
| 34 | + set -e |
| 35 | + git clone https://x-access-token:${{ secrets.RDKCM_RDKE }}@github.com/${{ github.repository }} project |
| 36 | + cd project |
| 37 | + git fetch --all |
| 38 | + git checkout main || git checkout -b main origin/main |
| 39 | + git checkout develop || git checkout -b develop origin/develop |
| 40 | +
|
| 41 | + git config gitflow.branch.master main |
| 42 | + git config gitflow.branch.develop develop |
| 43 | + git config gitflow.prefix.feature feature/ |
| 44 | + git config gitflow.prefix.bugfix bugfix/ |
| 45 | + git config gitflow.prefix.release release/ |
| 46 | + git config gitflow.prefix.hotfix hotfix/ |
| 47 | + git config gitflow.prefix.support support/ |
| 48 | + git config gitflow.prefix.versiontag '' |
| 49 | +
|
| 50 | + echo "git config completed" |
| 51 | + # Extract version from PR description |
| 52 | + PR_DESC="${{ github.event.pull_request.body }}" |
| 53 | + # Get top tag from CHANGELOG.md |
| 54 | + TOP_TAG=$(grep -m 1 -oP '^#### \[\K[^\]]+' CHANGELOG.md) |
| 55 | + if [[ -z "$TOP_TAG" ]]; then |
| 56 | + echo "No version found in CHANGELOG.md!" |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | + # Validate TOP_TAG format (semantic versioning: major.minor.patch) |
| 60 | + if [[ ! "$TOP_TAG" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 61 | + echo "Invalid version format in CHANGELOG.md: $TOP_TAG. Expected format: major.minor.patch" |
| 62 | + exit 1 |
| 63 | + fi |
| 64 | + IFS='.' read -r major minor patch <<< "$TOP_TAG" |
| 65 | + VERSION_TYPE=$(echo "$PR_DESC" | grep -oiP 'version\s*:\s*\K(major|minor|patch)' | tr '[:upper:]' '[:lower:]') |
| 66 | + if [[ -z "$VERSION_TYPE" ]]; then |
| 67 | + echo "No version type found in PR description, defaulting to PATCH increment." |
| 68 | + patch=$((patch + 1)) |
| 69 | + elif [[ "$VERSION_TYPE" == "major" ]]; then |
| 70 | + major=$((major + 1)) |
| 71 | + minor=0 |
| 72 | + patch=0 |
| 73 | + elif [[ "$VERSION_TYPE" == "minor" ]]; then |
| 74 | + minor=$((minor + 1)) |
| 75 | + patch=0 |
| 76 | + elif [[ "$VERSION_TYPE" == "patch" ]]; then |
| 77 | + patch=$((patch + 1)) |
| 78 | + else |
| 79 | + echo "Invalid version type in PR description: $VERSION_TYPE" |
| 80 | + exit 1 |
| 81 | + fi |
| 82 | + RELEASE_VERSION="$major.$minor.$patch" |
| 83 | + echo "Using calculated version: $RELEASE_VERSION" |
| 84 | + echo "RELEASE_VERSION=$RELEASE_VERSION" |
| 85 | + echo "RELEASE_VERSION=$RELEASE_VERSION" >> $GITHUB_ENV |
| 86 | + # Check if tag already exists |
| 87 | + if git rev-parse "refs/tags/$RELEASE_VERSION" >/dev/null 2>&1; then |
| 88 | + echo "Tag $RELEASE_VERSION already exists. Skipping release." |
| 89 | + exit 0 |
| 90 | + fi |
| 91 | + git flow release start $RELEASE_VERSION |
| 92 | + auto-changelog -v $RELEASE_VERSION |
| 93 | + git add CHANGELOG.md |
| 94 | + git commit -m "$RELEASE_VERSION release changelog updates" |
| 95 | + git flow release publish |
| 96 | +
|
| 97 | + - name: Finish release and push (default git-flow messages) |
| 98 | + run: | |
| 99 | + set -e |
| 100 | + cd project |
| 101 | + git flow release finish -m "$RELEASE_VERSION release" $RELEASE_VERSION |
| 102 | + git push origin main |
| 103 | + git push origin --tags |
| 104 | + git push origin develop |
| 105 | + |
| 106 | + - name: Cleanup tag if workflow fails |
| 107 | + if: failure() |
| 108 | + run: | |
| 109 | + cd project |
| 110 | + git tag -d $RELEASE_VERSION || true |
| 111 | + git push origin :refs/tags/$RELEASE_VERSION || true |
0 commit comments