refactor for speed on semantic version checking #12
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: Build and Publish | |
| on: | |
| push: | |
| branches: [main] | |
| tags: | |
| - 'v*' | |
| pull_request: | |
| branches: [main] | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '22' | |
| cache: 'npm' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Run type check | |
| run: npm run check | |
| - name: Build | |
| run: npm run build | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to GitHub Container Registry | |
| if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')) | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Login to Docker Hub | |
| if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')) | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Get package version, git tag, and short SHA | |
| id: version-info | |
| run: | | |
| echo "version=$(node -p "require('./package.json').version")" >> $GITHUB_OUTPUT | |
| echo "sha_short=${GITHUB_SHA::7}" >> $GITHUB_OUTPUT | |
| # Extract version tag if this is a version tag push (e.g., v1.0.0 -> 1.0.0) | |
| if [[ "${{ github.ref }}" == refs/tags/v* ]]; then | |
| git_tag="${GITHUB_REF#refs/tags/}" | |
| # Remove 'v' prefix if present (v1.0.0 -> 1.0.0) | |
| version_tag="${git_tag#v}" | |
| echo "version_tag=${version_tag}" >> $GITHUB_OUTPUT | |
| echo "has_version_tag=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "version_tag=" >> $GITHUB_OUTPUT | |
| echo "has_version_tag=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Build and push Docker image (multi-arch) - regular push | |
| if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| platforms: linux/amd64,linux/arm64 | |
| push: true | |
| tags: | | |
| ghcr.io/${{ github.repository }}:latest | |
| ghcr.io/${{ github.repository }}:${{ steps.version-info.outputs.version }} | |
| ghcr.io/${{ github.repository }}:${{ steps.version-info.outputs.sha_short }} | |
| ${{ secrets.DOCKERHUB_USERNAME }}/pocker:latest | |
| ${{ secrets.DOCKERHUB_USERNAME }}/pocker:${{ steps.version-info.outputs.version }} | |
| ${{ secrets.DOCKERHUB_USERNAME }}/pocker:${{ steps.version-info.outputs.sha_short }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| - name: Build and push Docker image (multi-arch) - version tag | |
| if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| platforms: linux/amd64,linux/arm64 | |
| push: true | |
| tags: | | |
| ghcr.io/${{ github.repository }}:latest | |
| ghcr.io/${{ github.repository }}:${{ steps.version-info.outputs.version }} | |
| ghcr.io/${{ github.repository }}:${{ steps.version-info.outputs.version_tag }} | |
| ghcr.io/${{ github.repository }}:${{ steps.version-info.outputs.sha_short }} | |
| ${{ secrets.DOCKERHUB_USERNAME }}/pocker:latest | |
| ${{ secrets.DOCKERHUB_USERNAME }}/pocker:${{ steps.version-info.outputs.version }} | |
| ${{ secrets.DOCKERHUB_USERNAME }}/pocker:${{ steps.version-info.outputs.version_tag }} | |
| ${{ secrets.DOCKERHUB_USERNAME }}/pocker:${{ steps.version-info.outputs.sha_short }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |