Skip to content

Commit df6040b

Browse files
authored
Release to TiUP (#56)
- Release ticoud to TiUP. - Download archives from tiup mirror. - Disable command `update` when managed by tiup
1 parent b3ddcf7 commit df6040b

File tree

6 files changed

+100
-15
lines changed

6 files changed

+100
-15
lines changed

.github/workflows/release.yml

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- name: Generate changelog
2323
id: changelog
2424
run: |
25-
echo "::set-output name=tag-name::${GITHUB_REF#refs/tags/}"
25+
echo "TAG_NAME=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
2626
gh api repos/$GITHUB_REPOSITORY/releases/generate-notes \
2727
-f tag_name="${GITHUB_REF#refs/tags/}" \
2828
-f target_commitish=main \
@@ -37,4 +37,56 @@ jobs:
3737
args: release --release-notes=.CHANGELOG.md
3838
env:
3939
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
40-
GORELEASER_CURRENT_TAG: ${{steps.changelog.outputs.tag-name}}
40+
GORELEASER_CURRENT_TAG: ${{steps.changelog.outputs.TAG_NAME}}
41+
42+
- name: Set up tiup
43+
id: init_tiup
44+
run: |
45+
TMP_DIR=`mktemp -d`
46+
export TIUP_HOME="$TMP_DIR/home"
47+
echo "TIUP_HOME=$TIUP_HOME" >> $GITHUB_OUTPUT
48+
echo "TIUP_BIN=$TIUP_HOME/bin/tiup" >> $GITHUB_OUTPUT
49+
rm -rf $TIUP_HOME && mkdir -p $TIUP_HOME/{bin,keys}
50+
wget -O $TMP_DIR/tiup-linux-amd64.tar.gz -q https://tiup-mirrors.pingcap.com/tiup-linux-amd64.tar.gz
51+
tar -zxf $TMP_DIR/tiup-linux-amd64.tar.gz -C $TIUP_HOME/bin && chmod 755 $TIUP_HOME/bin/tiup
52+
curl -s https://tiup-mirrors.pingcap.com/root.json -o $TIUP_HOME/bin/root.json
53+
54+
TIUP_MIRRORS=${{ secrets.TIUP_SERVER_PROD }}
55+
$TIUP_HOME/bin/tiup mirror set ${TIUP_MIRRORS}
56+
echo ${{ secrets.TIUP_COMP_KEY_PINGCAP }} | base64 -d > $TIUP_HOME/keys/private.json
57+
58+
- name: Publish package
59+
env:
60+
TIUP_HOME: ${{ steps.init_tiup.outputs.TIUP_HOME }}
61+
TIUP_BIN: ${{ steps.init_tiup.outputs.TIUP_BIN }}
62+
REL_VER: ${{steps.changelog.outputs.TAG_NAME}}
63+
BINARY_NAME: "ticloud"
64+
COMPONENT_NAME: "cloud"
65+
COMPONENT_DESC: "CLI tool to manage TiDB Cloud"
66+
run: |
67+
matrix=(
68+
'darwin amd64'
69+
'darwin arm64'
70+
'linux amd64'
71+
'linux arm64'
72+
)
73+
74+
# skip the first letter v in the tag name
75+
export VER="${REL_VER:1}"
76+
77+
for item in "${matrix[@]}" ; do
78+
os_arch=($item)
79+
os=(${os_arch[0]})
80+
arch=(${os_arch[1]})
81+
82+
filename="dist/${BINARY_NAME}_${VER}_${os}_${arch}.tar.gz"
83+
84+
if [ ! -f "$filename" ]; then
85+
echo "File $filename does not exist"
86+
exit 1
87+
fi
88+
89+
echo "Releasing v${REL_VER} for os $os arch $arch..."
90+
91+
${TIUP_BIN} mirror publish $COMPONENT_NAME ${REL_VER} $filename $BINARY_NAME --os=$os --arch=$arch --desc="$COMPONENT_DESC"
92+
done

.goreleaser.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ archives:
3030
builds: [linux, macos]
3131
<<: &archive_defaults
3232
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
33-
replacements:
34-
darwin: macos
35-
amd64: x86_64
3633
wrap_in_directory: false
3734
format: tar.gz
3835
- id: windows
@@ -47,8 +44,6 @@ nfpms:
4744
license: Apache 2.0
4845
builds: [ linux ]
4946
formats: [ deb,rpm ]
50-
replacements:
51-
amd64: x86_64
5247

5348
checksum:
5449
name_template: 'checksums.txt'

install.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
version="0.1.4"
16+
version="0.1.5"
1717

18-
repo='https://github.com/tidbcloud/tidbcloud-cli/releases/download'
18+
repo='https://tiup-mirrors.pingcap.com/'
1919
if [ -n "$TICLOUD_MIRRORS" ]; then
2020
repo=$TICLOUD_MIRRORS
2121
fi
2222

2323
case $(uname -s) in
2424
Linux|linux) os=linux ;;
25-
Darwin|darwin) os=macos ;;
25+
Darwin|darwin) os=darwin ;;
2626
*) os= ;;
2727
esac
2828

@@ -32,7 +32,7 @@ if [ -z "$os" ]; then
3232
fi
3333

3434
case $(uname -m) in
35-
amd64|x86_64) arch=x86_64 ;;
35+
amd64|x86_64) arch=amd64 ;;
3636
arm64|aarch64) arch=arm64 ;;
3737
*) arch= ;;
3838
esac
@@ -49,9 +49,9 @@ bin_dir=$TICLOUD_HOME/bin
4949
mkdir -p "$bin_dir"
5050

5151
install_binary() {
52-
curl -L "$repo/v${version}/ticloud_${version}_${os}_$arch.tar.gz" -o "/tmp/ticloud_${version}_${os}_$arch.tar.gz" || return 1
53-
tar -zxf "/tmp/ticloud_${version}_${os}_$arch.tar.gz" -C "$bin_dir" || return 1
54-
rm "/tmp/ticloud_${version}_${os}_$arch.tar.gz"
52+
curl -L "$repo/ticloud-v${version}-${os}-$arch.tar.gz" -o "/tmp/ticloud-v${version}-${os}-$arch.tar.gz" || return 1
53+
tar -zxf "/tmp/ticloud-v${version}-${os}-$arch.tar.gz" -C "$bin_dir" || return 1
54+
rm "/tmp/ticloud-v${version}-${os}-$arch.tar.gz"
5555
return 0
5656
}
5757

internal/cli/root.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,17 @@ func RootCmd(h *internal.Helper, ver, commit, buildDate string) *cobra.Command {
124124
color.YellowString("A new version of %s is available:", config.CliName),
125125
color.CyanString(ver),
126126
color.CyanString(newRelease.Version)))
127-
fmt.Fprintln(h.IOStreams.Out, color.GreenString("Use `ticloud update` to update to the latest version"))
127+
128+
var binpath string
129+
if exepath, err := os.Executable(); err == nil {
130+
binpath = exepath
131+
}
132+
133+
if util.IsUnderTiUP(binpath) {
134+
fmt.Fprintln(h.IOStreams.Out, color.GreenString("Use `tiup update cloud` to update to the latest version"))
135+
} else {
136+
fmt.Fprintln(h.IOStreams.Out, color.GreenString("Use `ticloud update` to update to the latest version"))
137+
}
128138
}
129139
}
130140
},

internal/cli/update/update.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ import (
1818
"bytes"
1919
"context"
2020
"fmt"
21+
"os"
2122
"os/exec"
2223
"time"
2324

2425
"tidbcloud-cli/internal"
2526
"tidbcloud-cli/internal/config"
2627
"tidbcloud-cli/internal/service/github"
2728
"tidbcloud-cli/internal/ui"
29+
"tidbcloud-cli/internal/util"
2830

2931
tea "github.com/charmbracelet/bubbletea"
3032
"github.com/fatih/color"
@@ -37,6 +39,15 @@ func UpdateCmd(h *internal.Helper, ver string) *cobra.Command {
3739
Use: "update",
3840
Short: "Update the CLI to the latest version",
3941
RunE: func(cmd *cobra.Command, args []string) error {
42+
var binpath string
43+
if exepath, err := os.Executable(); err == nil {
44+
binpath = exepath
45+
}
46+
// If is managed by TiUP, we should disable the update command since binpath is different.
47+
if util.IsUnderTiUP(binpath) {
48+
return errors.New("the CLI is managed by TiUP, please update it by `tiup update cloud`")
49+
}
50+
4051
// When update CLI, we don't need to check the version again after command executes.
4152
newRelease, err := github.CheckForUpdate(config.Repo, ver, false)
4253
// FIXME: Since github API has rate limit, we should not return error when check update failed.

internal/util/search.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
package util
1616

17+
import "strings"
18+
19+
const tiupBinPrefix = "/.tiup/components/"
20+
1721
func StringInSlice(a []string, x string) bool {
1822
for _, b := range a {
1923
if b == x {
@@ -22,3 +26,16 @@ func StringInSlice(a []string, x string) bool {
2226
}
2327
return false
2428
}
29+
30+
// IsUnderTiUP checks whether the given binary is under the TiUP path.
31+
func IsUnderTiUP(binpath string) bool {
32+
if binpath == "" {
33+
return false
34+
}
35+
36+
if strings.Contains(binpath, tiupBinPrefix) {
37+
return true
38+
}
39+
40+
return false
41+
}

0 commit comments

Comments
 (0)