|
| 1 | +name: Build React Application |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + newVersion: |
| 7 | + description: 'new version?' |
| 8 | + required: true |
| 9 | + type: string |
| 10 | + version: |
| 11 | + description: 'The version of the file to build and push' |
| 12 | + required: true |
| 13 | + type: string |
| 14 | + tag: |
| 15 | + description: 'The tag of the image to build and push' |
| 16 | + required: true |
| 17 | + type: string |
| 18 | + channel: |
| 19 | + description: 'The channel of the image to build and push' |
| 20 | + required: true |
| 21 | + type: string |
| 22 | +jobs: |
| 23 | + Build: |
| 24 | + runs-on: ubuntu-latest |
| 25 | + permissions: |
| 26 | + id-token: write |
| 27 | + contents: write |
| 28 | + steps: |
| 29 | + - name: Checkout Repo |
| 30 | + uses: actions/checkout@v4 |
| 31 | + - name: Use Node 20 |
| 32 | + uses: actions/setup-node@v4 |
| 33 | + with: |
| 34 | + cache: npm |
| 35 | + node-version: '20' |
| 36 | + registry-url: 'https://npm.pkg.github.com' |
| 37 | + - name: Set package.json version |
| 38 | + run: | |
| 39 | + if [ '${{ inputs.newVersion }}' = 'true' ]; then |
| 40 | + echo "Setting package.json version to ${{ inputs.version }}" |
| 41 | + npm version ${{ inputs.version }} --no-git-tag-version |
| 42 | + else |
| 43 | + echo "Not setting package.json version" |
| 44 | + fi |
| 45 | + - name: Install Packages |
| 46 | + run: | |
| 47 | + npm ci |
| 48 | + - name: Build App |
| 49 | + id: build |
| 50 | + run: | |
| 51 | + # Enable pipefail to ensure the step fails if npm build fails |
| 52 | + set -o pipefail |
| 53 | +
|
| 54 | + echo "## Build Output" >> $GITHUB_STEP_SUMMARY |
| 55 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 56 | + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY |
| 57 | +
|
| 58 | + # Run build and capture output, but handle failure gracefully for summary |
| 59 | + if npm run build 2>&1 | tee build_output.log; then |
| 60 | + echo "Build completed successfully" |
| 61 | + else |
| 62 | + echo "Build failed - capturing output for summary" |
| 63 | + fi |
| 64 | +
|
| 65 | + # Always add the build output to summary (success or failure) |
| 66 | + sed 's/\x1b\[[0-9;]*m//g' build_output.log >> $GITHUB_STEP_SUMMARY |
| 67 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 68 | + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY |
| 69 | +
|
| 70 | + # Check if build actually failed and exit with error code |
| 71 | + if [ ! -d "dist" ]; then |
| 72 | + echo "Build failed - dist directory not created" |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | + - name: Clean up after failed build |
| 76 | + if: failure() && steps.build.conclusion == 'failure' |
| 77 | + run: | |
| 78 | + git push origin --delete ${{ inputs.tag }} |
| 79 | +
|
| 80 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 81 | + echo "---" >> $GITHUB_STEP_SUMMARY |
| 82 | + echo "## Build Result" >> $GITHUB_STEP_SUMMARY |
| 83 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 84 | + echo "❌ **BUILD FAILED**" >> $GITHUB_STEP_SUMMARY |
| 85 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 86 | + echo "The build output above shows the specific errors that caused the build to fail." >> $GITHUB_STEP_SUMMARY |
| 87 | + - name: Archive Output |
| 88 | + if: success() |
| 89 | + id: package |
| 90 | + run: | |
| 91 | + cd dist |
| 92 | + branch=${GITHUB_REF#refs/heads/} |
| 93 | + repo_name=${GITHUB_REPOSITORY#*/} |
| 94 | +
|
| 95 | + if [ '${{ inputs.newVersion }}' = 'true' ]; then |
| 96 | + filename=${repo_name}-${{ inputs.tag }}.zip |
| 97 | + else |
| 98 | + filename=${repo_name}-${branch//\//-}.zip |
| 99 | + fi |
| 100 | +
|
| 101 | + zip -r ../$filename . |
| 102 | + echo "filename=${filename}" >> $GITHUB_OUTPUT |
| 103 | + - name: Get release notes |
| 104 | + if: success() && inputs.newVersion == 'true' |
| 105 | + uses: actions/download-artifact@v4 |
| 106 | + with: |
| 107 | + name: change-log |
| 108 | + - name: Create Release |
| 109 | + id: create_release |
| 110 | + if: success() && inputs.newVersion == 'true' |
| 111 | + uses: ncipollo/release-action@v1 |
| 112 | + with: |
| 113 | + artifacts: './${{ steps.package.outputs.filename }}' |
| 114 | + bodyFile: ./CHANGELOG.md |
| 115 | + prerelease: ${{ inputs.channel != '' }} |
| 116 | + tag: ${{ inputs.tag }} |
| 117 | + commit: ${{ github.sha }} |
| 118 | + allowUpdates: true |
0 commit comments