name: 自动流水线示例 run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀 on: pull_request: branches: ["main"] types: [closed] push: branches: [ "main","releases/**" ] tags: - '*' # 任意的tag都会触发 # paths: # - 'sub-project/**' # - '!sub-project/docs/**' # !在正匹配之后匹配负模式(以 为前缀)将排除该路径。 # paths-ignore: # - 'docs/**' # tags: # - v2 # - v1.* # schedule: # - cron: '30 5 * * 1,3' # - cron: '30 5 * * 2,4' jobs: build: runs-on: ubuntu-latest steps: # https://github.com/actions/checkout - name: 下载代码 uses: actions/checkout@v4 - run: | echo "🎉 The job was automatically triggered by a ${{ gitea.event_name }} event." echo "🐧 This job is now running on a ${{ runner.os }} server hosted by Gitea!" echo "🔎 The name of your branch is ${{ gitea.ref }} and your repository is ${{ gitea.repository }}." - name: 获取提交的消息 if: github.event_name == 'push' run: | echo "提交的信息: ${{ github.event.head_commit.message }}" commit_title=$(git log -1 --pretty=format:"%s") commit_body=$(git log -1 --pretty=format:"%b") echo "Commit Title: $commit_title" echo "Commit Body: $commit_body" commit_name=$(git log -1 --pretty=format:"%an") echo "Commit name: $commit_name" commit_email=$(git log -1 --pretty=format:"%ae") echo "作者邮箱: $commit_email" commit_date=$(git log -1 --pretty=format:"%ci") echo "作者提交时间: $commit_date" commit_durl=$(git log -1 --pretty=format:"%cr") echo "提交时长: $commit_durl" commit_hash=$(git log -1 --pretty=format:"%H") echo "提交Hash: $commit_hash" if [[ "$commit_body" =~ "发布生产" ]]; then echo '包含: if echo "$string1" | grep -q "$string2"; then' exit 0 else echo "不包含" exit 1 fi - name: 获取合并请求的消息 if: github.event_name == 'pull_request' env: PR_STATUS_FLAG: ${{ github.event.pull_request.merged }} run: | echo "是否是合并关闭的 $PR_STATUS_FLAG" if [[ "$PR_STATUS_FLAG" = true ]] ; then echo "合并完成" else echo "取消合并" fi pr_num=${{ github.event.pull_request.number }} git fetch origin refs/pull/${pr_num}/head:refs/remotes/origin/pull/${pr_num}/head commit_title=$(git log -1 --pretty=format:"%s" origin/pull/${pr_num}/head) commit_body=$(git log -1 --pretty=format:"%b" origin/pull/${pr_num}/head) echo "合并请求 Commit Title: $commit_title" echo "合并请求Commit Body: $commit_body" if echo "$commit_body" | grep -q "发布生产"; then exit 0 else echo "不包含" exit 1 fi # https://github.com/actions/setup-node 和 https://cicube.io/workflow-hub/github-action-setup-node/ - name: 安装nodejs uses: actions/setup-node@v4 with: node-version: '22' check-latest: true # cache: 'npm' # cache-dependency-path: '**/package-lock.json' - name: 安装node依赖 run: npm --version - name: 运行node测试 run: echo "npm test 成功actions/github-script@v5创建合并请求" - name: 安装JDK uses: actions/setup-java@v4 with: distribution: 'temurin' # See 'Supported distributions' for available options java-version: '21' check-latest: true cache: 'maven' # cache-dependency-path: 'sub-project/pom.xml' # optional - name: 安装Maven uses: stCarolas/setup-maven@v5 with: maven-version: 3.9.11 - name: Maven打包 run: | echo 'mvn clean install -DskipTests && mvn -B package --file pom.xml' mvn --version - name: 列出工作目录列表 run: | ls ${{ gitea.workspace }} - name: 构建镜像并推送 run: | echo 'docker login -u "${{ secrets.DOCKER_USERNAME }}" -p "${{ secrets.DOCKER_PASSWORD }}"' echo 'docker build -t .' echo 'docker push :latest' docker info deploy: needs: build runs-on: ubuntu-latest steps: - run: echo "Deploying build ${{ needs.build.outputs.build_id }}" - name: 执行远程SSH命令 uses: appleboy/ssh-action@v1 env: FOO: "BAR" BAR: "FOO" with: host: ${{ vars.DEPLOY_HOST }} # host: "foo.com:1234,bar.com:5678" username: ${{ secrets.USERNAME }} password: ${{ secrets.PASSWORD }} # key: ${{ secrets.KEY }} # passphrase: ${{ secrets.PASSPHRASE }} port: ${{ vars.PORT }} # sync: true envs: FOO,BAR # script_path: scripts/script.sh script: | whoami ls -al echo "I am $FOO" - run: echo "🍏 This job's status is ${{ job.status }}." check: name: 检查 needs: [build, deploy] runs-on: ubuntu-latest # if: ${{ success() }} 参考:https://docs.github.com/en/actions/reference/workflows-and-actions/expressions if: ${{ success() }} steps: - run: echo "任务成功构建和部署完成" upload-test: name: 制品上传 needs: check strategy: matrix: os: [ubuntu-latest] #os: [ubuntu-latest, windows-latest,macos-latest] https://github.com/actions/runner-images version: [a, b, c] runs-on: ${{ matrix.os }} if: ${{ failure() }} steps: - name: 生成制品 # run: ./some-script --version=${{ matrix.version }} > my-binary run: echo "系统=${{ matrix.os }},version=${{ matrix.version }}" > my-binary-${{ matrix.os }}-${{ matrix.version }} - name: 安装上传制品插件 uses: actions/upload-artifact@v3 #download-artifact@v4,v4版本不支持github企业服务[GitHub Enterprise Server(GHES)]v3支持 id: artifact-upload-step-id with: name: binary-${{ matrix.os }}-${{ matrix.version }} path: my-binary-${{ matrix.os }}-${{ matrix.version }} retention-days: 90 # 保留期必须介于 1 到 90 天(含)之间。 if-no-files-found: error # 'warn' or 'ignore' are also available, defaults to `warn` compression-level: 9 # 6默认压缩如果设置成0不进行文件压缩 #path: | # path/output/bin/ # path/output/test-results # !path/**/*.tmp - name: 输出制品相关信息 run: | echo 'Artifact ID is ${{ steps.artifact-upload-step-id.outputs.artifact-id }}' echo 'Artifact URL is ${{ steps.artifact-upload-step-id.artifact-url }}' echo 'Artifact SHA 256 is ${{ steps.artifact-upload-step-id.artifact-digest }}' upload: outputs: tag-name: ${{ steps.set-tag.outputs.tag }} # 设置一个输出标签。对外包露tag-name,值来自于当前job下steps下id叫set-tag的环境变量tag strategy: matrix: runs-on: [ubuntu-latest] # [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.runs-on }} steps: - id: set-tag # run: echo "tag=release-$(TZ=+8 date '+%Y-%m-%d_%H_%M_%S')" >> $GITHUB_OUTPUT # 把变量输出到github的输出环境,上面的outputs就能获取到 run: echo "tag=release-$(TZ='Asia/Shanghai' date '+%Y-%m-%d_%H_%M_%S')" >> $GITHUB_OUTPUT - name: Create a File run: | echo "$(TZ='Asia/Shanghai' date '+%Y%m%d%H%M%S') hello from ${{ matrix.runs-on }}" > file-${{ matrix.runs-on }}.txt echo '{"version": "1.0.0", "build": "1234"}' > meta.json # 跨job传递参数 - name: Upload Artifact uses: actions/upload-artifact@v3 #github公开版可以用v4 with: name: my-artifact-${{ matrix.runs-on }} path: | file-${{ matrix.runs-on }}.txt meta.json download: needs: upload runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4 with: fetch-depth: 0 - name: Set release tag as date id: vars run: echo "tag=release-$(TZ='Asia/Shanghai' date +'%Y%m%d%H%M%S')" >> $GITHUB_OUTPUT # 包含$GITHUB_ENV 的作用域,全steps也可访问 - name: 自定提交tag run: | git config user.name "github-actions" git config user.email "actions@github.com" git tag -a ${{ steps.vars.outputs.tag }} -m "Automated release" git push origin ${{ steps.vars.outputs.tag }} - name: 获取跨job上游job的输出变量值 run: echo "The tag from previous job is ${{ needs.upload.outputs.tag-name }}" - name: Download All Artifacts uses: actions/download-artifact@v3 #github公开版可以用v4 with: path: my-artifact # pattern: my-artifact-* # 匹配名称 # merge-multiple: true # 合并多个文件,会生成一个新的file-xxxx文件 # 下载其他仓库的制品 # github-token: ${{ secrets.GH_PAT }} # repository: actions/toolkit # run-id: 1234 - run: ls -R my-artifact - name: 读取跨job文件中的变量 run: | # first_meta=$(find ./my-artifact -type f -name 'meta.json' | head -n 1) # 返回./my-artifact/**/xx first_meta=$(find ./my-artifact -type f -name '*.json' -exec realpath {} \; | head -n 1) # 全路径 version=$(jq -r .version "$first_meta") echo "Version is $version" echo "VERSION=$version" >> $GITHUB_ENV #变量在$GITHUB_ENV中存放,可实现同一个steps可以共享 - name: Use shared version run: echo "读取跨job的变量=$VERSION" - name: Flatten directory run: | mkdir release # find all-artifacts -type f -exec cp {} release/ \; find ./my-artifact -type f -exec cp {} release/ \; ls -lah ./release - name: Generate SHA-256 run: | cd release echo "运行操作系统:$RUNNER_OS" for f in *; do if [[ "$RUNNER_OS" == "Windows" ]]; then certutil -hashfile "$f" SHA256 > "$f.sha256" sed -i 's/\r//' "$f.sha256" else shasum -a 256 "$f" > "$f.sha256" fi done cd .. ls -alh ./release/ echo "获取github的环境变量: https://graphite.dev/guides/github-actions-variables ${{ GITHUB_REPOSITORY }} " echo "获取gitea: ${{ gitea.repository }}" - name: release # uses: softprops/action-gh-release@v2这是github的.gitea官方的actions才支持.gitea官方的actions:https://gitea.com/actions uses: akkuman/gitea-release-action@v1 env: #GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # github版本专用 NODE_OPTIONS: '--experimental-fetch' # if nodejs < 18[gitea版本] NODE_TLS_REJECT_UNAUTHORIZED: false # 忽略 ssl 验证错误[gitea版本] if: github.ref_type == 'tag' with: # body_path: ${{ github.workspace }}-CHANGELOG.txt #加载文本的路径,用于传达此版本中的显著变化 # repository: my_gh_org/my_gh_repo # token: ${{ secrets.CUSTOM_GITHUB_TOKEN }} # name: 发布名称。默认为标签名称 # tag_name: 标签名称。默认为github.ref_name #tag_name: ${{ needs.upload.outputs.tag-name }} #tag_name: ${{ steps.vars.outputs.tag }} fail_on_unmatched_files: true name: Release ${{ github.ref_name }} make_latest: true # 指定是否将此版本设置为仓库的最新版本。草稿版和预发布版不能设置为最新版本。可以是true、false或legacy。 draft: false # 指示此版本是否为草稿 prerelease: false # 是否为预发布版本 preserve_order: true # 指示上传资产时是否应保留文件顺序 md5sum: true # gitea版本 sha256sum: true # gitea版本 去掉release/*.sha256 #files: | # release/*.txt files: |- ./release/** - run: ls -R ./release/ - name: Use Go Action id: use-go-action uses: https://gitea.com/actions/release-action@main with: files: |- release/** #api_key: '${{secrets.RELEASE_TOKEN}}'