[GO] Bitbucket → GitHub migration part 3 ~ CI surroundings ~

This is a record of Mercurial users starting to use Git and being taken care of by GitHub.

I make tools exclusively using Go.

In this article, as a method of automatically releasing with the mechanism of CI, Use GoReleaser on GitHub Actions to set the binary to be uploaded to GitHub Releases for each tag.

master→main

If you don't do it separately, it won't affect your future work or success in GitHub Actions.

Rename the default branch on Git (GitHub) [https://qiita.com/e_chan1007/items/ad3e849b7e04fcf42fad), but change the default branch name to main.

In the above article, I've set up to switch branches locally and then push them to change the default branch on GitHub. I don't forget to switch branches locally, so I think that's a good way to do it. __ In any case, it seems that the default branch of GitHub can only be done on the GitHub site. __

If you say "you only need to change on GitHub for the time being", it will be completed on the GitHub site. (Procedure below) However, since I will change the file in connection with GitHub Actions after this, I do not have to worry about completing it on the site.

Create a branch main on GitHub

image.png

Enter "main"

image.png

At this point, the default branch hasn't changed yet. (↓ "default" is attached to master)

image.png

Change default branch

View a list of branches

image.png

Click "Change default branch"

image.png

Switch to main and press the "Update" button next to it

image.png

Go back to the branch list page and click the trash can icon under master

image.png

Will be like this

image.png

Switch locally

git pull
git switch main

This concludes the branch change procedure.

GitHub Actions

Create and place two files

Create the following two files.

Location of the file to be created

image.png

See below for the contents of the file.

.github/workflows/release.yml

It seems that the file name does not have to be release.yml separately.


name: release
on:
  push:
    tags:
    - "v[0-9]+.[0-9]+.[0-9]+"
jobs:
  goreleaser:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Setup Go
        uses: actions/setup-go@v2
      - name: Run GoReleaser
        uses: goreleaser/goreleaser-action@v2
        with:
          version: latest
          args: release --rm-dist
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

It's written exclusively using uses: xxx, but you can use commands like on the shell with run: xxx.

GoReleaser doesn't allow git dirty conditions, so if you download and use the tool with go get, you may need to do run: go mod tidy before running GoReleaser.

Action is executed at the timing of git push --tags. It seems that GoReleaser cannot be released multiple times for the same tag. Also, if the description at the beginning is on: push: branches: --main (line feed omitted), Action was not executed in the first place for some reason.

Added goreleaser.yml

project_name: vvin
env:
  - GO111MODULE=on
before:
  hooks:
    - go mod tidy
builds:
  - binary: vvin
    flags:
      - -trimpath
    ldflags:
      - -s -w
      - -X main.Version={{.Version}}
      - -X main.Revision={{.ShortCommit}}
    env:
      - CGO_ENABLED=0
    goos:
      - windows
      - darwin
      - linux
    goarch:
      - amd64
      - 386
    hooks:
      post: upx --lzma '{{ .Path }}'
archives:
  - name_template: '{{ .ProjectName }}_v{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}'
    replacements:
      darwin: darwin
      linux: linux
      windows: windows
      386: 386
      amd64: amd64
    format_overrides:
      - goos: windows
        format: zip
release:
  prerelease: auto

: heart_eyes: Among the release tools, I'm grateful to be able to add -trimpath and run upx.

If the git tag is v1.2.3, {{.Version}} seems to be 1.2.3.

You can also do goreleaser init to create a template for .goreleaser.yml. Although it is different from the above content.

If you want to add files to the archive

If you want to include various things in the one released on GitHub Releases, add it to archives:.

archives:
  - name_template: '{{ .ProjectName }}_v{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}'
    replacements:

      :

    files:
      - LICENSE*
      - README*
      - sample.bat

The default for files: seems to be LICENSE *, README *. However, if you write files: as above, that setting seems to disappear, so you need to write it including the default value.

push

After committing the above changes, git push, add a new tag, and also git push --tags.

Tags are assigned in the format v {Major}. {Minor}. {Patch}. As specified in release.yml set above, it will be executed only when a tag of this format is pushed.

result

image.png

(There are few files, but I changed the above .goreleaser.yml to binaries for Windows only)

Compared to CircleCI

When I was using Mercurial on BitBucket, I was using CircleCI. Since I switched to GitHub, I wanted to experience GitHub Actions, so I switched.

I use CircleCI on a free plan, but GitHub Actions now takes about 30 seconds instead of about 10 seconds.

Also, I think this is a part related to the GoReleaser specification, but it seems that it can not be successfully upgraded to Releases without changing the tag. It's a little stressful if you don't want to change the tag because the function hasn't changed ...

Recommended Posts

Bitbucket → GitHub migration part 3 ~ CI surroundings ~