Skip to content

Commit

Permalink
Added GHA, entrypoint, and hooked it together
Browse files Browse the repository at this point in the history
  • Loading branch information
dauglyon authored and dakotablair committed Jul 25, 2023
1 parent 0d224cd commit e6fda97
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Base URL path for the enviroment
PUBLIC_URL = '/dev/'

REACT_APP_KBASE_ENV=ci-europa
# Domain of enviroment for build
REACT_APP_KBASE_DOMAIN=ci-europa.kbase.us
# The following must be a subdomain of REACT_APP_KBASE_DOMAIN
REACT_APP_KBASE_LEGACY_DOMAIN=legacy.ci-europa.kbase.us
Expand Down
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build
coverage
node_modules
deploy
92 changes: 92 additions & 0 deletions .github/workflows/build_image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
name: Build Static Nginx Docker Image

on:
push:
branches: [ "main" ]
# Publish semver tags as releases.
tags: [ 'v*.*.*' ]
pull_request:
branches: [ "main" ]

env:
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
IMAGE_NAME: ${{ github.repository }}


jobs:
build:

runs-on: ubuntu-latest
permissions:
contents: read
packages: write
# This is used to complete the identity challenge
# with sigstore/fulcio when running outside of PRs.
id-token: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Install the cosign tool except on PR
# https://github.com/sigstore/cosign-installer
- name: Install cosign
if: github.event_name != 'pull_request'
uses: sigstore/cosign-installer@f3c664df7af409cb4873aa5068053ba9d61a57b6 #v2.6.0
with:
cosign-release: 'v1.13.1'


# Workaround: https://github.com/docker/build-push-action/issues/461
- name: Setup Docker buildx
uses: docker/setup-buildx-action@79abd3f86f79a9d68a23c75a09a9a85889262adf

# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Run multi-enviroment build
run: bash ./scripts/build_deploy.sh

# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@ac9327eae2b366085ac7f6a2d02df8aa8ead720a
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max


# Sign the resulting Docker image digest except on PRs.
# This will only write to the public Rekor transparency log when the Docker
# repository is public to avoid leaking data. If you would like to publish
# transparency data even for private images, pass --force to cosign below.
# https://github.com/sigstore/cosign
- name: Sign the published Docker image
if: ${{ github.event_name != 'pull_request' }}
env:
COSIGN_EXPERIMENTAL: "true"
# This step uses the identity token to provision an ephemeral certificate
# against the sigstore community Fulcio instance.
run: echo "${{ steps.meta.outputs.tags }}" | xargs -I {} cosign sign {}@${{ steps.build-and-push.outputs.digest }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

# production
/build
/deploy

# misc
.DS_Store
Expand Down
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM bitnami/nginx:latest
USER root

# Copy built static files for all enviroments to image
COPY ./deploy /deploy/

# Copy nginx config template to image
COPY ./scripts/nginx.conf.tmpl /nginx.conf.tmpl

COPY ./scripts/entrypoint.sh /entrypoint.sh
USER 1001
ENTRYPOINT ["/entrypoint.sh"]
16 changes: 0 additions & 16 deletions scripts/Dockerfile

This file was deleted.

20 changes: 17 additions & 3 deletions scripts/build_deploy.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
#!/usr/bin/env bash

# npm run build for every environment listed ...
# put it in /deploy/$ENVIRONMENT/app
# then in our Dockerfile we have COPY /deploy/ /

declare -a enviroments=(
# "<name> <domain> <legacy-domain>"
"ci-europa ci-europa.kbase.us legacy.ci-europa.kbase.us"
"narrative-dev narrative-dev.kbase.us legacy.narrative-dev.kbase.us"
)

for enviro in "${enviroments[@]}"; do
read -a strarr <<< "$enviro"
echo "Building static files for enviroment \"${strarr[0]}\"...";

BUILD_PATH="./deploy/${strarr[0]}" \
REACT_APP_KBASE_DOMAIN="${strarr[1]}" \
REACT_APP_KBASE_LEGACY_DOMAIN="${strarr[2]}" \
npm run build && \
echo "Built static files for enviroment \"${strarr[0]}\".";
done
4 changes: 4 additions & 0 deletions scripts/docker_entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

sed "s/__ENVIRONMENT__/$STATIC_ENVIRONMENT/" /nginx.conf.tmpl > /opt/bitnami/nginx/conf/server_blocks/app.conf
/opt/bitnami/scripts/nginx/entrypoint.sh /opt/bitnami/scripts/nginx/run.sh
2 changes: 2 additions & 0 deletions src/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const useInitApp = () => {

// Placeholder code for determining environment.
useEffect(() => {
// eslint-disable-next-line no-console
console.info('Static Deploy Domain:', process.env.REACT_APP_KBASE_DOMAIN);
dispatch(setEnvironment('ci-europa'));
}, [dispatch]);

Expand Down
3 changes: 1 addition & 2 deletions src/common/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { kbaseBaseQuery } from './utils/kbaseBaseQuery';
import { createApi } from '@reduxjs/toolkit/query/react';

const ENV = process.env.REACT_APP_KBASE_ENV;
const baseUrl =
process.env.NODE_ENV === 'development'
? 'http://localhost:3000/'
: `https://${ENV === 'PROD' ? '' : `${ENV}.`}kbase.us`;
: `https://${process.env.REACT_APP_KBASE_DOMAIN}`;

export const baseApi = createApi({
reducerPath: 'combinedApi',
Expand Down
2 changes: 1 addition & 1 deletion src/common/components/PlaceholderFactory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const PlaceholderFactory = (name: string) => {
const Placeholder: FC<PlaceholderInterface> = (props) => {
const invalidEnvironment =
process.env.NODE_ENV === 'production' &&
process.env.REACT_APP_KBASE_ENV !== 'ci-europa';
process.env.REACT_APP_KBASE_DOMAIN !== 'ci-europa.kbase.us';
if (invalidEnvironment) {
throw new Error('Placeholder components may not be used in production.');
}
Expand Down

0 comments on commit e6fda97

Please sign in to comment.