2

How to set and access the response of python code to a variable in github workflow. I have to use the token which is generated from the python code in the step Create container web in the Auth_Header

- name: setup python
    uses: actions/setup-python@v2
    with:
      python-version: '3.9.0'

  - name: Get Token
    run: |
      python -m pip install --upgrade pip
      pip install requests-oauthlib
      pip install Authlib
      python -c 'from authlib.integrations.requests_client import OAuth2Session;
      session = OAuth2Session("${{ env.CLIENT_ID }}", "${{ env.CLIENT_SECRET }}")
      session.fetch_token("${{ env.TOKEN_ENDPOINT }}")
      session.token["access_token"]'

  - name: Create Container Web
      #if: steps.pr-label.outputs.result == 'true'
    run: |
        AUTH_HEADER="Authorization: token $access_token"

so far what I have tried is below but still it is not working

- name: Get Token
        env:
          ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true'
        run: |
          python -m pip install --upgrade pip
          pip install requests-oauthlib
          pip install Authlib
          echo ::set-env name=ACCESS_TOKEN::$(python -c 'from authlib.integrations.requests_client import OAuth2Session;
          session = OAuth2Session("${{ env.CLIENT_ID }}", "${{ env.CLIENT_SECRET }}")
          session.fetch_token("${{ env.TOKEN_ENDPOINT }}")
          session.token["access_token"]')
        id: token

      - name: Create Container Web
          #if: steps.pr-label.outputs.result == 'true'
        run: |
            echo token is ${{ env.ACCESS_TOKEN }}
            AUTH_HEADER="Authorization: token ${{ env.ACCESS_TOKEN }}"

1 Answer 1

2

You could set an output using ::set-output and then get this output back in the next step using steps.[id].outputs.ACCESS_TOKEN:

name: Token

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Get Token
        env:
          ACTIONS_ALLOW_UNSECURE_COMMANDS: 'true'
        run: |
          python -m pip install --upgrade pip
          pip install requests-oauthlib
          pip install Authlib
          echo ::set-output name=ACCESS_TOKEN::$(python 'from authlib.integrations.requests_client import OAuth2Session;
          session = OAuth2Session("${{ env.CLIENT_ID }}", "${{ env.CLIENT_SECRET }}")
          session.fetch_token("${{ env.TOKEN_ENDPOINT }}")
          print(session.token["access_token"])')
        id: token
      - name: Create Container Web
        run: |
            echo token is ${{ steps.token.outputs.ACCESS_TOKEN }}
            AUTH_HEADER="Authorization: token ${{ steps.token.outputs.ACCESS_TOKEN }}"
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Bertrand it worked like a charm!! do I need to use print statement at the end..
@Tim the print statement is to send the value to stdout so that the result of $(...) will be "token"
is it safe to use print in the console
@Tim what do you mean by safe? for the access token ? I would say that it's as safe as echo but the risk would be script injection if it outputs some command like "ls", it will resolve to $("ls") etc... The risk is the same as echo ${{ ..... }}. You need to check that session.token["access_token"] is a valid formatted access token for example. See this doc about the risk of script injections.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.