1.简介 Github Actions是一种持续集成和持续交付(CI/CD)平台,可用于自动执行生成、测试和部署管道。你可以创建工作流,以便在推送更改到存储库时运行测试,或将合并的拉取请求部署到生产环境。
2.工作流模版 Github提供预配置的工作流模版,可以按照原样使用或自定义它来创建自己的工作流。Github分析代码并显示可能对仓库有用的工作流模版。
使用这些工作流作为构建自定义工作流的起点或按原样使用它们。 可以在 actions/starter-workflows 存储库中浏览工作流模板的完整列表。
3.创建第一个工作流
(1) 在Github上的存储库中,创建.github/workflows目录,并向里面添加一个github-actions-demo.yml工作流文件。
1 2 3 4 >> mkdir -p .github/workflows >> vim .github/workflows/github-actions-demo.yml
(2) 将下面YAML内容复制到github-actions-demo.yml文件中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 name: GitHub Actions Demo run-name: ${{ github.actor }} is testing out GitHub Actions 🚀 on: [push ]jobs: Explore-GitHub-Actions: runs-on: ubuntu-latest steps: - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event." - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!" - run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }} ." - name: Check out repository code uses: actions/checkout@v5 - run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner." - run: echo "🖥️ The workflow is now ready to test your code on the runner." - name: List files in the repository run: | ls ${{ github.workspace }} - run: echo "🍏 This job's status is ${{ job.status }} ."
1 2 3 4 5 6 >> git add . >> git commit -m "Add workflow" >> git push -u origin main
4.查看工作流结果
(1) 在Github上,导航到存储库页面;
(2) 单击”Actions”:
(3) 也可以单击“Re-run all jobs”再次执行
5.CI工作流学习 开发语言主要是C++,所以这里以搜狗开源的workflow 框架的ci.yml工作流学习:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 name: ci build on: push: branches: [ master ] pull_request: branches: [ master ] jobs: ubuntu-cmake: name: ubuntu runs-on: ubuntu-latest steps: - name: setup run: | sudo apt-get update sudo apt-get install -y cmake g++ libgtest-dev make libssl-dev sudo apt-get install -y valgrind sudo apt-get install -y libsnappy-dev libzstd-dev liblz4-dev - uses: actions/checkout@v2 - name: make run: make KAFKA=y - name: make check run: make check KAFKA=y - name: make tutorial run: make tutorial KAFKA=y fedora-cmake: ... freebsd-cmake: ...
6.同步到gitee
(1) gitee创建同名项目,并在账号上添加公钥:
1 2 3 4 5 6 7 8 9 10 11 12 >> cat ~/.ssh/id_ed25519.pub >> ssh -T git@gitee.com Hi MirrorYuChen(@mirror_yu_chen)! You've successfully authenticated, but GITEE.COM does not provide shell access. # 4.生成私人令牌 # "设置" -> "账号设置" -> "私人令牌" -> "+生成新令牌" -> "私人令牌描述": Access-Token,"令牌过期时间": 永不过期 -> "请选择将要生成的私人令牌所拥有的权限": "user_info", "projects", "pull_requests" -> "提交" -> 输入密码,完成"密码验证" -> "验证" -> 复制并保存生成的Token。
1 2 3 4 5 6 7 8 9 >> ssh -T git@github.com Hi MirrorYuChen! You've successfully authenticated, but GitHub does not provide shell access. # 3.生成私人令牌 # "Settings" -> "Developer Settings" -> "Personal access tokens" -> "Generate new token(classic)" -> "Note": Access_TOKEN,勾选"repo" -> "Generate token"
1 2 3 >> cat ~/.ssh/id_ed25519
注意,要添加两个:
(a) GITEE_PRIVATE_KEY
Name: GITEE_PRIVATE_KEY
Secret: 粘贴本地私钥内容,可以本地通过cat ~/.ssh/id_ed25519得到
(b) GITEE_TOKEN
Name: GITEE_TOKEN
Secret:粘贴gitee令牌内容
(c) ACCESS_TOKEN
Name: ACCESS_TOKEN
Secret: 粘贴github令牌内容
(4) 添加同步到gitee的工作流
1 2 3 >> vim .github/workflows/sync.yml
注意将dst改成你自己gitee仓库地址,比如我的是mirror_yu_chen。dst_account_type当为组织时,设置为org,个人用户设置为user
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 name: sync on: push: branches: [ main ] pull_request: branches: [ main ] jobs: repo-sync: name: sync env: dst_key: ${{ secrets.GITEE_PRIVATE_KEY }} dst_token: ${{ secrets.GITEE_TOKEN }} src_token: ${{ secrets.ACCESS_TOKEN }} runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: persist-credentials: false - name: Setup git credentials for GitHub run: | git config --global credential.helper store echo "https://oauth2:${{ secrets.ACCESS_TOKEN }}@github.com" > ~/.git-credentials - name: sync github -> gitee uses: Yikun/hub-mirror-action@master if: env.dst_key && env.dst_token && env.src_token with: src: 'github/${{ github.repository_owner }}' dst: gitee/mirror_yu_chen src_token: ${{ secrets.ACCESS_TOKEN }} dst_key: ${{ secrets.GITEE_PRIVATE_KEY }} dst_token: ${{ secrets.GITEE_TOKEN }} static_list: ${{ github.event.repository.name }} dst_account_type: user force_update: true
参考资料