From 6bf2d7519b4f4f2960ed197484707612ff5bcf55 Mon Sep 17 00:00:00 2001 From: "Andreas Goelzer (NuoDB)" <139239314+agoelzer@users.noreply.github.com> Date: Fri, 4 Oct 2024 13:08:23 -0400 Subject: [PATCH] Publish Docker image to GHCR (#30) --- .circleci/config.yml | 2 +- .github/workflows/build.yaml | 41 ++++++++++++++++++++++++++++++++++++ Makefile | 10 ++++----- build_utils.sh | 21 +++++++++++++++++- docker/production/Dockerfile | 5 +++-- ui/package.json | 4 ++-- 6 files changed, 72 insertions(+), 11 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 7a7c9fa..8312532 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -67,7 +67,7 @@ jobs: command: make teardown-integration-tests - run: name: Deploy Docker image / Helm Charts to ECR - command: make deploy-image + command: make deploy-image-ecr - run: name: Save test results command: | diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 3ac6537..b59165c 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -5,6 +5,47 @@ on: - "main" - "rel/*" jobs: + publish_docker_image: + name: Publish Docker Image + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Install JDK 17 + uses: actions/setup-java@v4 + with: + distribution: 'zulu' + java-version: '17' + - name: Build Image + run: | + make build-image + - name: Setup Integration Test Environment + run: | + make setup-integration-tests + - name: Run Integration Tests + run: | + make run-integration-tests-only + - name: Teardown Integration Test Environment + if: '!cancelled()' + run: | + make teardown-integration-tests + - name: 'Login to GHCR' + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{github.actor}} + password: ${{secrets.GITHUB_TOKEN}} + - name: Deploy Docker image to GHCR + run: | + ./build_utils.sh uploadDockerImage + - name: Upload Test Results + if: '!cancelled()' + uses: actions/upload-artifact@v4 + with: + name: test-results + path: selenium-tests/target/test-results + retention-days: 7 + publish_helm_chart: name: Publish Helm Chart runs-on: ubuntu-latest diff --git a/Makefile b/Makefile index 7a0df8e..b00a91f 100644 --- a/Makefile +++ b/Makefile @@ -39,14 +39,14 @@ help: ## Display this help. ##@ Production Builds .PHONY: all -all: run-integration-tests deploy-image ## build, test + deploy everything +all: run-integration-tests deploy-image-ecr ## build, test + deploy everything .PHONY: build-image build-image: ## build UI and create Docker image - @docker build -t "${IMG_REPO}:latest" -f docker/production/Dockerfile . + @docker build -t "${IMG_REPO}:latest" --build-arg "REACT_APP_GIT_SHA=${SHA}" -f docker/production/Dockerfile . -.PHONY: deploy-image -deploy-image: build-image ## deploy Docker image to AWS +.PHONY: deploy-image-ecr +deploy-image-ecr: build-image ## deploy Docker image to AWS @if [ "${ECR_ACCOUNT_URL}" = "" ] ; then \ echo "ECR_ACCOUNT_URL environment variable must be set"; \ elif [ "${UNCOMMITTED}" != "" ] ; then \ @@ -74,7 +74,7 @@ install-crds: $(KWOKCTL) $(KUBECTL) .PHONY: setup-integration-tests setup-integration-tests: build-image install-crds ## setup containers before running integration tests @docker compose -f selenium-tests/compose.yaml up --wait - @docker exec -it selenium-tests-nuodb-cp-1 bash -c "curl \ + @docker exec selenium-tests-nuodb-cp-1 bash -c "curl \ http://localhost:8080/users/acme/admin?allowCrossOrganizationAccess=true \ --data-binary \ '{\"password\":\"passw0rd\", \"name\":\"admin\", \"organization\": \"acme\", \"accessRule\":{\"allow\": \"all:*\"}}' \ diff --git a/build_utils.sh b/build_utils.sh index 80aa0b9..8f6127e 100755 --- a/build_utils.sh +++ b/build_utils.sh @@ -51,7 +51,7 @@ function helmChartExists() { } # Returns the docker image location (and tag) based on branch info -if [ "$1" == "getDockerImageTag" ] ; then +function getDockerImageTag() { if [ "${BRANCH}" == "main" ] ; then # development builds echo "${DOCKER_REGISTRY}:${VERSION}-${GIT_HASH}" @@ -67,6 +67,10 @@ if [ "$1" == "getDockerImageTag" ] ; then fi fi exit 0 +} + +if [ "$1" == "getDockerImageTag" ] ; then + getDockerImageTag fi # creates helm package @@ -152,6 +156,21 @@ if [ "$1" == "uploadHelmPackage" ] ; then exit 0 fi +if [ "$1" == "uploadDockerImage" ] ; then + DOCKER_IMAGE_TAG=$(getDockerImageTag) + if [ "${UNCOMMITTED}" != "" ] ; then + echo "Uncommitted changes in GIT. Will not push to GHCR." + echo "${UNCOMMITTED}" + exit 1 + else + docker tag "nuodbaas-webui:latest" "${DOCKER_IMAGE_TAG}" && \ + docker push "${DOCKER_IMAGE_TAG}" + fi + + exit 0 +fi + echo "$0 getDockerImageTag" echo "$0 createHelmPackage" echo "$0 uploadHelmPackage" +echo "$0 uploadDockerImage" diff --git a/docker/production/Dockerfile b/docker/production/Dockerfile index d3cdc37..13635f1 100644 --- a/docker/production/Dockerfile +++ b/docker/production/Dockerfile @@ -2,11 +2,12 @@ FROM node:18-alpine AS build-ui WORKDIR /app/ -COPY ui/public/ /app/public -COPY ui/src/ /app/src +ARG REACT_APP_GIT_SHA COPY ui/package.json /app/ COPY ui/tsconfig.json /app/ RUN npm install +COPY ui/public/ /app/public +COPY ui/src/ /app/src RUN npm run build FROM nginx:alpine diff --git a/ui/package.json b/ui/package.json index 14ec1a0..c63c9d0 100644 --- a/ui/package.json +++ b/ui/package.json @@ -27,8 +27,8 @@ "web-vitals": "^2.1.4" }, "scripts": { - "start": "export REACT_APP_GIT_SHA=$(git rev-parse HEAD) && react-scripts start", - "build": "export REACT_APP_GIT_SHA=$(git rev-parse HEAD) && react-scripts build", + "start": "react-scripts start", + "build": "react-scripts build", "test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!axios)/\"", "eject": "react-scripts eject" },