Run tests and release npm package #111
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Run tests and release npm package | |
on: | |
workflow_dispatch: | |
push: | |
branches: | |
- master | |
- main | |
- development | |
- beta | |
paths: | |
- 'src/**' | |
- 'package.json' | |
- 'package-lock.json' | |
- 'Dockerfile*' | |
pull_request: | |
branches: | |
- master | |
- main | |
- beta | |
paths: | |
- 'src/**' | |
- 'package.json' | |
- 'package-lock.json' | |
- 'Dockerfile*' | |
jobs: | |
# Install dependencies, run tests and try to build | |
test: | |
name: 'Build and test on Node v${{ matrix.node }}' | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
node: [18, 20] | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: actions/setup-node@v4 | |
with: | |
node-version: ${{ matrix.node }} | |
- name: Install depencencies | |
run: npm ci | |
- name: Build library | |
run: npm run build | |
- name: Run tests | |
run: npm test | |
- name: Run --help | |
run: npm run help | |
# Release to NPM if its not a pull request | |
npm-release: | |
runs-on: ubuntu-latest | |
name: NPM Release | |
if: github.event_name != 'pull_request' | |
needs: [test] | |
outputs: | |
released: ${{ steps.semantic.outputs.new_release_published }} | |
version: ${{ steps.semantic.outputs.new_release_version }} | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: actions/setup-node@v4 | |
with: | |
node-version: 20 | |
- name: Install depencencies | |
run: npm ci | |
- name: Build library | |
run: npm run build | |
- name: Semantic Release | |
uses: cycjimmy/semantic-release-action@v4 | |
id: semantic | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
NPM_TOKEN: ${{ secrets.NPM_TOKEN }} | |
- name: Setup node for Github Registry | |
if: steps.semantic.outputs.new_release_published == 'true' | |
uses: actions/setup-node@v3 | |
with: | |
node-version: 20 | |
registry-url: 'https://npm.pkg.github.com' | |
scope: 'svrooij' | |
- name: Publish To GitHub Package Registry | |
if: steps.semantic.outputs.new_release_published == 'true' | |
run: | | |
sed -i 's+"name": "sonos2mqtt"+"name": "@svrooij/sonos2mqtt"+g' package**.json | |
npm publish | |
env: | |
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}} | |
# Release container if it released to npm. | |
container-release: | |
runs-on: ubuntu-latest | |
name: Container release | |
needs: [npm-release] | |
if: needs.npm-release.outputs.released == 'true' | |
steps: | |
- uses: actions/checkout@v4 | |
- | |
name: Set up QEMU (multi platform) | |
uses: docker/setup-qemu-action@v3 | |
with: | |
platforms: all | |
- | |
name: Set up Docker Buildx | |
id: buildx | |
uses: docker/setup-buildx-action@v3 | |
with: | |
version: latest | |
- | |
name: Login to DockerHub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- | |
name: Login to GitHub Container Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Publish container as latest | |
if: github.ref == 'refs/heads/master' | |
run: | | |
docker buildx build \ | |
--platform linux/amd64,linux/arm/v7,linux/arm64 \ | |
--push \ | |
-f ./Dockerfile.npm \ | |
-t $GITHUB_REPOSITORY:latest \ | |
-t $GITHUB_REPOSITORY:${{ needs.npm-release.outputs.version }} \ | |
-t ghcr.io/svrooij/sonos2mqtt:latest \ | |
-t ghcr.io/svrooij/sonos2mqtt:${{ needs.npm-release.outputs.version }} \ | |
--build-arg BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') \ | |
--build-arg BUILD_VERSION=${{ needs.npm-release.outputs.version }} \ | |
--build-arg VSC_REF=$(echo ${GITHUB_SHA} | cut -c1-8) \ | |
. | |
- name: Publish container as beta | |
if: github.ref != 'refs/heads/master' | |
run: | | |
docker buildx build \ | |
--platform linux/amd64,linux/arm/v7,linux/arm64 \ | |
--push \ | |
-f ./Dockerfile.npm \ | |
-t $GITHUB_REPOSITORY:beta \ | |
-t $GITHUB_REPOSITORY:${{ needs.npm-release.outputs.version }} \ | |
-t ghcr.io/svrooij/sonos2mqtt:beta \ | |
-t ghcr.io/svrooij/sonos2mqtt:${{ needs.npm-release.outputs.version }} \ | |
--build-arg BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') \ | |
--build-arg BUILD_VERSION=${{ needs.npm-release.outputs.version }} \ | |
--build-arg VSC_REF=$(echo ${GITHUB_SHA} | cut -c1-8) \ | |
. |