├── .drone.yml ├── .github └── workflows │ └── deploy.yml ├── .gitignore ├── Dockerfile ├── Dockerfile.alpine ├── LICENSE ├── Makefile ├── README.md ├── docker-compose.yml ├── docker └── helloworld │ └── Dockerfile.linux.amd64 ├── go.mod ├── main.go ├── main_test.go ├── screenshots └── traefik+docker+golang.png └── traefik ├── docker-compose.yml └── traefik.toml /.drone.yml: -------------------------------------------------------------------------------- 1 | kind: pipeline 2 | type: docker 3 | name: linux_amd64 4 | 5 | environment: 6 | GOOS: linux 7 | GOARCH: amd64 8 | CGO_ENABLED: 0 9 | 10 | steps: 11 | - name: build-linux-amd64 12 | image: golang:1.17 13 | commands: 14 | - make build 15 | - ./release/linux/amd64/helloworld -h 16 | 17 | --- 18 | kind: pipeline 19 | type: docker 20 | name: linux_386 21 | 22 | environment: 23 | GOOS: linux 24 | GOARCH: 386 25 | CGO_ENABLED: 0 26 | 27 | steps: 28 | - name: build-linux-386 29 | image: golang:1.17 30 | commands: 31 | - make build 32 | - ./release/linux/386/helloworld -h 33 | 34 | --- 35 | kind: pipeline 36 | type: docker 37 | name: build_all 38 | 39 | steps: 40 | - name: build-linux-386 41 | image: golang:1.17 42 | environment: 43 | GOOS: linux 44 | GOARCH: 386 45 | CGO_ENABLED: 0 46 | commands: 47 | - make build 48 | - ./release/linux/386/helloworld -h 49 | 50 | - name: build-linux-amd64 51 | image: golang:1.17 52 | environment: 53 | GOOS: linux 54 | GOARCH: amd64 55 | CGO_ENABLED: 0 56 | commands: 57 | - make build 58 | - ./release/linux/amd64/helloworld -h 59 | 60 | - name: notify 61 | image: golang:1.17 62 | commands: 63 | - echo "Done!" 64 | depends_on: 65 | - build-linux-amd64 66 | - build-linux-386 67 | -------------------------------------------------------------------------------- /.github/workflows/deploy.yml: -------------------------------------------------------------------------------- 1 | name: Build and Test 2 | on: 3 | push: 4 | branches: 5 | - master 6 | pull_request: 7 | jobs: 8 | build: 9 | strategy: 10 | matrix: 11 | # go-version: [1.13.x] 12 | platform: [ubuntu-latest] 13 | runs-on: ${{ matrix.platform }} 14 | container: golang:1.13 15 | steps: 16 | # - name: Install Go 17 | # uses: actions/setup-go@v1 18 | # with: 19 | # go-version: ${{ matrix.go-version }} 20 | 21 | - name: Check out code 22 | uses: actions/checkout@v1 23 | 24 | - name: Tesing 25 | run: | 26 | make test 27 | 28 | - name: Build binary 29 | run: | 30 | make build_linux_amd64 31 | 32 | - name: Publish to Registry 33 | uses: elgohr/Publish-Docker-Github-Action@2.9 34 | with: 35 | name: appleboy/helloworld 36 | username: appleboy 37 | password: ${{ secrets.docker_password }} 38 | dockerfile: docker/helloworld/Dockerfile.linux.amd64 39 | 40 | - name: Update the API service 41 | uses: appleboy/ssh-action@v0.0.6 42 | with: 43 | host: ${{ secrets.ssh_host }} 44 | username: deploy 45 | key: ${{ secrets.ssh_key }} 46 | script_stop: true 47 | script: | 48 | cd golang && docker-compose pull && docker-compose up -d 49 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | release/* 2 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM plugins/base:multiarch 2 | 3 | LABEL maintainer="Bo-Yi Wu " \ 4 | org.label-schema.name="Drone Workshop" \ 5 | org.label-schema.vendor="Bo-Yi Wu" \ 6 | org.label-schema.schema-version="1.0" 7 | 8 | ADD release/linux/amd64/helloworld /bin/ 9 | 10 | HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 CMD [ "/bin/helloworld", "-ping" ] 11 | 12 | ENTRYPOINT ["/bin/helloworld"] 13 | -------------------------------------------------------------------------------- /Dockerfile.alpine: -------------------------------------------------------------------------------- 1 | FROM alpine:3.8 2 | 3 | LABEL maintainer="Bo-Yi Wu " \ 4 | org.label-schema.name="Drone Workshop" \ 5 | org.label-schema.vendor="Bo-Yi Wu" \ 6 | org.label-schema.schema-version="1.0" 7 | 8 | RUN apk add --no-cache ca-certificates && \ 9 | rm -rf /var/cache/apk/* 10 | 11 | ADD release/linux/amd64/helloworld /bin/ 12 | 13 | ENTRYPOINT ["/bin/helloworld"] 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | build: 2 | go build -v -a -o release/${GOOS}/${GOARCH}/helloworld 3 | 4 | docker: 5 | docker build -t appleboy/helloworld . 6 | 7 | test: 8 | go test -v . 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # drone-golang-example 2 | 3 | Golang example with drone service 4 | 5 | [![Build Status](https://cloud.drone.io/api/badges/go-training/drone-golang-example/status.svg)](https://cloud.drone.io/go-training/drone-golang-example) 6 | 7 | ## Install Drone 1.0.x 8 | 9 | Please see the [drone tutorial](https://github.com/go-training/drone-tutorial). 10 | 11 | ## Traefik + Docker Deploy 12 | 13 | 14 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | 3 | services: 4 | app_1: 5 | image: appleboy/test:alpine 6 | restart: always 7 | networks: 8 | - web 9 | logging: 10 | options: 11 | max-size: "100k" 12 | max-file: "3" 13 | labels: 14 | - "traefik.docker.network=web" 15 | - "traefik.enable=true" 16 | - "traefik.basic.frontend.rule=Host:domain1.com" 17 | - "traefik.basic.port=8080" 18 | - "traefik.basic.protocol=http" 19 | 20 | app_2: 21 | image: appleboy/test:alpine 22 | restart: always 23 | networks: 24 | - web 25 | logging: 26 | options: 27 | max-size: "100k" 28 | max-file: "3" 29 | labels: 30 | - "traefik.docker.network=web" 31 | - "traefik.enable=true" 32 | - "traefik.basic.frontend.rule=Host:domain2.com" 33 | - "traefik.basic.port=8080" 34 | - "traefik.basic.protocol=http" 35 | 36 | networks: 37 | web: 38 | external: true 39 | -------------------------------------------------------------------------------- /docker/helloworld/Dockerfile.linux.amd64: -------------------------------------------------------------------------------- 1 | FROM plugins/base:linux-amd64 2 | 3 | LABEL maintainer="Bo-Yi Wu " \ 4 | org.label-schema.name="helloworld" \ 5 | org.label-schema.vendor="Bo-Yi Wu" \ 6 | org.label-schema.schema-version="1.0" 7 | 8 | EXPOSE 8080 9 | 10 | COPY release/linux/amd64/helloworld /bin/ 11 | 12 | ENTRYPOINT ["/bin/helloworld"] 13 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/go-training/drone-golang-example 2 | 3 | go 1.17 4 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "log" 7 | "net/http" 8 | "os" 9 | "time" 10 | ) 11 | 12 | // HelloWorld for hello world 13 | func HelloWorld() string { 14 | return "Trigger the GitHub Actions, traefik workshop!" 15 | } 16 | 17 | func handler(w http.ResponseWriter, r *http.Request) { 18 | log.Printf("Got http request. time: %v", time.Now()) 19 | fmt.Fprintf(w, "I love %s!, %s", r.URL.Path[1:], HelloWorld()) 20 | } 21 | 22 | func pinger(port string) error { 23 | resp, err := http.Get("http://localhost:" + port) 24 | if err != nil { 25 | return err 26 | } 27 | defer resp.Body.Close() 28 | if resp.StatusCode != 200 { 29 | return fmt.Errorf("server returned not-200 status code") 30 | } 31 | 32 | return nil 33 | } 34 | 35 | func main() { 36 | var port string 37 | var ping bool 38 | flag.StringVar(&port, "port", "8080", "server port") 39 | flag.StringVar(&port, "p", "8080", "server port") 40 | flag.BoolVar(&ping, "ping", false, "check server live") 41 | flag.Parse() 42 | 43 | if p, ok := os.LookupEnv("PORT"); ok { 44 | port = p 45 | } 46 | 47 | if ping { 48 | if err := pinger(port); err != nil { 49 | log.Printf("ping server error: %v\n", err) 50 | } 51 | 52 | return 53 | } 54 | 55 | http.HandleFunc("/", handler) 56 | log.Println("http server run on " + port + " port") 57 | log.Fatal(http.ListenAndServe(":"+port, nil)) 58 | } 59 | -------------------------------------------------------------------------------- /main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "testing" 4 | 5 | func TestHello(t *testing.T) { 6 | if HelloWorld() != "Trigger the GitHub Actions, traefik workshop!" { 7 | t.Error("Testing error") 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /screenshots/traefik+docker+golang.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/go-training/drone-golang-example/67c64a19c84034c8fe4a86c89d4c124ac2a91f77/screenshots/traefik+docker+golang.png -------------------------------------------------------------------------------- /traefik/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | 3 | services: 4 | traefik: 5 | image: traefik 6 | restart: always 7 | ports: 8 | - 8080:8080 9 | # - 80:80 10 | # - 443:443 11 | networks: 12 | - web 13 | volumes: 14 | - /var/run/docker.sock:/var/run/docker.sock 15 | - ./traefik.toml:/traefik.toml 16 | # - ./acme.json:/acme.json 17 | container_name: traefik 18 | 19 | networks: 20 | web: 21 | external: true 22 | -------------------------------------------------------------------------------- /traefik/traefik.toml: -------------------------------------------------------------------------------- 1 | debug = false 2 | 3 | logLevel = "INFO" 4 | defaultEntryPoints = ["http"] 5 | 6 | [entryPoints] 7 | [entryPoints.http] 8 | address = ":8080" 9 | 10 | [retry] 11 | 12 | ################################################################ 13 | # Docker Provider 14 | ################################################################ 15 | 16 | # Enable Docker Provider. 17 | [docker] 18 | 19 | # Docker server endpoint. Can be a tcp or a unix socket endpoint. 20 | # 21 | # Required 22 | # 23 | endpoint = "unix:///var/run/docker.sock" 24 | 25 | # Enable watch docker changes. 26 | # 27 | # Optional 28 | # 29 | watch = true 30 | --------------------------------------------------------------------------------