作业工作流程文档告诉我们还有一种在内部使用表达式的方法url field https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idenvironment:
environment:
name: test_environment
url: ${{ steps.step_name.outputs.url_output }}
现在想象一个ci.yml
该工作流程文件使用 AWS CLI 将静态网站部署到 S3,其中我们使用 Pulumi 等工具在我们的 AWS 账户内动态创建 S3 存储桶。我们可以使用以下命令读取动态创建的S3 urlpulumi stack output bucketName
。里面的部署步骤ci.yml
那么可能看起来像这样:
- name: Deploy Nuxt.js generated static site to S3 Bucket via AWS CLI
id: aws-sync
run: |
aws s3 sync ../dist/ s3://$(pulumi stack output bucketName) --acl public-read
echo "s3_url=http://$(pulumi stack output bucketUrl)" >> $GITHUB_OUTPUT
working-directory: ./deployment
这里有两个关键点:首先我们应该使用id
在部署步骤中定义一个步骤名称,我们可以通过以下方式轻松访问step_name
反了environment:url
。第二我们需要定义一个步骤输出 https://stackoverflow.com/a/57989070/4964553 using echo "s3_url=http://$(pulumi stack output bucketUrl)" >> $GITHUB_OUTPUT
。在这个例子中我创建了一个变量s3_url
。你可以更换pulumi stack output bucketUrl
与您想要的任何其他命令或您使用的工具一起使用,它会响应您的动态环境 url。
还请务必添加一个http://
or https://
为了防止出现这样的错误消息:
Environment URL 'microservice-ui-nuxt-js-hosting-bucket-bc75fce.s3-website.eu-central-1.amazonaws.com' is not a valid http(s) URL, so it will not be shown as a link in the workflow graph.
Now the environment
定义在我们的顶部ci.yml
可以访问s3_url
我们的部署步骤的输出变量如下所示:
jobs:
ci:
runs-on: ubuntu-latest
environment:
name: microservice-ui-nuxt-js-deployment
url: ${{ steps.aws-sync.outputs.s3_url }}
steps:
- name: Checkout
...
Using steps.aws-sync
我们直接引用部署步骤,因为我们使用id
。所附的.outputs.s3_url
然后直接引用包含我们的 S3 url 的变量。如果您正确定义了所有内容,GitHub Actions UI 将在已完成的作业正下方呈现环境 URL:
这里还有嵌入示例项目中的完整工作流程 https://github.com/jonashackt/microservice-ui-nuxt-js/blob/main/.github/workflows/static-site-pulumi-aws-s3-deploy.yml.
Update: The set-output name=VARNAME::VALUE
符号已弃用 https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/并产生以下输出(感谢 Rob 指出这一点!):
Warning: The `set-output` command is deprecated and will be disabled soon. Please upgrade to using Environment Files. For more information see: https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/