Skip to content

Create release

Create release #78

name: Create release
on:
workflow_dispatch:
inputs:
commit_url:
required: true
type: string
description: 'URL of commit for which release will be created'
tag_name:
required: true
type: string
description: 'The tag name of the release with leading v'
release_type:
description: 'Choose relase type'
default: latest
required: false
type: choice
options:
- latest
- prerelase
- regular
draft:
description: 'Draft release'
required: true
default: false
type: boolean
autoChangelog:
description: 'Auto generated release notes'
required: true
default: true
type: boolean
changeLog:
description: 'Custom release notes'
required: false
type: string
env:
COMMIT_URL: ${{ inputs.commit_url }}
TAG_NAME: ${{ inputs.tag_name }}
RELEASE_TYPE: ${{ inputs.release_type }}
DRAFT: ${{ inputs.draft }}
AUTO_CHANGELOG: ${{ inputs.autoChangelog }}
CHANGELOG: ${{ inputs.changeLog }}
RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
jobs:
display-parameters:
runs-on: ubuntu-latest
outputs:
latest: ${{ steps.step1.outputs.latest }}
prerelease: ${{ steps.step1.outputs.prerelase }}
target_repo: ${{ steps.step1.outputs.repo_name }}
commit_hash: ${{ steps.step1.outputs.commit_hash }}
steps:
- name: Display parameters
id: step1
run: |
if [[ $RELEASE_TYPE == "prerelase" ]]; then
PRE_RELEASE=true
LATEST=false
elif [[ $RELEASE_TYPE == "latest" ]]; then
PRE_RELEASE=false
LATEST=true
elif [[ $RELEASE_TYPE == "regular" ]]; then
PRE_RELEASE=false
LATEST=false
fi
# Extract target repository and commit hash from commit URL
TARGET_REPO=$(echo $COMMIT_URL | awk -F'/' '{print $(NF-2)}')
COMMIT_HASH=$(echo $COMMIT_URL | awk -F'/' '{print $NF}')
# Set outputs
echo "latest=$LATEST" >> "$GITHUB_OUTPUT"
echo "prerelase=$PRE_RELEASE" >> "$GITHUB_OUTPUT"
echo "repo_name=$TARGET_REPO" >> "$GITHUB_OUTPUT"
echo "commit_hash=$COMMIT_HASH" >> "$GITHUB_OUTPUT"
echo "Target repository: $TARGET_REPO" >> $GITHUB_STEP_SUMMARY
echo "Commit hash: $COMMIT_HASH" >> $GITHUB_STEP_SUMMARY
echo "Tag name: $TAG_NAME" >> $GITHUB_STEP_SUMMARY
echo "Draft: $DRAFT" >> $GITHUB_STEP_SUMMARY
echo "Auto changelog: $AUTO_CHANGELOG" >> $GITHUB_STEP_SUMMARY
echo "Custom changelog: $CHANGELOG" >> $GITHUB_STEP_SUMMARY
echo "Release type: $RELEASE_TYPE" >> $GITHUB_STEP_SUMMARY
echo "Pre-release: $PRE_RELEASE" >> $GITHUB_STEP_SUMMARY
echo "Latest: $LATEST" >> $GITHUB_STEP_SUMMARY
echo "Commit url: $COMMIT_URL" >> $GITHUB_STEP_SUMMARY
create-release:
runs-on: ubuntu-latest
environment: production
needs: display-parameters
env:
COMMIT_HASH: ${{ needs.display-parameters.outputs.commit_hash }}
TARGET_REPO: ${{ needs.display-parameters.outputs.target_repo }}
steps:
- name: Check exitence of commit hash and tag name in target repo
run: |
# Check if commit hash exists in target repo
CHECK_COMMIT_HASH=$(curl -s -o /dev/null -w "%{http_code}" --location "https://api.github.com/repos/pimcore/$TARGET_REPO/commits/$COMMIT_HASH" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $RELEASE_TOKEN")
if [[ $CHECK_COMMIT_HASH -ne 200 ]]; then
echo "Non existing commit hash: $COMMIT_HASH" >> $GITHUB_STEP_SUMMARY
exit 1
fi
# Check if tag name exists in target repo
CHECK_TAG_NAME=$(curl -s -o /dev/null -w "%{http_code}" --location "https://api.github.com/repos/pimcore/$TARGET_REPO/git/refs/tags/$TAG_NAME" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $RELEASE_TOKEN")
if [[ $CHECK_TAG_NAME -eq 200 ]]; then
echo "Tag name already exists: $TAG_NAME" >> $GITHUB_STEP_SUMMARY
exit 1
fi
- name: Create a release in target repo
env:
LATEST: ${{ needs.display-parameters.outputs.latest }}
PRE_RELEASE: ${{ needs.display-parameters.outputs.prerelease }}
COMMIT_HASH: ${{ needs.display-parameters.outputs.commit_hash }}
TARGET_REPO: ${{ needs.display-parameters.outputs.target_repo }}
run: |
# Create release in target repo with default release notes
# Remove leading v from tag name
RELEASE_NAME=${TAG_NAME:1}
REQ_BODY=$(cat << EOF
{
"target_commitish": "$COMMIT_HASH",
"name": "$RELEASE_NAME",
"draft": $DRAFT,
"make_latest": "$LATEST",
"prerelease": $PRE_RELEASE,
"tag_name": "$TAG_NAME",
"generate_release_notes": $AUTO_CHANGELOG,
"body": "$CHANGELOG"
}
EOF
)
CREATE_RELEASE=$(curl -v -w "\nHTTP_STATUS:%{http_code}" -X POST --location "https://api.github.com/repos/pimcore/$TARGET_REPO/releases" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $RELEASE_TOKEN" \
-d "$REQ_BODY")
HTTP_STATUS=$(echo "$CREATE_RELEASE" | awk -F: '/HTTP_STATUS:/ {print $2}')
if [[ $HTTP_STATUS -ne 201 ]]; then
echo "Failed to create release. HTTP Response: $HTTP_STATUS"
echo "Please create release manually..."
exit 1
else
RELEASE_URL=$(echo "$CREATE_RELEASE" | awk -F'"' '/"html_url":/ {print $4; exit}')
echo "Release created successfully: $RELEASE_URL" >> $GITHUB_STEP_SUMMARY
fi