jon*_*ckt 13 environment github github-actions
我发现了一个非常好的 GitHub Actions 功能,名为“环境”。使用适当的语法,也可以在 GitHub Action 中创建环境,workflow.yml如下所示:
environment:
name: test_environment
url: https://your-apps-url-here.com
Run Code Online (Sandbox Code Playgroud)
正如文档所述,这是创建 GitHub 操作环境的有效方法:
运行引用不存在的环境的工作流将创建具有引用名称的环境。
但在我当前的 GitHub Action 工作流程中,有没有一种方法可以url根据部署步骤输出动态设置?我有一个由 AWS 部署过程产生的动态 URL,我无法预先定义它。
jon*_*ckt 16
作业工作流程文档告诉我们,还有一种在url字段内使用表达式的方法:
environment:
name: test_environment
url: ${{ steps.step_name.outputs.url_output }}
Run Code Online (Sandbox Code Playgroud)
现在想象一个ci.yml使用 AWS CLI 将静态网站部署到 S3 的工作流程文件,其中我们使用 Pulumi 等工具在我们的 AWS 账户内动态创建 S3 存储桶。我们可以使用以下命令读取动态创建的 S3 url pulumi 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
Run Code Online (Sandbox Code Playgroud)
这里有两个关键点:首先,我们应该id在部署步骤中使用来定义一个步骤名称,我们可以step_name通过environment:url. 其次,我们需要使用定义一个步骤输出echo "s3_url=http://$(pulumi stack output bucketUrl)" >> $GITHUB_OUTPUT。在此示例中,我创建了一个变量s3_url。您可以将 替换pulumi stack output bucketUrl为您想要的任何其他命令或您使用的工具,它会响应您的动态环境 url。
还要确保添加一个http://或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.
Run Code Online (Sandbox Code Playgroud)
现在,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
...
Run Code Online (Sandbox Code Playgroud)
使用steps.aws-sync我们直接引用部署步骤,因为我们使用id. 然后附加的内容.outputs.s3_url直接引用包含我们的 S3 url 的变量。如果您正确定义了所有内容,GitHub Actions UI 将在已完成的作业正下方呈现环境 URL:
这也是嵌入在示例项目中的完整工作流程。
更新:该set-output name=VARNAME::VALUE表示法已被弃用,并产生以下输出(感谢 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/
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12138 次 |
| 最近记录: |