Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: 🎡 Publish packages via GitHub Release #8

Merged
merged 8 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/actions/upload-tar-artifact/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
name: "Upload Tar Artifact"
description: "Upload an artifact and zips it as {name}.tar.gz"
inputs:
name:
description: "Name for artifact"
required: true
path:
description: "Path to zip"
required: true
runs:
using: "composite"
steps:
- name: 📦 Pack build as tar
run: tar -czf ${{ inputs.name }} ${{ inputs.path }}
shell: bash

- name: ⬆ Upload build
uses: actions/upload-artifact@v3
with:
name: ${{ inputs.name }}
path: ${{ inputs.name }}
22 changes: 22 additions & 0 deletions .github/scripts/get-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

if [[ $GITHUB_REF == refs/tags/v* ]]
then
if [[ $GITHUB_ACTOR != 'dependabot[bot]' ]]
then

if [[ $GITHUB_PRE_RELEASE == true ]]
then
echo "PRE_RELEASE"
elif [[ $GITHUB_COMMITISH == 'main' ]]
then
echo "RELEASE"
fi
else
echo "Dependabot has no permission to publish!"
exit 1
fi
else
echo "Your tag has to start with 'v'"
exit 1
fi
20 changes: 20 additions & 0 deletions .github/scripts/package-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

SEMVER_VERSION=$(npx find-versions-cli "$TAG")
if [[ $RELEASE == "true" ]]
then
if [[ $SEMVER_VERSION == *-* ]]
then
echo "Version $SEMVER_VERSION contains hyphen, maybe you forgot to check the prerelease checkbox in github. A release should not have a hyphen!"
exit 1
fi
echo "$SEMVER_VERSION"
elif [[ $PRE_RELEASE == "true" ]]
then
GITHUB_SHA_SHORT=$(echo "$GITHUB_SHA" | cut -c1-7)
VALID_SEMVER_VERSION=$(echo "$SEMVER_VERSION"-"$GITHUB_SHA_SHORT")
echo "$VALID_SEMVER_VERSION"
else
echo "nothing found in environment for RELEASE or PRE_RELEASE"
exit 1
fi
43 changes: 43 additions & 0 deletions .github/scripts/publish-npm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash

if [[ -z $VALID_SEMVER_VERSION ]]; then
echo "Version is missing!"
exit 1
fi

if [[ $RELEASE == 'false' && $PRE_RELEASE == 'false' ]]; then
echo "RELEASE and PRE_RELEASE are false, there should be an error in the pipeline!"
exit 1
fi

echo "🛠 Forge all packages version numbers"
echo "which package version ?: $VALID_SEMVER_VERSION"

npm version --no-git-tag-version "$VALID_SEMVER_VERSION"

echo "📦 Create packages"
npm pack --quiet

TAG="latest"
if [[ $PRE_RELEASE == 'true' ]]; then
TAG="next"
fi

echo "📰 Publish Package to Registry with tag: $TAG)"
for REGISTRY in 'GITHUB' 'NPM'
do
echo "🔒 Authenticate $REGISTRY NPM Registry"
if [[ $REGISTRY == 'GITHUB' ]]; then
npm config set @db-ui:registry https://npm.pkg.github.com
npm set //npm.pkg.github.com/:_authToken "$GPR_TOKEN"
echo "🔑 Authenticated with GITHUB"
elif [[ $REGISTRY == 'NPM' ]]; then
npm config set @db-ui:registry https://registry.npmjs.org/
npm set //registry.npmjs.org/:_authToken "$NPM_TOKEN"
echo "🔑 Authenticated with NPM"
else
echo "Could not authenticate with $REGISTRY"
exit 1
fi
npm publish --dry-run --tag "$TAG" db-ui-base-"$VALID_SEMVER_VERSION".tgz
done
4 changes: 3 additions & 1 deletion .github/workflows/01-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ jobs:
uses: actions/upload-artifact@v3
with:
name: build
path: public
path: |
public
build
70 changes: 70 additions & 0 deletions .github/workflows/01-get-publish-version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
name: Get and save publish version

on:
workflow_call:
# Map the workflow outputs to job outputs
outputs:
release:
description: "If the current tag is a release"
value: ${{ jobs.publish.outputs.release }}
preRelease:
description: "If the current tag is a pre-release"
value: ${{ jobs.publish.outputs.preRelease }}
version:
description: "Which version has the tag"
value: ${{ jobs.publish.outputs.version }}

jobs:
publish:
name: Get and save publish version
runs-on: ubuntu-latest
outputs:
release: ${{ steps.releaseCheck.outputs.release }}
preRelease: ${{ steps.releaseCheck.outputs.preRelease }}
version: ${{ steps.getVersion.outputs.version }}
steps:
- name: ⬇ Checkout repo
uses: actions/checkout@v3

- name: 🔄 Init Cache
uses: ./.github/actions/npm-cache

- name: 💃🕺 Check if release or prerelease
id: releaseCheck
run: |
chmod +rx ./.github/scripts/get-release.sh
OUTPUT=$(./.github/scripts/get-release.sh)
if [[ $OUTPUT == "RELEASE" ]];
then
echo "::set-output name=release::true"
elif [[ $OUTPUT == "PRE_RELEASE" ]];
then
echo "::set-output name=preRelease::true"
fi
env:
GITHUB_REF: ${{ github.ref }}
GITHUB_ACTOR: ${{ github.actor }}
GITHUB_COMMITISH: ${{ github.event.release.target_commitish }}
GITHUB_PRE_RELEASE: ${{ github.event.release.prerelease }}

- name: ↔ Extract tag name
shell: bash
run: echo "##[set-output name=tag;]$(echo ${GITHUB_REF#refs/tags/})"
id: extractTag

- name: 🏷 Get and Set Package Version on Env
id: getVersion
env:
RELEASE: ${{ steps.releaseCheck.outputs.release }}
PRE_RELEASE: ${{ steps.releaseCheck.outputs.preRelease }}
TAG: ${{ steps.extractTag.outputs.tag }}
run: |
chmod +rx ./.github/scripts/package-version.sh
OUTPUT=$(./.github/scripts/package-version.sh)
echo "::set-output name=version::$OUTPUT"

- name: 🌳 Log Valid Version
env:
VALID_SEMVER_VERSION: ${{ steps.getVersion.outputs.version }}
run: echo "$VALID_SEMVER_VERSION"
53 changes: 53 additions & 0 deletions .github/workflows/03-publish-packages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
name: Publish all Packages to Registries

on:
workflow_call:
inputs:
release:
required: false
default: "false"
type: string
preRelease:
required: false
default: "false"
type: string
version:
required: true
type: string

jobs:
publish:
name: Publish latest package versions to GitHub Packages
runs-on: ubuntu-latest
strategy:
fail-fast: false
steps:
- name: ⬇ Checkout repo
uses: actions/checkout@v3

- name: 🔄 Init Cache
uses: ./.github/actions/npm-cache

- name: ⬇️Download package build
uses: actions/download-artifact@v3
with:
name: build
path: ./

- name: 📰 Publish to NPM Registries
run: |
chmod +rx ./.github/scripts/publish-npm.sh
./.github/scripts/publish-npm.sh
env:
RELEASE: ${{ inputs.release }}
PRE_RELEASE: ${{ inputs.preRelease }}
VALID_SEMVER_VERSION: ${{ inputs.version }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GPR_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: ⬆ Upload Package Artifact db-ui-base
uses: ./.github/actions/upload-tar-artifact
with:
name: package-base-tgz
path: db-ui-base-${{ inputs.version }}.tgz
35 changes: 35 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Test and publish to package registries after new GitHub release

on:
release:
types: [published]

jobs:
init:
uses: ./.github/workflows/00-init.yml

get-publish-version:
uses: ./.github/workflows/01-get-publish-version.yml
needs: [init]

# TODO: Reactivate when test and lint works
# lint:
# uses: ./.github/workflows/01-lint.yml
# needs: [init]
#
# test:
# uses: ./.github/workflows/01-test.yml
# needs: [init]

build:
uses: ./.github/workflows/01-build.yml
needs: [init]

publishpackages:
uses: ./.github/workflows/03-publish-packages.yml
needs: [build, get-publish-version]
secrets: inherit
with:
release: ${{ needs.get-publish-version.outputs.release }}
preRelease: ${{ needs.get-publish-version.outputs.preRelease }}
version: ${{ needs.get-publish-version.outputs.version }}
Loading