-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.yml
86 lines (69 loc) · 2.72 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Copyright 2023 Thomas Schmelzer & Azeem Sajid
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: 'PyPI Token Mint'
description: 'Create API token'
branding:
icon: check-square
color: yellow
inputs:
audience:
description: 'audience'
default: 'pypi'
outputs:
api-token:
description: 'api token'
value: '${{ steps.mint.outputs.api-token }}'
runs:
using: composite
steps:
- id: mint
shell: bash
run: |
echo "Checking the required environment variables"
env_vars_found=true
if [[ -z $ACTIONS_ID_TOKEN_REQUEST_TOKEN ]]; then
echo "ACTIONS_ID_TOKEN_REQUEST_TOKEN is not defined."
env_vars_found=false
fi
if [[ -z $ACTIONS_ID_TOKEN_REQUEST_URL ]]; then
echo "ACTIONS_ID_TOKEN_REQUEST_URL is not defined."
env_vars_found=false
fi
if [[ $env_vars_found == false ]]; then
echo "Make sure that:"
echo "a) Permission are set correctly"
echo "b) The .yml is registered as a trusted publisher"
echo "See https://github.com/tschm/token-mint-action/blob/main/README.md."
exit 1
else
echo "Required environment variables found!"
fi
echo "Fetching OIDC token"
resp=$(curl -sS -H "Authorization: bearer ${ACTIONS_ID_TOKEN_REQUEST_TOKEN}" \
"${ACTIONS_ID_TOKEN_REQUEST_URL}&audience=${{ inputs.audience }}")
# extract the token from the response
oidc_token=$(jq -r '.value' <<< "${resp}")
echo "OIDC token fetched successfully!"
echo "Fetching API token"
# and move the token into json format
pypi_api_json_payload="{\"token\":\"${oidc_token}\"}"
# use the OIDC token to request an api token
resp=$(curl -sS -X POST https://pypi.org/_/oidc/github/mint-token -d "$pypi_api_json_payload")
# extract the token from the response
api_token=$(jq -r '.token' <<< "${resp}")
echo "API token fetched successfully!"
# mask the newly minted API token, so that we don't accidentally leak it
echo "::add-mask::${api_token}"
echo "api-token=$api_token" >> "$GITHUB_OUTPUT"
echo "API token masked and exported successfully!"