|
| 1 | +name: Artifactory Automation |
| 2 | +description: Automates the updating of Chainguard pull tokens and authentication for an Artifactory repository. |
| 3 | + |
| 4 | +inputs: |
| 5 | + # String inputs |
| 6 | + identity: |
| 7 | + description: 'Github assumable identity to Chainguard' |
| 8 | + required: true |
| 9 | + |
| 10 | + organization: |
| 11 | + description: 'Chainguard Organization Name' |
| 12 | + required: true |
| 13 | + |
| 14 | + artifactory_url: |
| 15 | + description: 'The url to your artifactory instance' |
| 16 | + required: true |
| 17 | + |
| 18 | + artifactory_user: |
| 19 | + description: 'The username for artifactory' |
| 20 | + required: true |
| 21 | + |
| 22 | + artifactory_repository_name: |
| 23 | + description: 'The name of the artifactory repository to update' |
| 24 | + required: true |
| 25 | + |
| 26 | + artifactory_token: |
| 27 | + description: 'API token for deployment' |
| 28 | + required: true |
| 29 | + |
| 30 | + ttl: |
| 31 | + description: 'TTL for the token ex: 1h (default is 7 days)' |
| 32 | + required: false |
| 33 | + default: 168h |
| 34 | + |
| 35 | + prune_expired: |
| 36 | + description: 'Prune expired tokens (requires identity.list, identity.delete permissions)' |
| 37 | + required: false |
| 38 | + default: "false" |
| 39 | + |
| 40 | +runs: |
| 41 | + using: "composite" |
| 42 | + steps: |
| 43 | + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 |
| 44 | + |
| 45 | + - name: "Auth" |
| 46 | + uses: chainguard-dev/setup-chainctl@272698817627c158bbd813cb783b62a4b9bbbc67 |
| 47 | + with: |
| 48 | + identity: ${{ inputs.identity }} |
| 49 | + |
| 50 | + - name: Create Token |
| 51 | + shell: bash |
| 52 | + id: create_token |
| 53 | + env: |
| 54 | + ORGANIZATION: ${{ inputs.organization }} |
| 55 | + TTL: ${{ inputs.ttl }} |
| 56 | + PRUNE: ${{ inputs.prune_expired }} |
| 57 | + run: | |
| 58 | + if [[ $PRUNE = "true" ]]; then |
| 59 | + for id in $(chainctl iam identities list --parent "$ORGANIZATION" --name "pull token - registry" --expired -o json | jq -r '.items[]?.id'); do |
| 60 | + echo "Deleting old pull token $id" |
| 61 | + chainctl iam identities delete $id --parent "$ORGANIZATION" -y |
| 62 | + done |
| 63 | + fi |
| 64 | +
|
| 65 | + PULL_TOKEN_JSON=$(chainctl auth pull-token --ttl "$TTL" --output json) |
| 66 | +
|
| 67 | + user=$(jq -r '.identity_id' <<<$PULL_TOKEN_JSON) |
| 68 | + password=$(jq -r '.token' <<<$PULL_TOKEN_JSON) |
| 69 | +
|
| 70 | + echo "user=$user" >> $GITHUB_OUTPUT |
| 71 | + echo "password=$password" >> $GITHUB_OUTPUT |
| 72 | +
|
| 73 | + - name: Update Artifactory Registry |
| 74 | + shell: bash |
| 75 | + id: update-artifactory |
| 76 | + env: |
| 77 | + TOKEN_USER: ${{ steps.create_token.outputs.user }} |
| 78 | + TOKEN_PASSWORD: ${{ steps.create_token.outputs.password }} |
| 79 | + ARTIFACTORY_USER: ${{ inputs.artifactory_user }} |
| 80 | + ARTIFACTORY_TOKEN: ${{ inputs.artifactory_token }} |
| 81 | + ARTIFACTORY_URL: ${{ inputs.artifactory_url }} |
| 82 | + ARTIFACTORY_REPOSITORY_NAME: ${{ inputs.artifactory_repository_name }} |
| 83 | + run: | |
| 84 | + RESP=$(curl -u "$ARTIFACTORY_USER:$ARTIFACTORY_TOKEN" \ |
| 85 | + --silent --write-out "HTTPSTATUS:%{http_code}" \ |
| 86 | + -X POST \ |
| 87 | + "$ARTIFACTORY_URL/artifactory/api/repositories/$ARTIFACTORY_REPOSITORY_NAME" \ |
| 88 | + -H "Content-Type: application/json" \ |
| 89 | + -d '{ |
| 90 | + "key": "'$ARTIFACTORY_REPOSITORY_NAME'", |
| 91 | + "username": "'$TOKEN_USER'", |
| 92 | + "password": "'$TOKEN_PASSWORD'" |
| 93 | + }') |
| 94 | +
|
| 95 | + BODY=$(echo $RESP | sed -e 's/HTTPSTATUS\:.*//g') |
| 96 | + STATUS=$(echo $RESP | tr -d '\n' | sed -e 's/.*HTTPSTATUS://') |
| 97 | +
|
| 98 | + if [ "$STATUS" -ne 200 ]; then |
| 99 | + echo "Error $STATUS": $BODY |
| 100 | + exit 1 |
| 101 | + fi |
| 102 | +
|
| 103 | + echo $BODY |
0 commit comments