热门标签 | HotTags
当前位置:  开发笔记 > 运维 > 正文

GithubActions,如何在工作步骤之间共享计算值?

如何解决《GithubActions,如何在工作步骤之间共享计算值?》经验,为你挑选了1个好方法。

Github Actions是否有DRY方式在多个作业步骤中计算和共享值?

在下面的工作流程yml文件中,回显$ {GITHUB_REF} | cut -d'/'-f3`-$ {GITHUB_SHA}会分多个步骤重复。

name: Test, Build and Deploy
on:
  push:
    branches:
      - master
jobs:
  build_and_push:
    name: Build and Push
    runs-on: ubuntu-latest
    steps:
      - name: Docker Build
        uses: "actions/docker/cli@master"
        with:
          args: build . --file Dockerfile -t cflynnus/blog:`echo ${GITHUB_REF} | cut -d'/' -f3`-${GITHUB_SHA}
      - name: Docker Tag Latest
        uses: "actions/docker/cli@master"
        with:
          args: tag cflynnus/blog:`echo ${GITHUB_REF} | cut -d'/' -f3`-${GITHUB_SHA} cflynnus/blog:latest

peterevans.. 5

set-output可用于定义步骤的输出。然后可以将输出用于后续步骤,并在withenv输入部分中进行评估。

The following is what that would look like for your example.

name: Test, Build and Deploy
on:
  push:
    branches:
      - master
jobs:
  build_and_push:
    name: Build and Push
    runs-on: ubuntu-latest
    steps:
      - name: Set tag var
        id: vars
        run: echo ::set-output name=docker_tag::$(echo ${GITHUB_REF} | cut -d'/' -f3)-${GITHUB_SHA}
      - name: Docker Build
        uses: "actions/docker/cli@master"
        with:
          args: build . --file Dockerfile -t cflynnus/blog:${{ steps.vars.outputs.docker_tag }}
      - name: Docker Tag Latest
        uses: "actions/docker/cli@master"
        with:
          args: tag cflynnus/blog:${{ steps.vars.outputs.docker_tag }} cflynnus/blog:latest

Here is another example showing how to dynamically set multiple variables to be used by an action.

      - name: Set output variables
        id: vars
        run: |
          echo ::set-output name=pr_title::"[Test] Add report file $(date +%d-%m-%Y)"
          echo ::set-output name=pr_body::"This PR was auto-generated on $(date +%d-%m-%Y) \
            by [create-pull-request](https://github.com/peter-evans/create-pull-request)."
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v1.5.2
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PULL_REQUEST_TITLE: ${{ steps.vars.outputs.pr_title }}
          PULL_REQUEST_BODY: ${{ steps.vars.outputs.pr_body }}

Alternatively you can use set-env.

      - name: Set environment variables
        run: |
          echo ::set-env name=PR_TITLE::"[Test] Add report file $(date +%d-%m-%Y)"
          echo ::set-env name=PR_BODY::"This PR was auto-generated on $(date +%d-%m-%Y) \
            by [create-pull-request](https://github.com/peter-evans/create-pull-request)."
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v1.5.2
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PULL_REQUEST_TITLE: ${{ env.PR_TITLE }}
          PULL_REQUEST_BODY: ${{ env.PR_BODY }}

Update: The docker actions in the first example are deprecated. Please see this answer for the latest way to work with docker in GitHub Actions.



1> peterevans..:

set-output可用于定义步骤的输出。然后可以将输出用于后续步骤,并在withenv输入部分中进行评估。

The following is what that would look like for your example.

name: Test, Build and Deploy
on:
  push:
    branches:
      - master
jobs:
  build_and_push:
    name: Build and Push
    runs-on: ubuntu-latest
    steps:
      - name: Set tag var
        id: vars
        run: echo ::set-output name=docker_tag::$(echo ${GITHUB_REF} | cut -d'/' -f3)-${GITHUB_SHA}
      - name: Docker Build
        uses: "actions/docker/cli@master"
        with:
          args: build . --file Dockerfile -t cflynnus/blog:${{ steps.vars.outputs.docker_tag }}
      - name: Docker Tag Latest
        uses: "actions/docker/cli@master"
        with:
          args: tag cflynnus/blog:${{ steps.vars.outputs.docker_tag }} cflynnus/blog:latest

Here is another example showing how to dynamically set multiple variables to be used by an action.

      - name: Set output variables
        id: vars
        run: |
          echo ::set-output name=pr_title::"[Test] Add report file $(date +%d-%m-%Y)"
          echo ::set-output name=pr_body::"This PR was auto-generated on $(date +%d-%m-%Y) \
            by [create-pull-request](https://github.com/peter-evans/create-pull-request)."
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v1.5.2
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PULL_REQUEST_TITLE: ${{ steps.vars.outputs.pr_title }}
          PULL_REQUEST_BODY: ${{ steps.vars.outputs.pr_body }}

Alternatively you can use set-env.

      - name: Set environment variables
        run: |
          echo ::set-env name=PR_TITLE::"[Test] Add report file $(date +%d-%m-%Y)"
          echo ::set-env name=PR_BODY::"This PR was auto-generated on $(date +%d-%m-%Y) \
            by [create-pull-request](https://github.com/peter-evans/create-pull-request)."
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v1.5.2
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          PULL_REQUEST_TITLE: ${{ env.PR_TITLE }}
          PULL_REQUEST_BODY: ${{ env.PR_BODY }}

Update: The docker actions in the first example are deprecated. Please see this answer for the latest way to work with docker in GitHub Actions.


推荐阅读
author-avatar
走着走着就丢啦
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有