Skip to content

Commit

Permalink
Move GitHub API curls to ci/gihub-api.sh
Browse files Browse the repository at this point in the history
Reuse methods, make the deploy.yml more readable.

Signed-off-by: Jorge Marques <[email protected]>
  • Loading branch information
gastmaier committed Apr 5, 2024
1 parent 4837b4b commit 6d52762
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 100 deletions.
176 changes: 77 additions & 99 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
name: dist
path: dist

- name: Get head tag and if it is new
- name: Get head tag, if it is new and others vars
run: |
tag=$(sed -n 's/^__version__ = "\(.*\)"/\1/p' adi_doctools/__init__.py)
echo "tag=$tag" >> "$GITHUB_ENV"
Expand All @@ -25,136 +25,114 @@ jobs:
asset_name=adi-doctools.tar.gz
echo "asset_name=$asset_name" >> "$GITHUB_ENV"
- name: Get pre-release vars
run: |
pre_release_id=$(curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ github.repository }}/releases/tags/pre-release \
| jq -r .id)
echo "pre_release_id=$pre_release_id" >> "$GITHUB_ENV"
pre_release_asset_id=$(curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ github.repository }}/releases/$pre_release_id/assets | \
jq -r ".[] | select(.name==\"$asset_name\") | .id")
echo "pre_release_asset_id=$pre_release_asset_id" >> "$GITHUB_ENV"
full_asset_name=$(ls dist/adi-doctools-*.tar.gz)
echo "full_asset_name=$full_asset_name" >> "$GITHUB_ENV"
- name: Get pre-release vars
run: |
source ci/github-api.sh
release_id=$(
gh-get-release-id ${{ secrets.GITHUB_TOKEN }} \
${{ github.repository }} \
pre-release \
pre_release_id
)
asset_id=$(
gh-get-asset-id ${{ secrets.GITHUB_TOKEN }} \
${{ github.repository }} \
$release_id \
pre_release_asset_id
)
- name: Get latest vars
if: ${{ env.new_tag == '1' }}
run: |
release_id=$(curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ github.repository }}/releases/tags/latest \
| jq -r .id)
echo "release_id=$release_id" >> "$GITHUB_ENV"
release_asset_id=$(curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ github.repository }}/releases/$release_id/assets | \
jq -r ".[] | select(.name==\"$asset_name\") | .id")
echo "release_asset_id=$release_asset_id" >> "$GITHUB_ENV"
source ci/github-api.sh
release_id=$(
gh-get-release-id ${{ secrets.GITHUB_TOKEN }} \
${{ github.repository }} \
latest \
latest_id
)
asset_id=$(
gh-get-asset-id ${{ secrets.GITHUB_TOKEN }} \
${{ github.repository }} \
$release_id \
latest_asset_id
)
- name: Create new tag
if: ${{ env.new_tag == '1' }}
run: |
git tag v$tag
git push origin v$tag
- name: Create tag release
if: ${{ env.new_tag == '1' }}
run: |
release_id_new_tag=$(curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/${{ github.repository }}/releases" \
-d "{\"tag_name\":\"v$tag\",\"name\":\"v$tag\",\"make_latest\":\"true\"}" \
| jq -r .id)
echo "release_id_new_tag=$release_id_new_tag" >> "$GITHUB_ENV"
- name: Upload tag release asset
- name: Create tag release and upload asset
if: ${{ env.new_tag == '1' }}
run: |
curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Content-Type: application/octet-stream" \
"https://uploads.github.com/repos/${{ github.repository }}/releases/$release_id_new_tag/assets?name=$asset_name" \
--data-binary "@$full_asset_name"
source ci/github-api.sh
release_id=$(
gh-create-release ${{ secrets.GITHUB_TOKEN }} \
${{ github.repository }} \
$tag \
new_tag_release_id
)
gh-upload-asset ${{ secrets.GITHUB_TOKEN }} \
${{ github.repository }} \
$release_id \
$asset_name \
$full_asset_name
- name: Update pre-release target commitish
run: |
curl -s \
-X PATCH \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-d '{"target_commitish": "${{ github.sha }}"}' \
"https://api.github.com/repos/${{ github.repository }}/releases/$pre_release_id"
source ci/github-api.sh
gh-update-commitish ${{ secrets.GITHUB_TOKEN }} \
${{ github.repository }} \
${{ github.sha }} \
$pre_release_id
- name: Upload pre-release asset
run: |
source ci/github-api.sh
if [[ ! "$pre_release_asset_id" == "" ]]; then
curl -L \
-X DELETE \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/${{ github.repository }}/releases/assets/$pre_release_asset_id"
gh-delete-asset ${{ secrets.GITHUB_TOKEN }} \
${{ github.repository }} \
$pre_release_asset_id
fi
curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Content-Type: application/octet-stream" \
"https://uploads.github.com/repos/${{ github.repository }}/releases/$pre_release_id/assets?name=$asset_name" \
--data-binary "@$full_asset_name"
gh-upload-asset ${{ secrets.GITHUB_TOKEN }} \
${{ github.repository }} \
$pre_release_id \
$asset_name \
$full_asset_name
- name: Update latest target commitish
if: ${{ env.new_tag == '1' }}
run: |
curl -s \
-X PATCH \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-d '{"target_commitish": "${{ github.sha }}"}' \
"https://api.github.com/repos/${{ github.repository }}/releases/$release_id"
source ci/github-api.sh
gh-update-commitish ${{ secrets.GITHUB_TOKEN }} \
${{ github.repository }} \
${{ github.sha }} \
$latest_id
- name: Upload latest asset
if: ${{ env.new_tag == '1' }}
run: |
if [[ ! "$release_asset_id" == "" ]]; then
curl -L \
-X DELETE \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/${{ github.repository }}/releases/assets/$release_asset_id"
source ci/github-api.sh
if [[ ! "$latest_asset_id" == "" ]]; then
gh-delete-asset ${{ secrets.GITHUB_TOKEN }} \
${{ github.repository }} \
$latest_asset_id
fi
curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Content-Type: application/octet-stream" \
"https://uploads.github.com/repos/${{ github.repository }}/releases/$release_id/assets?name=$asset_name" \
--data-binary "@$full_asset_name"
gh-upload-asset ${{ secrets.GITHUB_TOKEN }} \
${{ github.repository }} \
$latest_id \
$asset_name \
$full_asset_name
- name: Switch to gh-pages and empty it
run: |
Expand Down
2 changes: 1 addition & 1 deletion adi_doctools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .role import setup as role_setup
from .lut import get_lut

__version__ = "0.3.24"
__version__ = "0.3.25"

logger = logging.getLogger(__name__)

Expand Down
72 changes: 72 additions & 0 deletions ci/github-api.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/bin/bash

gh-get-release-id()
{
release_id=$(curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $1" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/$2/releases/tags/$3 \
| jq -r .id)
echo "$4=$release_id" >> "$GITHUB_ENV"
echo $release_id
}

gh-get-asset-id()
{
asset_id=$(curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $1" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/$2/releases/$3/assets | \
jq -r ".[] | select(.name==\"$asset_name\") | .id")
echo "$4=$asset_id" >> "$GITHUB_ENV"
echo $asset_id
}

gh-create-release()
{
release_id=$(curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $1" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/$2/releases" \
-d "{\"tag_name\":\"v$3\",\"name\":\"v$3\",\"make_latest\":\"true\"}" \
| jq -r .id)
echo "$4=$release_id" >> "$GITHUB_ENV"
echo $release_id
}

gh-upload-asset()
{
curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $1" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Content-Type: application/octet-stream" \
"https://uploads.github.com/repos/$2/releases/$3/assets?name=$4" \
--data-binary "@$5"
}

gh-delete-asset()
{
curl -L \
-X DELETE \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $1" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/$2/releases/assets/$3"
}

gh-update-commitish()
{
curl -s \
-X PATCH \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $1" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-d '{"target_commitish": "$3"}' \
"https://api.github.com/repos/$2/releases/$4"
}

0 comments on commit 6d52762

Please sign in to comment.