|
| 1 | +--- |
| 2 | +name: cd |
| 3 | + |
| 4 | +on: |
| 5 | + release: |
| 6 | + types: [published] |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + dry_run: |
| 10 | + description: 'Run in dry-run mode (snapshot, no publish)' |
| 11 | + required: false |
| 12 | + default: 'true' |
| 13 | + type: choice |
| 14 | + options: |
| 15 | + - 'true' |
| 16 | + - 'false' |
| 17 | + tag: |
| 18 | + description: 'Tag to use for version extraction (optional, e.g., protoc-gen-elixir-grpc@v0.2.0)' |
| 19 | + required: false |
| 20 | + type: string |
| 21 | + |
| 22 | +permissions: |
| 23 | + contents: write |
| 24 | + packages: write |
| 25 | + id-token: write |
| 26 | + attestations: write |
| 27 | + |
| 28 | +jobs: |
| 29 | + # Run tests only for manual workflow_dispatch triggers to catch issues during testing. |
| 30 | + # For actual releases, CI has already validated the code. |
| 31 | + # TODO: Consider removing workflow_dispatch entirely once the release process is stable. |
| 32 | + test: |
| 33 | + name: Run Tests |
| 34 | + if: github.event_name == 'workflow_dispatch' |
| 35 | + runs-on: ubuntu-latest |
| 36 | + steps: |
| 37 | + - name: Checkout |
| 38 | + uses: actions/checkout@v5.0.0 |
| 39 | + |
| 40 | + - name: Set up Go |
| 41 | + uses: actions/setup-go@v6.0.0 |
| 42 | + with: |
| 43 | + go-version: "1.24" |
| 44 | + |
| 45 | + - name: Run Tests |
| 46 | + run: go test -race -v ./... |
| 47 | + |
| 48 | + - name: Run Vet |
| 49 | + run: go vet ./... |
| 50 | + |
| 51 | + goreleaser: |
| 52 | + name: Release with GoReleaser |
| 53 | + needs: test |
| 54 | + if: | |
| 55 | + always() && |
| 56 | + (needs.test.result == 'success' || needs.test.result == 'skipped') |
| 57 | + runs-on: ubuntu-latest |
| 58 | + steps: |
| 59 | + - name: Checkout |
| 60 | + uses: actions/checkout@v5.0.0 |
| 61 | + with: |
| 62 | + fetch-depth: 0 |
| 63 | + |
| 64 | + - name: Set up Go |
| 65 | + uses: actions/setup-go@v6.0.0 |
| 66 | + with: |
| 67 | + go-version: "1.24" |
| 68 | + |
| 69 | + - name: Extract Version from Tag |
| 70 | + id: version |
| 71 | + run: | |
| 72 | + # Extract version from monorepo tags like protoc-gen-elixir-grpc@v0.1.0 |
| 73 | + if [ "${{ github.event_name }}" = "release" ]; then |
| 74 | + TAG="${{ github.event.release.tag_name }}" |
| 75 | + elif [ -n "${{ github.event.inputs.tag }}" ]; then |
| 76 | + # Use tag from workflow_dispatch input if provided |
| 77 | + TAG="${{ github.event.inputs.tag }}" |
| 78 | + elif [[ "${{ github.ref }}" == refs/tags/* ]]; then |
| 79 | + # Use the ref if it's a tag |
| 80 | + TAG="${{ github.ref_name }}" |
| 81 | + else |
| 82 | + echo "ERROR: No tag provided. Please provide a tag via workflow_dispatch input." |
| 83 | + echo "Example: gh workflow run cd.yml -f dry_run=true -f tag=protoc-gen-elixir-grpc@v0.1.0" |
| 84 | + exit 1 |
| 85 | + fi |
| 86 | + |
| 87 | + # Validate tag format (must contain @ separator) |
| 88 | + if [[ ! "$TAG" =~ @ ]]; then |
| 89 | + echo "ERROR: Tag must follow pattern 'component@version', got: ${TAG}" |
| 90 | + echo "Examples: protoc-gen-elixir-grpc@v0.1.0, protoc-gen-connect-go-servicestruct@v0.2.0" |
| 91 | + exit 1 |
| 92 | + fi |
| 93 | + |
| 94 | + VERSION="${TAG##*@}" |
| 95 | + COMPONENT="${TAG%%@*}" |
| 96 | + |
| 97 | + # Validate version is not empty |
| 98 | + if [ -z "$VERSION" ]; then |
| 99 | + echo "ERROR: Failed to extract version from tag: ${TAG}" |
| 100 | + exit 1 |
| 101 | + fi |
| 102 | + |
| 103 | + # Validate component is known |
| 104 | + case "$COMPONENT" in |
| 105 | + protoc-gen-elixir-grpc|protoc-gen-connect-go-servicestruct) |
| 106 | + echo "Valid component: ${COMPONENT}" |
| 107 | + ;; |
| 108 | + *) |
| 109 | + echo "ERROR: Unknown component: ${COMPONENT}" |
| 110 | + echo "Valid components: protoc-gen-elixir-grpc, protoc-gen-connect-go-servicestruct" |
| 111 | + exit 1 |
| 112 | + ;; |
| 113 | + esac |
| 114 | + |
| 115 | + echo "version=${VERSION}" >> $GITHUB_OUTPUT |
| 116 | + echo "component=${COMPONENT}" >> $GITHUB_OUTPUT |
| 117 | + echo "Extracted component: ${COMPONENT}" |
| 118 | + echo "Extracted version: ${VERSION}" |
| 119 | +
|
| 120 | + - name: Run GoReleaser (Snapshot) |
| 121 | + if: github.event_name == 'workflow_dispatch' && github.event.inputs.dry_run == 'true' |
| 122 | + uses: goreleaser/goreleaser-action@v6 |
| 123 | + with: |
| 124 | + distribution: goreleaser |
| 125 | + version: "~> v2" |
| 126 | + args: release --snapshot --clean --skip=publish --config .github/goreleaser.yml |
| 127 | + env: |
| 128 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 129 | + GORELEASER_CURRENT_TAG: ${{ steps.version.outputs.version }} |
| 130 | + BUILD_COMPONENT: ${{ steps.version.outputs.component }} |
| 131 | + |
| 132 | + - name: Run GoReleaser (Release) |
| 133 | + if: github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.dry_run == 'false') |
| 134 | + uses: goreleaser/goreleaser-action@v6 |
| 135 | + with: |
| 136 | + distribution: goreleaser |
| 137 | + version: "~> v2" |
| 138 | + args: release --clean --config .github/goreleaser.yml |
| 139 | + env: |
| 140 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 141 | + GORELEASER_CURRENT_TAG: ${{ steps.version.outputs.version }} |
| 142 | + BUILD_COMPONENT: ${{ steps.version.outputs.component }} |
| 143 | + |
| 144 | + - name: Attest Build Provenance |
| 145 | + if: github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.dry_run == 'false') |
| 146 | + uses: actions/attest-build-provenance@v2.0.0 |
| 147 | + with: |
| 148 | + subject-path: "dist/**/*.tar.gz" |
| 149 | + |
| 150 | + - name: Upload Snapshot Artifacts |
| 151 | + if: github.event_name == 'workflow_dispatch' && github.event.inputs.dry_run == 'true' |
| 152 | + uses: actions/upload-artifact@v4 |
| 153 | + with: |
| 154 | + name: snapshot-${{ steps.version.outputs.component }}-${{ steps.version.outputs.version }} |
| 155 | + path: dist/* |
| 156 | + retention-days: 7 |
0 commit comments