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

chore(ci): change from circle ci to github actions #8

Merged
merged 10 commits into from
Mar 25, 2022
168 changes: 168 additions & 0 deletions .github/workflows/frontend-build-and-publish-lib.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
name: Frontend Build and Publish Library
on:
workflow_call:
inputs:
node-version:
description: Node version to use (defaults to 14)
required: false
type: string
default: '14'
junit-report-path:
description: The path to unit test report (defaults to ./junit/)
required: false
type: string
default: ./junit/
junit-report-name:
description: The file name for unit test report (defaults to test-results.xml)
required: false
type: string
default: test-results.xml
aws-region:
description: Which AWS region to use for publish storybook (defaults to eu-west-1)
required: false
type: string
default: eu-west-1
secrets:
npm-token:
required: true
aws-access-key-id:
required: true
aws-secret-access-key:
required: true
jobs:
build:
name: Build and publish library
runs-on: ubuntu-latest
steps:
- name: Setup Node
uses: actions/setup-node@v3
zeppu marked this conversation as resolved.
Show resolved Hide resolved
with:
node-version: ${{ inputs.node-version }}
always-auth: true
registry-url: https://www.myget.org/F/river-tech/npm/
scope: '@odin'
zeppu marked this conversation as resolved.
Show resolved Hide resolved

- name: Print environment versions
run: |
NODE_V=$(node --version)
NPM_V=$(npm -v)
echo node version':' $NODE_V
echo npm version':' $NPM_V

- name: Checkout
uses: actions/[email protected]

- name: Install npm
run: npm ci
env:
NODE_AUTH_TOKEN: ${{ secrets.npm-token }}

- name: Build
run: npm run build

- name: Test
run: npm run test -- --browsers=ChromeHeadlessCI --no-watch --no-progress

- name: Build storybook
run: npm run build:storybook

- name: Check if commit is publishable
id: commit
run: |
if git log --format=%B -n 1 | egrep "^(style|test|docs)"; then
echo "::set-output name=publishable::false"
echo "This update doesn't need to be published; finishing build..."
else
echo "::set-output name=publishable::true"
fi

- name: Fetch git tags
if: ${{ steps.commit.outputs.publishable == 'true' }}
run: |
git fetch --prune --unshallow --tags

- name: Check if tag exists
id: tag
if: ${{ steps.commit.outputs.publishable == 'true' }}
run: |
TAG=$(node -p "require('./lerna.json').version")
if git tag --list | grep "^$TAG$"; then
echo "::set-output name=publishable::false"
echo "Tag already exists. Please make sure you bump version!"
zeppu marked this conversation as resolved.
Show resolved Hide resolved
else
echo "::set-output name=publishable::true"
fi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could probably all be replaced with a dedicated check git tag action:
https://github.com/marketplace/actions/tag-exists-action
https://github.com/marketplace/actions/github-tag-check
Haven't tried these myself so not sure if it serves your purpose

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used first one


- name: Configure AWS credentials
if: ${{ steps.commit.outputs.publishable == 'true' && steps.tag.outputs.publishable == 'true' }}
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.aws-access-key-id }}
aws-secret-access-key: ${{ secrets.aws-secret-access-key }}
aws-region: ${{ inputs.aws-region }}

- name: Login to Amazon ECR
id: login-ecr
if: ${{ steps.commit.outputs.publishable == 'true' && steps.tag.outputs.publishable == 'true' }}
uses: aws-actions/amazon-ecr-login@v1

- name: Publish storybook
if: ${{ steps.commit.outputs.publishable == 'true' && steps.tag.outputs.publishable == 'true' }}
env:
ECR_HOST: ${{ steps.login-ecr.outputs.registry }}
run: |
TAG=$(node -p "require('./storybook/package.json').version")
if $GITHUB_REF_NAME == 'develop'
then
TAG=$TAG-dev-$GITHUB_RUN_ID
elif [[ $GITHUB_REF_NAME =~ ^feature\/(.*)$ ]]
then
TAG=$TAG-demo-${BASH_REMATCH[1]}-$GITHUB_RUN_ID
fi

[[ $ECR_HOST =~ ^([0-9]*) ]] && REGISTRY=${BASH_REMATCH[1]}

if aws ecr describe-images --repository-name=odin.ngx-storybook --registry-id $REGISTRY --image-ids=imageTag=$TAG > /dev/null 2>&1; then
echo "$TAG already exists on remote... skipping..."
exit 0
fi

npm run build:storybook

TAG=$TAG docker-compose build
TAG=$TAG docker-compose push

if ! ([ $GITHUB_REF_NAME = "master" ] || [[ $GITHUB_REF_NAME =~ ^([0-9].*)\.x$ ]]); then
echo "PUBLISHABLE=false" >> $GITHUB_ENV
echo "Non-publishable branch; finishing build..."
fi

- name: Logout of Amazon ECR
if: ${{ steps.commit.outputs.publishable == 'true' && steps.tag.outputs.publishable == 'true' }}
run: docker logout ${{ steps.login-ecr.outputs.registry }}

- name: Deploy
if: ${{ steps.commit.outputs.publishable == 'true' && steps.tag.outputs.publishable == 'true' }}
run: |
echo "git tag"
PACKAGE_VERSION=$(node -p "require('./lerna.json').version")
git tag $PACKAGE_VERSION
git push --tags
npm run publish:rel

- name: Store tests artifact
uses: actions/upload-artifact@v3
if: failure()
with:
name: ${{ env.junit-report-name }}
path: ${{ env.junit-report-path }}
retention-days: 2

- name: Store npm artifact
uses: actions/upload-artifact@v3
if: failure()
with:
name: npm-logs
path: ~/.npm/_logs/*
retention-days: 2