Skip to content

Changes from gocardless/client-library-templates #169

Changes from gocardless/client-library-templates

Changes from gocardless/client-library-templates #169

Workflow file for this run

name: test & publish
on:
push:
branches: [master]
pull_request:
types: [opened, reopened, synchronize]
permissions:
id-token: write # Required for OIDC
contents: write # Required for creating releases and tags
jobs:
test:
runs-on: ubuntu-latest
container:
image: node:lts
steps:
- uses: actions/checkout@v3.3.0
- run: npm install --save-dev jest typescript ts-jest @types/jest
- run: npm test
transpile:
runs-on: ubuntu-latest
container:
image: node:lts
needs:
- test
steps:
- uses: actions/checkout@v3.3.0
- run: npm install
- run: npm install -g typescript
- run: npm run build
- uses: actions/upload-artifact@v4
with:
path: "dist"
all-tests-passed:
name: all-tests-passed
runs-on: ubuntu-latest
needs: [test, transpile]
if: always()
steps:
- name: Check test/transpile job status
run: |
if [ "${{ needs.test.result }}" != "success" ] || [ "${{ needs.transpile.result }}" != "success" ]; then
echo "test or transpile job did not succeed"
exit 1
fi
check_version:
if: github.ref == 'refs/heads/master'
runs-on: ubuntu-latest
outputs:
should_publish: ${{ steps.check.outputs.should_publish }}
version: ${{ steps.check.outputs.version }}
steps:
- uses: actions/checkout@v3.3.0
with:
fetch-depth: 0
- name: Check if version tag exists
id: check
run: |
VERSION=$(node -p "require('./package.json').version")
echo "version=$VERSION" >> $GITHUB_OUTPUT
if git rev-parse "v${VERSION}" >/dev/null 2>&1; then
echo "Tag v${VERSION} already exists, skipping publish"
echo "should_publish=false" >> $GITHUB_OUTPUT
else
echo "Tag v${VERSION} does not exist, will publish"
echo "should_publish=true" >> $GITHUB_OUTPUT
fi
publish_to_npm:
if: github.ref == 'refs/heads/master' && needs.check_version.outputs.should_publish == 'true'
runs-on: ubuntu-latest
container:
image: node:lts
needs:
- transpile
- check_version
env:
DIST_FOLDER: "/dist/artifact"
steps:
- uses: actions/checkout@v3.3.0
- uses: actions/download-artifact@v4
with:
path: "/dist"
- run: cp package.json $DIST_FOLDER
- run: cp README.md $DIST_FOLDER
- run: npm install
- name: Publish the library.
run: npm publish $DIST_FOLDER
create_release:
if: github.ref == 'refs/heads/master' && needs.check_version.outputs.should_publish == 'true'
runs-on: ubuntu-latest
needs:
- publish_to_npm
- check_version
steps:
- uses: actions/checkout@v3.3.0
with:
fetch-depth: 0
- name: Extract changelog notes for this version
id: notes
if: needs.check_version.outputs.should_publish == 'true'
run: |
VERSION="${{ needs.check_version.outputs.version }}"
NOTES=""
if [ -f CHANGELOG.md ]; then
NOTES=$(awk -v ver="$VERSION" '
$0 ~ "^## " ver {p=1; next}
p && /^## / {exit}
p {print}
' CHANGELOG.md)
fi
{
echo "notes<<EOF"
echo "$NOTES"
echo "EOF"
} >> "$GITHUB_OUTPUT"
- name: Create tag and release
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag v${{ needs.check_version.outputs.version }}
git push origin v${{ needs.check_version.outputs.version }}
NOTES="${{ steps.notes.outputs.notes }}"
TAG="v${{ needs.check_version.outputs.version }}"
if [ -n "$(echo "$NOTES" | tr -d '[:space:]')" ]; then
gh release create "$TAG" --title "$TAG" --notes "$NOTES"
else
gh release create "$TAG" --title "$TAG" --generate-notes
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}