|
13 | 13 | - minor |
14 | 14 | - major |
15 | 15 | npm_tag: |
16 | | - description: 'Custom npm dist-tag (ALWAYS specify for non-standard branches to avoid overwriting latest)' |
| 16 | + description: 'Custom npm dist-tag (ALWAYS specify for non-standard branches)' |
17 | 17 | required: false |
18 | 18 | default: 'dev' |
19 | 19 | type: string |
@@ -81,144 +81,64 @@ jobs: |
81 | 81 | - name: Run build |
82 | 82 | run: npm run build |
83 | 83 |
|
84 | | - publish: |
85 | | - name: Publish |
| 84 | + determine-version: |
| 85 | + name: Determine Version Bump |
86 | 86 | runs-on: ubuntu-24.04 |
87 | 87 | needs: [build] |
88 | | - environment: production |
89 | | - |
| 88 | + outputs: |
| 89 | + version_bump: ${{ steps.set-bump-type.outputs.version_bump }} |
| 90 | + npm_tag: ${{ steps.set-npm-tag.outputs.npm_tag }} |
| 91 | + |
90 | 92 | steps: |
91 | 93 | - name: Check out code |
92 | 94 | uses: actions/checkout@v4 |
93 | 95 | with: |
94 | 96 | fetch-depth: 2 |
95 | | - ref: ${{ github.ref }} |
96 | | - |
97 | | - - name: Restore dependencies |
98 | | - uses: actions/cache/restore@v4 |
99 | | - with: |
100 | | - enableCrossOsArchive: true |
101 | | - fail-on-cache-miss: true |
102 | | - key: npm-install-${{ runner.os }}-${{ hashFiles('package-lock.json') }} |
103 | | - path: node_modules |
104 | | - |
105 | | - - name: Restore build |
106 | | - uses: actions/cache/restore@v4 |
107 | | - with: |
108 | | - enableCrossOsArchive: true |
109 | | - fail-on-cache-miss: true |
110 | | - key: npm-build-${{ runner.os }}-${{ github.sha }} |
111 | | - path: | |
112 | | - .server |
113 | | - .public |
114 | | -
|
115 | | - - name: Setup Node.js |
116 | | - uses: actions/setup-node@v4 |
117 | | - with: |
118 | | - node-version-file: .nvmrc |
119 | | - registry-url: https://registry.npmjs.org |
120 | | - scope: '@defra' |
121 | | - |
| 97 | + |
122 | 98 | - name: Determine version bump type |
123 | | - id: version-type |
| 99 | + id: set-bump-type |
124 | 100 | run: | |
125 | | - BRANCH_NAME=${GITHUB_REF#refs/heads/} |
| 101 | + # Default to patch |
| 102 | + VERSION_BUMP="patch" |
126 | 103 | |
127 | | - # Check if this is a manual trigger with inputs |
| 104 | + # For manual workflow |
128 | 105 | if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then |
129 | | - VERSION_TYPE="${{ github.event.inputs.version_bump }}" |
130 | | - echo "Manual workflow run with version bump: $VERSION_TYPE" |
131 | | - |
132 | | - # If a custom npm tag was provided, use it |
133 | | - if [[ -n "${{ github.event.inputs.npm_tag }}" ]]; then |
134 | | - CUSTOM_NPM_TAG="${{ github.event.inputs.npm_tag }}" |
135 | | - echo "Using custom npm tag: $CUSTOM_NPM_TAG" |
136 | | - echo "CUSTOM_NPM_TAG=$CUSTOM_NPM_TAG" >> $GITHUB_OUTPUT |
137 | | - fi |
| 106 | + VERSION_BUMP="${{ github.event.inputs.version_bump }}" |
| 107 | + echo "Using manual input for version bump: $VERSION_BUMP" |
| 108 | + # For automatic trigger |
138 | 109 | else |
139 | | - # Automatic trigger via push - use commit message logic |
140 | 110 | COMMIT_MSG=$(git log -1 --pretty=%B) |
141 | | - VERSION_TYPE="patch" |
142 | | - |
143 | | - # Check for MINOR or MAJOR in commit message |
144 | 111 | if [[ "$COMMIT_MSG" == *"MINOR"* ]]; then |
145 | | - VERSION_TYPE="minor" |
| 112 | + VERSION_BUMP="minor" |
| 113 | + echo "Found MINOR keyword in commit" |
146 | 114 | elif [[ "$COMMIT_MSG" == *"MAJOR"* ]]; then |
147 | | - VERSION_TYPE="major" |
| 115 | + VERSION_BUMP="major" |
| 116 | + echo "Found MAJOR keyword in commit" |
| 117 | + else |
| 118 | + echo "No version keywords found, using patch" |
148 | 119 | fi |
149 | 120 | fi |
150 | 121 | |
151 | | - # For release branches, respect the branch naming convention |
152 | | - if [[ "$BRANCH_NAME" =~ release/v([0-9]+) ]]; then |
153 | | - # Extract just the major version number |
154 | | - MAJOR_VERSION="${BASH_REMATCH[1]}" |
155 | | - |
156 | | - # Set the package version to match the major version if needed |
157 | | - CURRENT_VERSION=$(npm pkg get version | tr -d \") |
158 | | - CURRENT_MAJOR=$(echo $CURRENT_VERSION | cut -d. -f1) |
159 | | - |
160 | | - # If the major version doesn't match, reset to major.0.0 |
161 | | - if [[ "$CURRENT_MAJOR" != "$MAJOR_VERSION" ]]; then |
162 | | - npm version $MAJOR_VERSION.0.0 --git-tag-version false --allow-same-version |
163 | | - |
164 | | - # Override to patch since we've already set the version |
165 | | - VERSION_TYPE="patch" |
166 | | - fi |
167 | | - fi |
168 | | - |
169 | | - echo "VERSION_TYPE=$VERSION_TYPE" >> $GITHUB_OUTPUT |
170 | | - echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_OUTPUT |
171 | | -
|
172 | | - - name: Update package version |
173 | | - run: npm version ${{ steps.version-type.outputs.VERSION_TYPE }} --git-tag-version false --save |
174 | | - |
175 | | - - name: Commit and push updates |
176 | | - run: | |
177 | | - git config user.name github-actions |
178 | | - git config user.email github-actions@github.com |
179 | | - NEW_VERSION=$(npm pkg get version | tr -d \") |
180 | | - git commit -am "v$NEW_VERSION [skip ci]" && git push |
181 | | -
|
182 | | - - name: Publish to npm with appropriate dist-tag |
| 122 | + echo "version_bump=$VERSION_BUMP" >> $GITHUB_OUTPUT |
| 123 | + |
| 124 | + - name: Determine npm tag |
| 125 | + id: set-npm-tag |
183 | 126 | run: | |
184 | | - BRANCH_NAME="${{ steps.version-type.outputs.BRANCH_NAME }}" |
185 | | - NEW_VERSION=$(npm pkg get version | tr -d \") |
186 | | - PUBLISH_ARGS="--access public" |
187 | | - |
188 | | - # First priority: Check for custom tag from version-type step |
189 | | - if [[ -n "${{ steps.version-type.outputs.CUSTOM_NPM_TAG }}" ]]; then |
190 | | - DIST_TAG="${{ steps.version-type.outputs.CUSTOM_NPM_TAG }}" |
191 | | - PUBLISH_ARGS="$PUBLISH_ARGS --tag $DIST_TAG" |
192 | | - echo "Publishing v$NEW_VERSION with custom tag '$DIST_TAG'" |
193 | | - # Second priority: Check for branch-specific tags |
194 | | - elif [[ "$BRANCH_NAME" == "main" ]]; then |
195 | | - echo "Publishing v$NEW_VERSION from main -> using default 'latest' tag" |
196 | | - elif [[ "$BRANCH_NAME" =~ release/v([0-9]+) ]]; then |
197 | | - MAJOR_VERSION="${BASH_REMATCH[1]}" |
198 | | - DIST_TAG="v${MAJOR_VERSION}" |
199 | | - PUBLISH_ARGS="$PUBLISH_ARGS --tag $DIST_TAG" |
200 | | - echo "Publishing v$NEW_VERSION from $BRANCH_NAME -> using tag '$DIST_TAG'" |
201 | | - else |
202 | | - # Safety check for non-standard branches |
203 | | - if [[ "${{ github.event_name }}" == "workflow_dispatch" && -z "${{ github.event.inputs.npm_tag }}" ]]; then |
204 | | - echo "⚠️ WARNING: Publishing from non-standard branch '$BRANCH_NAME' without a custom npm tag" |
205 | | - echo "⚠️ This will publish as 'latest' and may overwrite your production release" |
206 | | - fi |
207 | | - echo "Branch $BRANCH_NAME doesn't match expected patterns, using default publishing" |
| 127 | + # For manual workflow |
| 128 | + if [[ "${{ github.event_name }}" == "workflow_dispatch" && -n "${{ github.event.inputs.npm_tag }}" ]]; then |
| 129 | + NPM_TAG="${{ github.event.inputs.npm_tag }}" |
| 130 | + echo "Using manual input for npm tag: $NPM_TAG" |
| 131 | + echo "npm_tag=$NPM_TAG" >> $GITHUB_OUTPUT |
208 | 132 | fi |
209 | | - |
210 | | - # Add dry-run flag if specified |
211 | | - if [[ "${{ github.event.inputs.dry_run }}" == "true" ]]; then |
212 | | - PUBLISH_ARGS="$PUBLISH_ARGS --dry-run" |
213 | | - echo "DRY RUN MODE - No actual publishing will occur" |
214 | | - fi |
215 | | - |
216 | | - # TODO: REMOVE THIS LINE BEFORE MERGING TO MAIN |
217 | | - # Temporary safety measure for testing |
218 | | - PUBLISH_ARGS="$PUBLISH_ARGS --dry-run" |
219 | | - echo "⚠️ TEST MODE: Force using --dry-run flag. Remove before merging to main! ⚠️" |
220 | | - |
221 | | - # Execute npm publish with all arguments |
222 | | - npm publish $PUBLISH_ARGS |
223 | | - env: |
224 | | - NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }} |
| 133 | + |
| 134 | + publish: |
| 135 | + name: Publish Package |
| 136 | + needs: [determine-version] |
| 137 | + uses: ./.github/workflows/publish-core.yml |
| 138 | + with: |
| 139 | + version_bump: ${{ needs.determine-version.outputs.version_bump }} |
| 140 | + npm_tag: ${{ needs.determine-version.outputs.npm_tag }} |
| 141 | + dry_run: ${{ github.event.inputs.dry_run || false }} |
| 142 | + branch_name: ${{ github.ref_name }} |
| 143 | + secrets: |
| 144 | + NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }} |
0 commit comments