Skip to content

Commit

Permalink
build: add CI/CD pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
mihirsamdarshi committed Apr 17, 2024
1 parent acdc100 commit 6aaeb0c
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: ci

on:
pull_request:
branches:
- main
- dev
push:
branches:
- main
- dev
- 130_MS_Github_Actions
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 20
cache: yarn

- name: Build site
run: |
yarn install --frozen-lockfile
yarn sass && yarn build
- name: Test site
run: yarn test --updateSnapshot --ci
172 changes: 172 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
name: deploy

on:
pull_request:
types:
- closed
branches:
- master
- dev
- 130_MS_Github_Actions
paths:
- .github/**
- .circleci/**
workflow_dispatch:
inputs:
version_bump:
description: 'Version increment to bump (patch, minor, major)'
required: true

jobs:
get-version-bump:
if: github.event.pull_request.merged == true || github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
outputs:
increment: ${{ steps.run-script.outputs.result }}
steps:
- name: Check for PR labels or workflow dispatch input
id: run-script
uses: actions/github-script@v7
with:
result-encoding: string
script: |
var versionBump = null;
console.log(`Event name: ${context.eventName}`);
if (context.eventName == 'pull_request') {
var pullRequestNumber = context.issue.number;
if (!pullRequestNumber) {
console.log('No pull request number found in context, trying to get it from commit');
// Otherwise return issue number from commit
pullRequestNumber = (
await github.rest.repos.listPullRequestsAssociatedWithCommit({
commit_sha: context.sha,
owner: context.repo.owner,
repo: context.repo.repo,
})
).data[0].number;
}
console.log(`Pull request number: ${pullRequestNumber}`);
const { data: pullRequest } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pullRequestNumber,
});
console.log(`Pull request labels: ${JSON.stringify(pullRequest.labels, null, 2)}`);
// check if on dev branch
if (pullRequest.base.ref !== 'master') {
console.log('On dev branch, defaulting to pre-release version bump');
versionBump = "prerelease";
return versionBump;
}
for (const label of pullRequest.labels) {
if ([ 'major', 'minor', 'patch' ].includes(label.name)) {
versionBump = label.name;
break;
}
}
} else if (context.eventName == 'workflow_dispatch') {
if ([ 'major', 'minor', 'patch' ].includes(${{ toJSON(inputs.version_bump) }})) {
versionBump = ${{ toJSON(inputs.version_bump) }};
}
}
console.log(`Version bump: ${versionBump}`);
return versionBump;
bump-version:
needs: get-version-bump
runs-on: ubuntu-latest
# Grant GITHUB_TOKEN the permissions required to make a Pages deployment
permissions:
contents: write
id-token: write # to verify the deployment originates from an appropriate source

steps:
- name: Check if version bump is needed
if: needs.get-version-bump.outputs.increment == null || needs.get-version-bump.outputs.increment == 'null'
# exit 0 to prevent further steps from running
run: exit 0

- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for all branches and tags.
ref: ${{ github.base_ref }}

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: yarn

- name: Clean input string
run: |
INPUT_VERSION="${{ needs.get-version-bump.outputs.increment }}"
INPUT_VERSION=${INPUT_VERSION,,}
echo "INPUT_VERSION=${INPUT_VERSION// /}" >> $GITHUB_ENV
- name: Bump version and push tag
working-directory: documentation
run: |
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config user.name "github-actions[bot]"
# add args to yarn version command if INPUT_VERSION is "prerelease"
if [ $INPUT_VERSION == "prerelease" ]; then
yarn version --prerelease --preid dev --message "chore(release): bump version to v%s"
else
yarn version --${{ env.INPUT_VERSION }} --message "chore(release): bump version to v%s"
fi
git push
git push --tags
build-image:
runs-on: ubuntu-latest
# Grant GITHUB_TOKEN the permissions required to make a Pages deployment
permissions:
contents: read
id-token: write # to verify the deployment originates from an appropriate source

needs:
- bump-version
steps:
- uses: actions/checkout@v4

- name: Authenticate to Google Cloud
id: auth
uses: google-github-actions/auth@v2
with:
token_format: access_token
workload_identity_provider: ${{ secrets.WORKLOAD_IDENTITY_PROVIDER }}
service_account: ${{ secrets.SERVICE_ACCOUNT }}

# This example uses the docker login action
- name: Login to Artifact Registry
uses: docker/login-action@v3
with:
registry: us-docker.pkg.dev
username: oauth2accesstoken
password: ${{ steps.auth.outputs.access_token }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Extract version
id: extract-version
run: |
echo "PACKAGE_VERSION=$(jq -r '.version' package.json)" >> $GITHUB_ENV
- name: Build and push server image
uses: docker/build-push-action@v5
with:
push: true
tags: |
us-docker.pkg.dev/motrpac-portal-dev/datahub/frontend:${{ github.sha }}
us-docker.pkg.dev/motrpac-portal-dev/datahub/frontend:${{ env.PACKAGE_VERSION }}
us-docker.pkg.dev/motrpac-portal-dev/datahub/frontend:latest
cache-from: type=gha
cache-to: type=gha,mode=max

0 comments on commit 6aaeb0c

Please sign in to comment.