Home > Software design >  How to set and access response of python code in next step in github workflow
How to set and access response of python code in next step in github workflow

Time:10-14

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 }}"

CodePudding user response:

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 }}"
  • Related