标签: github-actions

如何将 Terraform 输出变量保存到 Github Action 的环境变量中

我的项目使用 Terraform 来设置 CI/CD 的基础设施和 Github Actions。运行后,terraform apply我想将 Terraform 输出变量的值保存为 Github Action 环境变量,以供工作流程稍后使用。

根据 Github Action 的文档,是使用工作流命令创建或更新环境变量的方法。

这是我简化的 Github Action 工作流程:

name: Setup infrastructure
jobs:
  run-terraform:
    name: Apply infrastructure changes
    runs-on: ubuntu-latest
    steps:
      ...
      - run: terraform output vm_ip
      - run: echo TEST=$(terraform output vm_ip) >> $GITHUB_ENV
      - run: echo ${{ env.TEST }}
Run Code Online (Sandbox Code Playgroud)

当本地运行时,命令echo TEST_VAR=$(terraform output vm_ip)输出准确TEST="192.168.23.23",但从 Github Action CLI 输出中我得到一些非常奇怪的东西:

在此输入图像描述

我尝试过使用单引号、双引号。在某些时候我改变了策略并尝试使用jq. 因此,我添加了以下步骤,以便将所有 Terraform 输出导出到 json 文件并使用以下命令解析它jq

- …
Run Code Online (Sandbox Code Playgroud)

continuous-integration json environment-variables terraform github-actions

7
推荐指数
2
解决办法
8213
查看次数

是否可以通过 GitHub Actions 在合并过程中更新文件,而无需创建第二次提交?

目前,我正在尝试使用 GitHub Actions 将应用程序的凹凸版本自动化,作为 PR 合并的一部分。有一个自定义脚本可以识别应用程序的当前版本和 PR 附带的标签,并在存储版本号的文件上相应地更改主要|次要|补丁版本。重要的是,版本碰撞仅在合并 PR 时发生,并且作为 GitHub Actions 的一部分,因为它有助于避免版本文件中的合并冲突,并消除了手动碰撞版本的方式。

下面给出了 GitHub Actions 代码片段。

jobs:
  release:
    # Skip on Pull Request Close event.
    if: "!(github.event_name == 'pull_request' && !github.event.pull_request.merged)"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.ref }}

      - id: bumpversion
        if: github.event.pull_request.merged
        env:
          PR_LABELS: ${{ toJson(github.event.pull_request.labels) }}
        run: bash .github/scripts/bump_version.sh -l "${PR_LABELS}"
Run Code Online (Sandbox Code Playgroud)

bump_version.sh脚本具有下面给出的函数,该函数可以进行版本更改,然后将其推送到分支main

  git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"

  echo "commiting the version bump changes..."
  git add .
  git commit -m "Bump …
Run Code Online (Sandbox Code Playgroud)

versioning github pull-request semantic-versioning github-actions

7
推荐指数
1
解决办法
2326
查看次数

如何在github操作中从自托管运行器访问环境变量

我正在将 EC2 作为自托管运行器运行。我已将 AWS_ACCESS_KEY_ID 和 AWS_SECRET_ACCESS_KEY 导出到 EC2,我可以看到它们是用printenv命令设置的。

这样做的原因是我不想将 AWS_ACCESS_KEY_ID 和 AWS_SECRET_ACCESS_KEY 保存在 github 秘密中。

有什么方法可以在我的 github 操作工作流程中访问和使用环境变量(AWS_ACCESS_KEY_ID 和 AWS_SECRET_ACCESS_KEY)?

continuous-integration amazon-ec2 devops github-actions cicd

7
推荐指数
1
解决办法
1654
查看次数

动态 Github 操作步骤

下面显示了典型构建的 Github Actions 选项卡显示的内容:

输出

构建步骤实际上有许多子步骤,但我不想使用 Github Actions 作为脚本语言,只是为了能够谨慎地显示每个子步骤。Github Actions 是否提供了某种魔力来表明您希望构建的可视化显示离散步骤(即“动态步骤”)?

我希望出现类似以下内容的内容,这会导致在 Github Actions 构建可视化的输出中创建离散结果节点:

- name: Dynamic Steps
  run |
     echo "###github-action-step: Step 1"
     echo "###github-action-step: Step 2"
     echo "###github-action-step: Step 3"

Run Code Online (Sandbox Code Playgroud)

github-actions

7
推荐指数
1
解决办法
2964
查看次数

GitHub Actions 的配置:将 PR 合并到 main 时避免运行 CI 两次

我正在使用 GitHub actions 来管理我的 CI,以下事件会触发我的工作流程:

on:
  pull_request:
    branches: main
  push:
    branches: main
Run Code Online (Sandbox Code Playgroud)

我观察到以下“问题”:当我创建 PR 时,CI 就会运行。如果测试通过并且我将其合并到 main 中,则会再次运行测试(在特定情况下我不希望这样做)。如何设置我的工作流程,以便在合并 PR(CI 已通过 PR)时不会触发 CI?

提前致谢!

github github-actions building-github-actions

7
推荐指数
1
解决办法
1478
查看次数

秘密不会从 github 操作秘密传递到 github 操作中的可重用工作流程

我在 github 操作中创建了秘密,并尝试在可重用的工作流程中使用它们,但我无法使其工作,但是,如果我传递从调用者文件中硬编码的秘密,它就可以正常工作

## set_env.yml
name: Sent Env Creds and Vars

on:
  push:
    branches:
      - main
      - dev
  pull_request:
    branches: [ main ]

jobs:
  deploy-dev:
    uses: ./.github/workflows/main.yml
    with:
      AWS_REGION: "us-east-2"
      PREFIX: "dev"
    secrets:
      AWS_ACCESS_KEY_ID: ${{ secrets.DEV_AWS_ACCESS_KEY_ID }}
      AWS_SECRET_ACCESS_KEY: ${{ secrets.DEV_AWS_ACCESS_KEY_ID }}
Run Code Online (Sandbox Code Playgroud)

可重用的工作流程 = main.yml

## main.yml
name: Deploy to AWS  

# Controls when the workflow will run
on:
  workflow_call:
    inputs:
      AWS_REGION:
        required: true
        type: string
      PREFIX:
        required: true
        type: string
    secrets:
      AWS_ACCESS_KEY_ID:
        required: true
      AWS_SECRET_ACCESS_KEY:
        required: true

# A workflow …
Run Code Online (Sandbox Code Playgroud)

github amazon-web-services terraform github-actions

7
推荐指数
1
解决办法
3964
查看次数

Github 操作 + Azure OIDC,任何分支的“主题”值

我正在使用 Github Actions 构建一些要推送到 Azure 容器注册表的 Docker 映像。我正在尝试基于此 GH Action使用 OIDC 作为身份验证机制。我知道该操作支持其他身份验证策略,出于某种原因,我已在我的用例中放弃了这些策略。

根据GH 文档,需要根据 GH 帐户、存储库名称和分支名称填充“主题”字段。然而,想要为多个分支构建 Docker 镜像,这似乎需要每个分支一个联合配置 - 在我看来,这不切实际。

所以我的问题是:有谁知道是否可以(以及如何)设置一个带有“主题”值的联合配置,该值可以作为某种通配符,涵盖给定存储库中的所有分支?

谢谢!

azure azure-active-directory openid-connect github-actions

7
推荐指数
1
解决办法
2044
查看次数

如何使 github 工作流程错误消息显示在日志和注释中?

我有一个工作流程步骤,循环运行 shell 命令的最后阶段:

|| echo "::error Filename $filename doesn't match possible files" && exit 1 ; done

退出被适当触发,但我看到的唯一注释是:

错误:进程已完成,退出代码为 1。

日志显示了整个 shell 命令管道以及相同的错误消息,但不是我的回显错误。

如何获取我的输出,包括 $filename 变量?

github-actions

7
推荐指数
1
解决办法
9803
查看次数

7
推荐指数
1
解决办法
5980
查看次数

在 GitHub Actions 中获取“参数列表太长”

我正在遵循 HashiCorp 的学习指南,了解如何设置 GitHub Actions 和 terraform。除了使用 Terraform 计划更新 PR 的步骤之外,一切都运行良好。

\n

我遇到以下错误:

\n
\nAn error occurred trying to start process \'/home/runner/runners/2.287.1/externals/node12/bin/node\' with working directory \'/home/runner/work/ccoe-aws-ou-scp-manage/ccoe-aws-ou-scp-manage\'. Argument list too long\n\n
Run Code Online (Sandbox Code Playgroud)\n

我正在使用的代码是:

\n
    - uses: actions/github-script@0.9.0\n      if: github.event_name == \'pull_request\'\n      env:\n        PLAN: "terraform\\n${{ steps.plan.outputs.stdout }}"\n      with:\n        github-token: ${{ secrets.GITHUB_TOKEN }}\n        script: |\n          const output = `#### Terraform Format and Style \\`${{ steps.fmt.outcome }}\\`\n          #### Terraform Initialization \xe2\x9a\x99\xef\xb8\x8f\\`${{ steps.init.outcome }}\\`\n          #### Terraform Plan \\`${{ steps.plan.outcome }}\\`\n          <details><summary>Show Plan</summary>\n          \\`\\`\\`${process.env.PLAN}\\`\\`\\`\n          </details>\n          *Pusher: …
Run Code Online (Sandbox Code Playgroud)

github terraform github-actions cicd

7
推荐指数
1
解决办法
5603
查看次数