gpt4 book ai didi

github - 如何在两个连续的 GitHub Actions 作业之间传递变量?

转载 作者:行者123 更新时间:2023-12-02 00:58:10 35 4
gpt4 key购买 nike

我正在尝试编写一个工作流程,让标签创建工作流程填充我的 GitHub 项目版本。

我已经知道如何创建版本(使用 actions/create-release@v1.0.0)以及如何在版本中推送工件(使用 actions/upload-release- Assets )。

但是,由于我正在构建 Rust 代码,因此我必须在不同的平台上编译它。显然,为此,我在每个平台上都有一份工作,并且我正在努力将我的工件推向该工作。

但是为了让推送工作,我必须使用 actions/create-release@v1.0.0 给出的发布标识符,它在另一个作业中运行。

因此我的问题是:如何将发布 URL 从发布创建作业传递到将推送工件的作业?

完整的工作流程可以在这里找到: https://github.com/Riduidel/rrss2imap/blob/master/.github/workflows/on_tag.yml

我把它复制到这里

name: Push release artifacts on tag

on:
push:
tags:
- '[0-9]+.[0-9]+.[0-9]+'

jobs:
Make_GitHub_Release:
name: Create Release
runs-on: ubuntu-latest
steps:
- name: Create Release
id: create_release
uses: actions/create-release@v1.0.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
draft: true
prerelease: false
Standard_OS_build:
name: Build on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
# os: [ubuntu-latest, macOS-latest]
steps:
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- uses: actions/checkout@master
# see https://github.com/marketplace/actions/rust-cargo
- uses: actions-rs/cargo@v1
with:
command: build
args: --release --all-features
- uses: olegtarasov/get-tag@v1
- name: Upload matrix release asset
id: upload-release-asset
uses: actions/upload-release-asset@v1.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
# Doesn't work, since I can't access the first job from this one
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_name: rrss2imap_${{ matrix.os }}
asset_path: target/release/rrss2imap
asset_content_type: application/octet-stream
needs: Make_GitHub_Release
Windows_build:
name: Build on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-latest]
steps:
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- uses: actions/checkout@master
# see https://github.com/marketplace/actions/rust-cargo
- uses: actions-rs/cargo@v1
with:
command: build
args: --release --all-features
- uses: olegtarasov/get-tag@v1
- name: Upload Windows asset
id: upload-release-asset
uses: actions/upload-release-asset@v1.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
# Doesn't work, since I can't access the first job from this one
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_name: rrss2imap_${{ matrix.os }}
asset_path: target/release/rrss2imap.exe
asset_content_type: application/vnd.microsoft.portable-executable
needs: Make_GitHub_Release
Cross_build_for_Raspbian:
name: Build for Raspbian
runs-on: ubuntu-latest
steps:
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: armv7-unknown-linux-gnueabihf
override: true
- uses: actions/checkout@master
# see https://github.com/marketplace/actions/rust-cargo
- uses: actions-rs/cargo@v1
with:
use-cross: true
command: build
args: --release --all-features
- uses: olegtarasov/get-tag@v1
- name: Upload matrix release asset
id: upload-release-asset
uses: actions/upload-release-asset@v1.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
# Doesn't work, since I can't access the first job from this one
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_name: rrss2imap_${{ matrix.os }}
asset_path: target/release/rrss2imap
asset_content_type: application/octet-stream
needs: Make_GitHub_Release

最佳答案

一种可能的解决方案是在创建版本后创建一个 repository_dispatch 事件。您可以传递包含构建所需变量的有效负载。

按如下方式更改您的工作流程。使用 repo 范围 Personal Access Token名为 REPO_ACCESS_TOKEN

name: Push release artifacts on tag
on:
push:
tags:
- '[0-9]+.[0-9]+.[0-9]+'
jobs:
Make_GitHub_Release:
name: Create Release
runs-on: ubuntu-latest
steps:
- name: Create Release
id: create_release
uses: actions/create-release@v1.0.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
draft: true
prerelease: false
- name: Dispatch Builds
uses: peter-evans/repository-dispatch@v1.0.0
with:
token: ${{ secrets.REPO_ACCESS_TOKEN }}
event-type: builds
client-payload: '{"release_url": "${{ steps.create_release.outputs.upload_url }}", "ref": "${{ github.ref }}"}'

或者使用curl来调用GitHub API而不是操作。

      - name: Dispatch Builds
run: |
curl -XPOST \
-u "peter-evans:${{ secrets.REPO_ACCESS_TOKEN }}" \
-H "Accept: application/vnd.github.everest-preview+json" \
-H "Content-Type: application/json" https://api.github.com/repos/${{ github.repository }}/dispatches \
--data '{"event_type": "builds", "client_payload": {"release_url": "${{ steps.create_release.outputs.upload_url }}", "ref": "${{ github.ref }}"}}'

然后添加如下工作流程来处理您的构建。 (在此示例中,我从您的工作流程中删除了大部分细节)

on:
repository_dispatch:
types: [builds]
jobs:
Standard_OS_build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
with:
ref: ${{ github.event.client_payload.ref }}
- name: Check release URL
run: |
echo "${{ github.event.client_payload.release_url }}"
Windows_build:
runs-on: windows-latest
steps:
- uses: actions/checkout@master
with:
ref: ${{ github.event.client_payload.ref }}
- name: Check release URL
run: |
echo "${{ github.event.client_payload.release_url }}"

关于github - 如何在两个连续的 GitHub Actions 作业之间传递变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58738070/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com