Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Intro to GoReleaser

Intro to GoReleaser

Talk for the Sri Lanka Golang group.

Carlos Alexandro Becker

July 23, 2021
Tweet

More Decks by Carlos Alexandro Becker

Other Decks in Programming

Transcript

  1. Introduction to GoReleaser 1 — Golang Sri Lanka - July

    23th, 2021 - carlosbecker.dev/talks/
  2. Who am I? → SRE @ totvslabs → @caarlos0 almost

    everywhere → carlosbecker.dev/talks/ → goreleaser.com → nfpm.goreleaser.com 2 — Golang Sri Lanka - July 23th, 2021 - carlosbecker.dev/ talks/
  3. What is GoReleaser? → an automation tool to build and

    release Go projects → setup via a yaml file once → release anytime with git tag + goreleaser 3 — Golang Sri Lanka - July 23th, 2021 - carlosbecker.dev/talks/
  4. What can it do? → Build a matrix of GOOS,

    GOARCH, GOARM, etc → Create archives for each platform → Create Linux packages: deb, rpm, apk, snap → Create Homebrew taps and Scoop buckets → Create Docker images and manifests 4 — Golang Sri Lanka - July 23th, 2021 - carlosbecker.dev/talks/
  5. What can it do? → Sign release files → Upload

    files to Artifactory, Fury, blob storage, and basically anything that accepts a PUT request → Create a release on GitHub/GitLab/Gitea and upload artifacts there → Announce new releases on Twitter 5 — Golang Sri Lanka - July 23th, 2021 - carlosbecker.dev/talks/
  6. Let's see it in practice! 6 — Golang Sri Lanka

    - July 23th, 2021 - carlosbecker.dev/talks/
  7. Demo Time! 7 — Golang Sri Lanka - July 23th,

    2021 - carlosbecker.dev/talks/
  8. What will we cover? → Initial project setup → Go

    mod proxying → Linux packaging → Docker images → GitHub Actions 8 — Golang Sri Lanka - July 23th, 2021 - carlosbecker.dev/talks/
  9. Creating a project 9 — Golang Sri Lanka - July

    23th, 2021 - carlosbecker.dev/talks/
  10. Creating a project First, lets create a new GitHub project:

    mkdir example-golang-sri-lanka cd example-golang-sri-lanka git init gh repo create caarlos0/example-golang-sri-lanka -y --public -l MIT git pull origin main 10 — Golang Sri Lanka - July 23th, 2021 - carlosbecker.dev/talks/
  11. Creating a project Next step is to make it a

    Go project: go mod init github.com/caarlos0/example-golang-sri-lanka With a main.go file like this: package main import ( "fmt" ) func main() { fmt.Println("hello golang community") } 11 — Golang Sri Lanka - July 23th, 2021 - carlosbecker.dev/talks/
  12. Creating a project Next step: create and verify a .goreleaser.yml

    file: goreleaser init goreleaser check goreleaser release --rm-dist --snapshot 12 — Golang Sri Lanka - July 23th, 2021 - carlosbecker.dev/talks/
  13. Creating a project Finally, lets try a real release: git

    add -A git commit -am 'feat: first commit' git tag v1.0.0 git push --tags goreleaser release --rm-dist gh release view v1.0.0 -w 13 — Golang Sri Lanka - July 23th, 2021 - carlosbecker.dev/talks/
  14. Go Mod Proxy 14 — Golang Sri Lanka - July

    23th, 2021 - carlosbecker.dev/talks/
  15. Go Mod Proxy First, lets enable the feature on .goreleaser.yml:

    # ... gomod: proxy: true # ... 15 — Golang Sri Lanka - July 23th, 2021 - carlosbecker.dev/talks/
  16. Go Mod Proxy Then, lets use it on our main.go:

    package main import ( "fmt" "runtime/debug" ) func main() { fmt.Println("hello golang community") if info, ok := debug.ReadBuildInfo(); ok { fmt.Println(info.Main.Path, info.Main.Version, info.Main.Sum) } } 16 — Golang Sri Lanka - July 23th, 2021 - carlosbecker.dev/talks/
  17. Go Mod Proxy Let's try it out first: go run

    . 17 — Golang Sri Lanka - July 23th, 2021 - carlosbecker.dev/talks/
  18. Go Mod Proxy Let's release it: git commit -am 'ci:

    go mod proxy' git tag v1.0.1 git push --tags goreleaser release --rm-dist ./dist/example-golang-sri-lanka_darwin_amd64/example-golang-sri-lanka 18 — Golang Sri Lanka - July 23th, 2021 - carlosbecker.dev/talks/
  19. Linux Packages Minimal config: # ... nfpms: - formats: [deb,

    apk, rpm] # ... 20 — Golang Sri Lanka - July 23th, 2021 - carlosbecker.dev/talks/
  20. Linux Packages Then we just need to release them: git

    commit -am 'ci: nfpm packages' git tag v1.0.2 git push --tags goreleaser release --rm-dist 21 — Golang Sri Lanka - July 23th, 2021 - carlosbecker.dev/talks/
  21. Linux Packages Let's test! docker run --rm -v $PWD/dist:/tmp/ alpine

    sh -c \ 'apk add -q --allow-untrusted /tmp/*_amd64.apk && example-golang-sri-lanka' docker run --rm -v $PWD/dist:/tmp/ ubuntu sh -c \ 'dpkg -i /tmp/*_amd64.deb && example-golang-sri-lanka' docker run --rm -v $PWD/dist:/tmp/ fedora sh -c \ 'rpm -i /tmp/*_amd64.rpm && example-golang-sri-lanka' 22 — Golang Sri Lanka - July 23th, 2021 - carlosbecker.dev/talks/
  22. Docker images Minimal config: # ... dockers: - image_templates: -

    caarlos0/{{ .ProjectName }}:{{ .Tag }} - caarlos0/{{ .ProjectName }}:latest # ... 24 — Golang Sri Lanka - July 23th, 2021 - carlosbecker.dev/talks/
  23. Docker images A simple Dockerfile: FROM alpine COPY *.apk /tmp/

    RUN apk add --allow-untrusted /tmp/*.apk ENTRYPOINT ["/usr/local/bin/example-golang-sri-lanka"] 25 — Golang Sri Lanka - July 23th, 2021 - carlosbecker.dev/talks/
  24. Docker images Release: git add -A git commit -am 'ci:

    nfpm packages' git tag v1.0.3 git push --tags goreleaser release --rm-dist 26 — Golang Sri Lanka - July 23th, 2021 - carlosbecker.dev/talks/
  25. Docker images Test it: docker run --rm caarlos0/example-golang-sri-lanka 27 —

    Golang Sri Lanka - July 23th, 2021 - carlosbecker.dev/talks/
  26. GitHub Actions Refer to https://goreleaser.com/ci/actions/ mkdir -p .github/workflows wget https://static.caarlos0.dev/goreleaser.yml

    -O .github/workflows/goreleaser.yml gh secret set DOCKER_PASSWORD -b $DOCKER_PASSWORD git add -A git commit -am 'ci: create workflow' git push origin HEAD 29 — Golang Sri Lanka - July 23th, 2021 - carlosbecker.dev/talks/
  27. GitHub Actions git tag v1.0.4 git push origin HEAD --tags

    gh run view -w 30 — Golang Sri Lanka - July 23th, 2021 - carlosbecker.dev/talks/
  28. Learning more The website has a lot of documentation, examples,

    and cookbooks. If you want only the Linux packaging stuff, check nfpm. 32 — Golang Sri Lanka - July 23th, 2021 - carlosbecker.dev/talks/