How to retrieve the cloud foundry oauth token from a devops deploy stage for setting up auto scaling?

孤人 提交于 2020-01-06 06:08:51

问题


I'm trying to get the cloud foundry oauth-token from a devops pipeline deploy stage:

...
cf push $CF_APP
...

accessToken=$(cf oauth-token | grep bearer | sed -e s/bearer/Bearer/g)
echo accessToken=$accessToken
...
# use token in Auto Scaling API call ...
curl $createPolicyUrl -X 'PUT' -H 'Content-Type:application/json' \
     -H 'Accept:application/json' \
     -H "Authorization:$accessToken" \
     --data-binary @${policyJson} \
     -s -o response.txt -w '%{http_code}\n'

The output from the echo command is:

accessToken=

How can I retrieve the oauth token?

Note that cf push works ok in the script ecen though there isn't a cf login performed in the deploy script. Therefore, I'm assuming cf oauth-token would not need login either. Is this a valid assumption?

Update: I added cf login to my deploy script:

...
cf push $CF_APP
...

cf login
accessToken=$(cf oauth-token | grep bearer | sed -e s/bearer/Bearer/g)
echo accessToken=$accessToken
...

The output:

cf login not allowed.

See also my similar question on reconfiguring availability monitoring in a devops deploy stage.


回答1:


Make sure to do a cf login to log in before you run the cf oauth-token command. Also make sure to double quote "Authorization:$accessToken" so the variable is substituted.

Update: It looks like you can access the oauth-token from within the script via the $CF_TOKEN environment variable. The token is associated with the owner of the pipeline, not the user running the current pipeline stage.




回答2:


Can you try

accessToken=$(cf oauth-token)
CF_TOKEN=$(echo $accessToken | grep “Bearer*” | perl -wpe ‘s/.*(Bearer .+)/$1/‘)
CF_TOKEN should now have the token value


来源:https://stackoverflow.com/questions/44668912/how-to-retrieve-the-cloud-foundry-oauth-token-from-a-devops-deploy-stage-for-set

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