Get the current pushed tag in Github Actions

会有一股神秘感。 提交于 2020-08-21 05:07:28

问题


Is there a way to access the current tag that has been pushed in a Github Action? In CircleCI you can access this value with the $CIRCLE_TAG variable.

My Workflow yaml is being triggered by a tag like so:

on:
  push:
    tags:
      - 'v*.*.*'

And I want to use that version number as a file path later on in the workflow.

I have included my final solution based on the chosen answer as another answer below: https://stackoverflow.com/a/58195087/756514


回答1:


As far as I know there is no tag variable. However, it can be extracted from GITHUB_REF which contains the checked out ref, e.g. refs/tags/v1.2.3

Try this workflow. It creates a new environment variable with the extracted version that you can use in later steps.

on:
  push:
    tags:
      - 'v*.*.*'
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: Set env
        run: echo ::set-env name=RELEASE_VERSION::${GITHUB_REF#refs/*/}
      - name: Test
        run: |
          echo $RELEASE_VERSION
          echo ${{ env.RELEASE_VERSION }}

Alternatively, use set-output:

on:
  push:
    tags:
      - 'v*.*.*'
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: Set output
        id: vars
        run: echo ::set-output name=tag::${GITHUB_REF#refs/*/}
      - name: Check output
        env:
          RELEASE_VERSION: ${{ steps.vars.outputs.tag }}
        run: |
          echo $RELEASE_VERSION
          echo ${{ steps.vars.outputs.tag }}



回答2:


Here's a workflow run showing that the GITHUB_REF environment variable contains refs/tags/v0.0.2:

https://github.com/rmunn/Testing/runs/242676390

I ran that by creating the tag, then doing git push origin v0.0.2.

Here's a snippet of the workflow you see in that log:

steps:
- uses: actions/checkout@v1
- name: Dump GitHub context
  env:
    GITHUB_CONTEXT: ${{ toJson(github) }}
  run: echo "$GITHUB_CONTEXT"
  if: runner.os != 'Windows'
- name: Show GitHub ref
  run: echo "$GITHUB_REF"
  if: runner.os != 'Windows'
- name: Dump event JSON
  env:
    EVENT_JSON_FILENAME: ${{ github.event_path }}
  run: cat "$EVENT_JSON_FILENAME"
  if: runner.os != 'Windows'

Since that log will eventually be deleted (I don't know how long Github Actions logs are retained, but it's surely not forever), here's a screenshot for evidence.




回答3:


So thanks to all the help from @peterevans I managed to achieve the result I wanted which was:

  • to tag a commit
  • push the tag to trigger the github action
  • github action sets the git tag as an env var
  • run install & build
  • use chrislennon/action-aws-cli action to install aws cli using secrets for keys
  • run command to sync the build to a new S3 bucket using the tag env var as the dir name

Here is an example of the what I ran using Chris Lennon's action:

on:
  push:
    tags:
      - 'v*.*.*'
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: Set env
        run: echo ::set-env name=RELEASE_VERSION::$(echo ${GITHUB_REF:10})
      - name: yarn install & build
        run: |
          yarn install
          yarn build
      - uses: chrislennon/action-aws-cli@v1.1
      - name: Publish to AWS S3
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_S3_BUCKET: ${{ secrets.AWS_S3_BUCKET }}
        run: aws s3 sync dist s3://$AWS_S3_BUCKET/$RELEASE_VERSION/ --acl public-read


来源:https://stackoverflow.com/questions/58177786/get-the-current-pushed-tag-in-github-actions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!