Skip to content

Commit a7fb946

Browse files
authored
feat(core): add Dockerfile and instructions (#789)
1 parent b05dc53 commit a7fb946

File tree

5 files changed

+143
-15
lines changed

5 files changed

+143
-15
lines changed

.github/workflows/unit-tests.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,15 @@ jobs:
4141
fetch-depth: 1
4242
- name: Build binaries
4343
run: ./scripts/build.sh
44+
45+
docker-tests:
46+
runs-on: ubuntu-latest
47+
steps:
48+
- name: Checkout
49+
uses: actions/checkout@v2
50+
with:
51+
fetch-depth: 1
52+
- name: Build image in Docker
53+
run: docker build -t scaleway/cli .
54+
- name: Test help command in docker
55+
run: docker run -i scaleway/cli help

Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
FROM golang:1.14-alpine as builder
2+
3+
ENV BUILD_IN_DOCKER true
4+
5+
# ca-certificates is needed to add the certificates on the next image
6+
# since it's FROM scratch, it does not have any certificates
7+
# bash is needed to run the build script
8+
RUN apk update && apk add --no-cache bash git ca-certificates && update-ca-certificates
9+
10+
WORKDIR /go/src/github.com/scaleway/scaleway-cli
11+
12+
COPY go.mod go.mod
13+
COPY go.sum go.sum
14+
RUN go mod download
15+
16+
COPY scripts/ scripts/
17+
COPY cmd/ cmd/
18+
COPY internal/ internal/
19+
COPY .git/ .git/
20+
21+
RUN ./scripts/build.sh
22+
23+
FROM scratch
24+
WORKDIR /
25+
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
26+
COPY --from=builder /go/src/github.com/scaleway/scaleway-cli/scw .
27+
ENTRYPOINT ["/scw"]

README.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,32 @@ Then, run the installation and remove the `.deb` file:
112112
```bash
113113
dpkg -i /tmp/scw.deb && rm -f /tmp/scw.deb
114114
```
115+
-->
115116

117+
<!-- TODO:
116118
## With a Docker Image
117119
120+
### Official releases (Coming soon..)
121+
118122
For each release, we deliver a tagged image on the [Scaleway Docker Hub](https://hub.docker.com/r/scaleway/cli/tags) so can run `scw` in a sandboxed way: _Coming soon..._
119123
120124
```sh
121125
docker run scaleway/cli version
122126
```
123127
-->
124128

125-
## Build Locally
129+
# Examples
130+
131+
## Create an instance server
132+
```
133+
scw instance server create type=DEV1-S image=ubuntu-bionic zone=fr-par-1 tags.0="scw-cli"
134+
```
135+
136+
TODO: Add more examples here.
137+
138+
## Build it yourself
139+
140+
### Build Locally
126141

127142
If you have a >= Go 1.13 environment, you can install the `HEAD` version to test the latest features or to [contribute](CONTRIBUTING.md).
128143
Note that this development version could include bugs, use [tagged releases](https://github.com/scaleway/scaleway-cli/releases/latest) if you need stability.
@@ -133,14 +148,19 @@ go get github.com/scaleway/scaleway-cli/cmd/scw
133148

134149
Dependencies: We only use go [Go Modules](https://github.com/golang/go/wiki/Modules) with vendoring.
135150

136-
# Examples
151+
### Build with Docker
137152

138-
## Create an instance server
153+
You can build the `scw` CLI with Docker. If you have Docker installed, you can run:
154+
```sh
155+
docker build -t scaleway/cli .
139156
```
140-
scw instance server create type=DEV1-S image=ubuntu-bionic zone=fr-par-1 tags.0="scw-cli"
157+
158+
Once build, you can then use the CLI as you would run any image:
159+
```sh
160+
docker run -i --rm scaleway/cli
141161
```
142162

143-
TODO: Add more examples here.
163+
See more in-depth information about running the CLI in Docker [here](./docs/docker.md)
144164

145165
# Tutorials
146166

docs/docker.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Using the CLI with Docker
2+
3+
The CLI can be used in Docker without any problem.
4+
5+
## Basic usage
6+
7+
In order to use it you must mount the scaleway configuration file:
8+
```sh
9+
docker run -it --rm -v $HOME/.config/scw/config.yaml:/.config/scw/config.yaml:ro scaleway/cli
10+
```
11+
12+
If you want to use `scw` instead of `docker run` you can add the following in your `~/.bashrc`:
13+
```bash
14+
scw() {
15+
docker run -it --rm -v $HOME/.config/scw/config.yaml:/.config/scw/config.yaml:ro scaleway/cli "$@"
16+
}
17+
export -f scw
18+
```
19+
20+
Or if you use ZSH, add the following in your `~/.zshrc`:
21+
```zsh
22+
scw() {
23+
docker run -it --rm -v $HOME/.config/scw/config.yaml:/.config/scw/config.yaml:ro scaleway/cli $@
24+
}
25+
```
26+
27+
## Autocompletion
28+
29+
You can still use autocompletion while running the CLI in Docker, you just need to modify script generated by the CLI.
30+
For instance, `scw autocomplete script shell=bash` will return:
31+
```bash
32+
_scw() {
33+
_get_comp_words_by_ref -n = cword words
34+
35+
output=$(scw autocomplete complete bash -- "$COMP_LINE" "$cword" "${words[@]}")
36+
COMPREPLY=($output)
37+
# apply compopt option and ignore failure for older bash versions
38+
[[ $COMPREPLY == *= ]] && compopt -o nospace 2> /dev/null || true
39+
return
40+
}
41+
complete -F _scw scw
42+
```
43+
44+
And in your `~/.bashrc` you can add:
45+
```bash
46+
scw() {
47+
docker run -it --rm -v $HOME/.config/scw/config.yaml:/.config/scw/config.yaml:ro scaleway/cli "$@"
48+
}
49+
export -f scw
50+
51+
_scw() {
52+
_get_comp_words_by_ref -n = cword words
53+
54+
output=$(docker run -i --rm -v $HOME/.config/scw/config.yaml:/.config/scw/config.yaml:ro scaleway/cli autocomplete complete bash -- "$COMP_LINE" "$cword" "${words[@]}")
55+
COMPREPLY=($output)
56+
# apply compopt option and ignore failure for older bash versions
57+
[[ $COMPREPLY == *= ]] && compopt -o nospace 2> /dev/null || true
58+
return
59+
}
60+
complete -F _scw scw
61+
```
62+
63+
The trick is to remove the `-t` when using docker inside the completion function.

scripts/build.sh

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
#!/bin/bash
22

3-
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
4-
ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
5-
BIN_DIR="$ROOT_DIR/bin"
6-
7-
mkdir -p $BIN_DIR
83

94
LDFLAGS=(
105
-w
@@ -15,9 +10,20 @@ LDFLAGS=(
1510
-X main.BuildDate="$(date -u '+%Y-%m-%dT%I:%M:%S%p')"
1611
)
1712

18-
VERSION=$(go run cmd/scw/main.go -o json version | jq -r .version | tr . -)
19-
2013
export CGO_ENABLED=0
21-
GOOS=linux GOARCH=amd64 go build -ldflags "${LDFLAGS[*]}" -o "$BIN_DIR/scw-$VERSION-linux-x86_64" cmd/scw/main.go
22-
GOOS=darwin GOARCH=amd64 go build -ldflags "${LDFLAGS[*]}" -o "$BIN_DIR/scw-$VERSION-darwin-x86_64" cmd/scw/main.go
23-
GOOS=windows GOARCH=amd64 go build -ldflags "${LDFLAGS[*]}" -o "$BIN_DIR/scw-$VERSION-windows-x86_64.exe" cmd/scw/main.go
14+
15+
if [[ "${BUILD_IN_DOCKER}" == "true" ]]; then
16+
GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -ldflags "${LDFLAGS[*]}" ./cmd/scw
17+
else
18+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
19+
ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
20+
BIN_DIR="$ROOT_DIR/bin"
21+
22+
mkdir -p $BIN_DIR
23+
24+
VERSION=$(go run cmd/scw/main.go -o json version | jq -r .version | tr . -)
25+
26+
GOOS=linux GOARCH=amd64 go build -ldflags "${LDFLAGS[*]}" -o "$BIN_DIR/scw-$VERSION-linux-x86_64" cmd/scw/main.go
27+
GOOS=darwin GOARCH=amd64 go build -ldflags "${LDFLAGS[*]}" -o "$BIN_DIR/scw-$VERSION-darwin-x86_64" cmd/scw/main.go
28+
GOOS=windows GOARCH=amd64 go build -ldflags "${LDFLAGS[*]}" -o "$BIN_DIR/scw-$VERSION-windows-x86_64.exe" cmd/scw/main.go
29+
fi

0 commit comments

Comments
 (0)