|
| 1 | +name: tweeter-automation |
| 2 | +on: |
| 3 | + push: |
| 4 | + tags: |
| 5 | + - 'v[0-9]+.[0-9]+.*' |
| 6 | + branches: |
| 7 | + - main |
| 8 | + pull_request: |
| 9 | + branches: |
| 10 | + - main |
| 11 | +jobs: |
| 12 | + test: |
| 13 | + permissions: |
| 14 | + contents: read |
| 15 | + strategy: |
| 16 | + matrix: |
| 17 | + go-version: [ 1.16.x, 1.17.x ] |
| 18 | + os: [ ubuntu-latest, macos-latest, windows-latest ] |
| 19 | + runs-on: ${{ matrix.os }} |
| 20 | + steps: |
| 21 | + - name: install go |
| 22 | + uses: actions/setup-go@v2 |
| 23 | + with: |
| 24 | + go-version: ${{ matrix.go-version }} |
| 25 | + - uses: actions/checkout@v2 |
| 26 | + - name: lint with golangci-lint |
| 27 | + uses: golangci/golangci-lint-action@v2 |
| 28 | + - name: run go test |
| 29 | + run: go test ./... |
| 30 | + test-action: |
| 31 | + permissions: |
| 32 | + contents: read |
| 33 | + packages: read |
| 34 | + runs-on: ubuntu-latest |
| 35 | + steps: |
| 36 | + - uses: actions/checkout@v2 |
| 37 | + - name: test the tweeter action in DRY_RUN |
| 38 | + id: tweeterAction |
| 39 | + env: |
| 40 | + DRY_RUN: true |
| 41 | + uses: ./ |
| 42 | + with: |
| 43 | + message: hello world! |
| 44 | + accessToken: fake |
| 45 | + accessTokenSecret: fake |
| 46 | + apiKey: fake |
| 47 | + apiKeySecret: fake |
| 48 | + - run: echo ${{ steps.tweeterAction.outputs.sentMessage }} from dry run test |
| 49 | + release: |
| 50 | + permissions: |
| 51 | + contents: write |
| 52 | + needs: test |
| 53 | + if: startsWith(github.ref, 'refs/tags/v') |
| 54 | + runs-on: ubuntu-latest |
| 55 | + steps: |
| 56 | + - uses: actions/checkout@v2 |
| 57 | + - name: Set RELEASE_VERSION ENV var |
| 58 | + run: echo "RELEASE_VERSION=${GITHUB_REF:10}" >> $GITHUB_ENV |
| 59 | + - uses: actions/setup-go@v2 |
| 60 | + with: |
| 61 | + go-version: 1.17.x |
| 62 | + - name: install gox |
| 63 | + run: go install github.com/mitchellh/[email protected] |
| 64 | + - name: build cross-platform binaries |
| 65 | + env: |
| 66 | + PLATFORMS: darwin/amd64 darwin/arm64 windows/amd64 linux/amd64 linux/arm64 |
| 67 | + VERSION_INJECT: github.com/PacktPublishing/Go-for-DevOps/chapter/9/pkg/tweeter.Version |
| 68 | + OUTPUT_PATH_FORMAT: ./bin/${{ env.RELEASE_VERSION }}/{{.OS}}/{{.Arch}}/tweeter |
| 69 | + run: | |
| 70 | + gox -osarch="${PLATFORMS}" -ldflags "-X ${VERSION_INJECT}=${RELEASE_VERSION}" -output "${OUTPUT_PATH_FORMAT}" |
| 71 | + - name: generate release notes |
| 72 | + env: |
| 73 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 74 | + run: | |
| 75 | + gh api -X POST 'repos/{owner}/{repo}/releases/generate-notes' \ |
| 76 | + -F commitish=${{ env.RELEASE_VERSION }} \ |
| 77 | + -F tag_name=${{ env.RELEASE_VERSION }} \ |
| 78 | + > tmp-release-notes.json |
| 79 | + - name: gzip the bins |
| 80 | + env: |
| 81 | + OUT_BASE: ./bin/${{ env.RELEASE_VERSION }} |
| 82 | + run: | |
| 83 | + tar -czvf "${OUT_BASE}/darwin/amd64/tweeter_darwin_amd64.tar.gz" -C "${OUT_BASE}/darwin/amd64" tweeter |
| 84 | + tar -czvf "${OUT_BASE}/darwin/arm64/tweeter_darwin_arm64.tar.gz" -C "${OUT_BASE}/darwin/arm64" tweeter |
| 85 | + tar -czvf "${OUT_BASE}/windows/amd64/tweeter_windows_amd64.tar.gz" -C "${OUT_BASE}/windows/amd64" tweeter.exe |
| 86 | + tar -czvf "${OUT_BASE}/linux/amd64/tweeter_linux_amd64.tar.gz" -C "${OUT_BASE}/linux/amd64" tweeter |
| 87 | + tar -czvf "${OUT_BASE}/linux/arm64/tweeter_linux_arm64.tar.gz" -C "${OUT_BASE}/linux/arm64" tweeter |
| 88 | + - name: create release |
| 89 | + env: |
| 90 | + OUT_BASE: ./bin/${{ env.RELEASE_VERSION }} |
| 91 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 92 | + run: | |
| 93 | + jq -r .body tmp-release-notes.json > tmp-release-notes.md |
| 94 | + gh release create ${{ env.RELEASE_VERSION }} \ |
| 95 | + -t "$(jq -r .name tmp-release-notes.json)" \ |
| 96 | + -F tmp-release-notes.md \ |
| 97 | + "${OUT_BASE}/darwin/amd64/tweeter_darwin_amd64.tar.gz#tweeter_osx_amd64" \ |
| 98 | + "${OUT_BASE}/darwin/arm64/tweeter_darwin_arm64.tar.gz#tweeter_osx_arm64" \ |
| 99 | + "${OUT_BASE}/windows/amd64/tweeter_windows_amd64.tar.gz#tweeter_windows_amd64" \ |
| 100 | + "${OUT_BASE}/linux/amd64/tweeter_linux_amd64.tar.gz#tweeter_linux_amd64" \ |
| 101 | + "${OUT_BASE}/linux/arm64/tweeter_linux_arm64.tar.gz#tweeter_linux_arm64" |
0 commit comments