Publishing #4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Publishing | |
| on: | |
| push: | |
| branches: | |
| - main | |
| workflow_dispatch: | |
| inputs: | |
| source_branch: | |
| description: 'Source branch' | |
| required: true | |
| default: 'main' | |
| permissions: | |
| contents: write | |
| packages: write | |
| jobs: | |
| publish: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v3 | |
| with: | |
| ref: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.source_branch || github.ref }} | |
| submodules: recursive | |
| - name: Check if should be published | |
| if: ${{ github.event_name != 'workflow_dispatch' && !contains(github.event.head_commit.message, '[pub]') }} | |
| run: | | |
| echo "[pub] string is missing in commit message, skipping ..." | |
| exit 0 | |
| - name: Publish | |
| id: pub | |
| if: ${{ github.event_name == 'workflow_dispatch' || contains(github.event.head_commit.message, '[pub]') }} | |
| run: | | |
| CGO_ENABLED=0 \ | |
| go build -trimpath -ldflags="-s -w" -o main | |
| if ldd main 2>&1 | grep -vq "not a dynamic executable"; then | |
| echo "ERROR: binary is dynamically linked" | |
| ldd main || true | |
| exit 1 | |
| fi | |
| CGO_ENABLED=0 GOOS=windows GOARCH=amd64 \ | |
| go build -trimpath -ldflags="-s -w" -o main.exe | |
| echo "exists=true" >> $GITHUB_OUTPUT | |
| - name: Create tag | |
| id: newtag | |
| if: steps.pub.outputs.exists == 'true' | |
| run: | | |
| echo "::group::Version overview" | |
| VERSION=$(awk '/^## /{print $2; exit}' CHANGELOG.md) | |
| BODY=$(awk '/^## /{if (seen++) exit} seen' CHANGELOG.md | tail -n +2) | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| echo "body<<EOF" >> $GITHUB_OUTPUT | |
| echo "$BODY" >> $GITHUB_OUTPUT | |
| echo "EOF" >> $GITHUB_OUTPUT | |
| echo "::endgroup::" | |
| TAG="$VERSION" | |
| echo "::group::New tag created" | |
| echo "tag=$TAG" >> $GITHUB_OUTPUT | |
| git tag $TAG | |
| git push origin $TAG || true | |
| echo "::endgroup::" | |
| - name: Create release | |
| id: crelease | |
| if: steps.pub.outputs.exists == 'true' | |
| uses: actions/create-release@v1 | |
| with: | |
| tag_name: ${{ steps.newtag.outputs.version }} | |
| release_name: ${{ steps.newtag.outputs.version }} | |
| body: ${{ steps.newtag.outputs.body }} | |
| env: | |
| GITHUB_TOKEN: ${{ secrets._TOKEN }} | |
| - name: Upload asset | |
| if: steps.pub.outputs.exists == 'true' | |
| uses: actions/upload-release-asset@v1 | |
| env: | |
| GITHUB_TOKEN: ${{ secrets._TOKEN }} | |
| with: | |
| upload_url: ${{ steps.crelease.outputs.upload_url }} | |
| asset_path: ./main | |
| asset_name: main | |
| asset_content_type: application/octet-stream | |
| - name: Upload asset Win | |
| if: steps.pub.outputs.exists == 'true' | |
| uses: actions/upload-release-asset@v1 | |
| env: | |
| GITHUB_TOKEN: ${{ secrets._TOKEN }} | |
| with: | |
| upload_url: ${{ steps.crelease.outputs.upload_url }} | |
| asset_path: ./main.exe | |
| asset_name: main.exe | |
| asset_content_type: application/octet-stream | |
| - name: Set up Docker Buildx | |
| if: steps.pub.outputs.exists == 'true' | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to GitHub Container Registry | |
| if: steps.pub.outputs.exists == 'true' | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Normalize repository owner | |
| if: steps.pub.outputs.exists == 'true' | |
| id: normalize | |
| run: | | |
| echo "owner_lc=${GITHUB_REPOSITORY_OWNER,,}" >> "$GITHUB_OUTPUT" | |
| - name: Build and push Docker image | |
| if: steps.pub.outputs.exists == 'true' | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| push: true | |
| platforms: linux/amd64 | |
| tags: | | |
| ghcr.io/${{ steps.normalize.outputs.owner_lc }}/MiniHTTPServer:latest | |
| ghcr.io/${{ steps.normalize.outputs.owner_lc }}/MiniHTTPServer:${{ steps.newtag.outputs.version }} | |
| build-args: | | |
| PACKAGE_TAG=${{ steps.newtag.outputs.version }} |