ssh-keygen Generating public/private rsa key pair. Enter file inwhich to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:XG1vkchp5b27tteZASx6ZrPRtTayGYmacRdjjRxR1Y0 root@8fe85d51123b The key's randomart image is: +---[RSA 2048]----+ | .+o=| | o *Eoo| | . X B .| | . . + X +.| | S . = O..| | o O B =.| | O = *.*| | o . o ++| | .oo| +----[SHA256]-----+
on: push: # Sequence of patterns matched against refs/tags tags: -"v*"# Push events to matching v*, i.e. v1.0, v20.15.10 jobs: build: runs-on:ubuntu-latest
steps: -name:Checkoutsource uses:actions/checkout@v1 -name:UseGolang uses:actions/setup-go@v1 with: go-version:"1.13.x" -name:Buildnormal run:| CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o normal-windows-x64.exe cmd/normal/main.go CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o normal-linux-x64 cmd/normal/main.go CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o normal-darwin-x64 cmd/normal/main.go zip normal-windows-x64.zip normal-windows-x64.exe zip normal-linux-x64.zip normal-linux-x64 zip normal-darwin-x64.zip normal-darwin-x64 -name:Buildcgo run:| go get github.com/monkeyWie/xgo ~/go/bin/xgo -targets=windows/amd64,linux/amd64,darwin/amd64 -ldflags="-w -s" -pkg=cmd/cgo/main.go -out=cgo . mv cgo-windows-* cgo-windows-x64.exe mv cgo-linux-* cgo-linux-x64 mv cgo-darwin-* cgo-darwin-x64 zip cgo-windows-x64.zip cgo-windows-x64.exe zip cgo-linux-x64.zip cgo-linux-x64 zip cgo-darwin-x64.zip cgo-darwin-x64 -name:CreateRelease id:create_release uses:monkeyWie/create-release@master env: GITHUB_TOKEN:${{secrets.GITHUB_TOKEN}} with: tag_name:${{github.ref}} release_name:Release${{github.ref}} draft:false prerelease:false
-name:UploadReleasenormalwindows uses:actions/upload-release-asset@v1.0.1 env: GITHUB_TOKEN:${{secrets.GITHUB_TOKEN}} with: upload_url:${{steps.create_release.outputs.upload_url}}# This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps asset_path:./normal-windows-x64.zip asset_name:normal-${{steps.create_release.outputs.tag}}-windows-x64.zip asset_content_type:application/zip -name:UploadReleasenormallinux uses:actions/upload-release-asset@v1.0.1 env: GITHUB_TOKEN:${{secrets.GITHUB_TOKEN}} with: upload_url:${{steps.create_release.outputs.upload_url}}# This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps asset_path:./normal-linux-x64.zip asset_name:normal-${{steps.create_release.outputs.tag}}-linux-x64.zip asset_content_type:application/zip -name:UploadReleasenormaldarwin uses:actions/upload-release-asset@v1.0.1 env: GITHUB_TOKEN:${{secrets.GITHUB_TOKEN}} with: upload_url:${{steps.create_release.outputs.upload_url}}# This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps asset_path:./normal-darwin-x64.zip asset_name:normal-${{steps.create_release.outputs.tag}}-darwin-x64.zip asset_content_type:application/zip
-name:UploadReleasecgowindows uses:actions/upload-release-asset@v1.0.1 env: GITHUB_TOKEN:${{secrets.GITHUB_TOKEN}} with: upload_url:${{steps.create_release.outputs.upload_url}}# This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps asset_path:./cgo-windows-x64.zip asset_name:cgo-${{steps.create_release.outputs.tag}}-windows-x64.zip asset_content_type:application/zip -name:UploadReleasecgolinux uses:actions/upload-release-asset@v1.0.1 env: GITHUB_TOKEN:${{secrets.GITHUB_TOKEN}} with: upload_url:${{steps.create_release.outputs.upload_url}}# This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps asset_path:./cgo-linux-x64.zip asset_name:cgo-${{steps.create_release.outputs.tag}}-linux-x64.zip asset_content_type:application/zip -name:UploadReleasecgodarwin uses:actions/upload-release-asset@v1.0.1 env: GITHUB_TOKEN:${{secrets.GITHUB_TOKEN}} with: upload_url:${{steps.create_release.outputs.upload_url}}# This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps asset_path:./cgo-darwin-x64.zip asset_name:cgo-${{steps.create_release.outputs.tag}}-darwin-x64.zip asset_content_type:application/zip
# 创建tag名为v1.0.8,并添加描述 git tag -a "v1.0.8" -m '发布v1.0.8版本 修复了以下bug: 1. xxxxx 2. xxxxx' # 把tag推到github上 git push --tags
然后就可以看到已经有一个新的workflow正在运行了:
运行完成后在Releases页面查看结果:
完美!和预想的结果一致。
注:由于官方的create-release有点不能满足需求,于是我自己fork了一份create-release代码,就是把tag name给输出来了,这里是相关的PR,还没被合并,所以上面的创建 Release 的 action 是用的我自己的仓库monkeyWie/create-release@master,还有关于 go 交叉编译的知识,有兴趣可以看看我的这篇博客:go-cross-compile。
自动构建和部署 docker 镜像
在Github Actions提供的虚拟机中,已经内置了docker,而刚好我有一个项目因为国内的网络原因构建docker镜像非常的慢,这是我fork的一个用于 go 项目交叉编译的项目,仓库地址https://github.com/monkeyWie/xgo,这个项目的主要工作原理就是通过 docker 里内置好各种交叉编译的工具链,然后对外提供 go 项目交叉编译功能,下面节选一点Dockerfile内容: