我想构建一个GitHub工作流程,当另一个存储库创建新的发布时将触发该工作流程。
在文档中,有这样一段话:
问题是:是否有办法引用另一个存储库的
在文档中,有这样一段话:
on.event_name.types
,其中event_name
将是release
。问题是:是否有办法引用另一个存储库的
release
事件?on.event_name.types
,其中event_name
将是release
。release
事件?name: Get latest release version
on:
schedule:
- cron: '0 10 * * *'
jobs:
get-version:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
token: ${{ secrets.REPO_SCOPED_TOKEN }}
- name: Fetch release version
run: |
curl -sL https://api.github.com/repos/swagger-api/swagger-ui/releases/latest | \
jq -r ".tag_name" > release-versions/swagger-ui-latest.txt
- name: Check for modified files
id: git-check
run: echo ::set-output name=modified::$([ -z "`git status --porcelain`" ] && echo "false" || echo "true")
- name: Commit latest release version
if: steps.git-check.outputs.modified == 'true'
run: |
git config --global user.name 'Your Name'
git config --global user.email 'your-email@users.noreply.github.com'
git commit -am "New release version"
git push
然后,您可以创建第二个工作流,仅在检测到目录release-versions
发生更改时运行。
on:
push:
paths:
- 'release-versions/*'
在这个工作流程中,你可以使用保存的版本来获取所需的资产并进行任何需要的处理。
这里是一个类似的示例,它不会立即提交而是发起拉取请求。
$(if git diff-index --quiet HEAD --; then echo "false"; else echo "true"; fi)
我认为最好使用:
$([ -z "`git status --porcelain`" ] && echo "false" || echo "true")
为什么?
git diff-index
将会在文件的修改时间戳发生变化时也返回一个值。这将导致提交失败,因为没有内容被更改。
使用 git status
只有实际内容被更改时才会返回结果。 - KirKone name: Trigger automation tests
steps:
- uses: actions/github-script@v6
with:
github-token: ${{ secrets.PERSONAL_TOKEN }}
script: |
await github.rest.actions.createWorkflowDispatch({
owner: '*****',
repo: '*****',
workflow_id: '*****.yml',
ref: 'main'
})
owner = 组织 / 个人 gitHub。 repo = 目标要运行的存储库。 workflow_id = 目标工作流程的 yml 文件名。 ref = 在目标存储库中运行的分支
createWorkflowDispatch
的任何文档,这个功能还支持吗? - tim-phillipsname: Check for new release of xyz
on:
schedule:
- cron: '0 0 * * *'
workflow_dispatch:
jobs:
check:
runs-on: ubuntu-latest
steps:
- name: Download previous release info
id: download-artifact
uses: dawidd6/action-download-artifact@v2 # It's not possible to use actions/upload-artifact as of the time of writing
with:
name: xyz-release-info
workflow_conclusion: success
workflow: publish.yml
if_no_artifact_found: warn
- name: Get latest release
id: get_release
run: |
# Fetch release information and extract the release tag
RELEASE_TAG=$(curl -s https://api.github.com/repos/USER/xyz/releases/latest | jq -r '.tag_name')
echo "release_tag=$RELEASE_TAG" >> $GITHUB_OUTPUT
echo "latest release: $RELEASE_TAG"
- name: Compare with previous release
id: compare_release
run: |
# Read the release info from the downloaded artifact
PREVIOUS_RELEASE=$(cat xyz-release-info 2> /dev/null || echo "NONE")
echo "previous release: $PREVIOUS_RELEASE"
# Compare the fetched release tag with the previous release tag
if [ "${{ steps.get_release.outputs.RELEASE_TAG }}" != "$PREVIOUS_RELEASE" ]; then
echo "release_changed=true" >> $GITHUB_OUTPUT
echo "Release changed: true"
else
echo "release_changed=false" >> $GITHUB_OUTPUT
echo "Release changed: false"
fi
- name: Call workflow to build code
if: steps.compare_release.outputs.release_changed == 'true'
uses: benc-uk/workflow-dispatch@v1
with:
workflow: publish.yml
您想要触发的工作流程(publish.yml)如下:
name: Build
on:
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
...
steps:
...
- name: Get latest release
id: get_release
run: |
# Fetch release information and extract the release tag
RELEASE_TAG=$(curl -s https://api.github.com/repos/USER/xyz/releases/latest | jq -r '.tag_name')
echo "release_tag=$RELEASE_TAG" >> $GITHUB_OUTPUT
echo "latest release: $RELEASE_TAG"
- name: Store artifacts
run: |
# Store the newly fetched release version in a file
echo "${{ steps.get_release.outputs.RELEASE_TAG }}" > xyz-release-info
echo "Saved ${{ steps.get_release.outputs.RELEASE_TAG }} to xyz-release-info"
- name: Upload new artifacts
uses: actions/upload-artifact@v3
with:
name: xyz-release-info
path: xyz-release-info