diff --git a/.env b/.env new file mode 100644 index 000000000..b290898cf --- /dev/null +++ b/.env @@ -0,0 +1 @@ +SERVER_IMAGE_TAG=2024-09-20--06-45 diff --git a/.github/workflows/image_build_push.yml b/.github/workflows/image_build_push.yml index 1847af67b..bbc092cd8 100644 --- a/.github/workflows/image_build_push.yml +++ b/.github/workflows/image_build_push.yml @@ -10,21 +10,36 @@ env: DOCKER_PASSWORD: ${{secrets.DOCKER_PASSWORD}} jobs: + test-with-docker: + uses: e-mission/e-mission-server/.github/workflows/test-with-docker.yml@master + + test-with-manual-install: + uses: e-mission/e-mission-server/.github/workflows/test-with-manual-install.yml@master + build: runs-on: ubuntu-latest + needs: [test-with-docker, test-with-manual-install] outputs: date: ${{ steps.date.outputs.date }} steps: - uses: actions/checkout@v2 + + - name: Set docker image tags + id: set-tags + run: | + set -a; source .env; set +a + echo "SERVER_IMAGE_TAG=${SERVER_IMAGE_TAG}" >> "$GITHUB_OUTPUT" + echo "Current server image tag (push): ${SERVER_IMAGE_TAG}" + - name: docker login run: | # log into docker hub account docker login -u $DOCKER_USER -p $DOCKER_PASSWORD - name: Get current date # get the date of the build id: date - run: echo "::set-output name=date::$(date +'%Y-%m-%d--%M-%S')" + run: echo "date=$(date +'%Y-%m-%d--%M-%S')" >> "$GITHUB_OUTPUT" #Runs a single command using the runners shell - name: Run a one-line script @@ -40,25 +55,26 @@ jobs: run: | docker push $DOCKER_USER/${GITHUB_REPOSITORY#*/}:${GITHUB_REF##*/}_${{ steps.date.outputs.date }} - - name: Create a text file + - name: Update .env file run: | - echo ${{ steps.date.outputs.date }} > tag_file.txt - echo "Created tag text file" + echo "SERVER_IMAGE_TAG=${GITHUB_REF##*/}_${{ steps.date.outputs.date }}" > .env - - name: Upload Artifact - uses: actions/upload-artifact@v4 - with: - name: docker-image-tag - path: tag_file.txt - overwrite: true + - name: Add, Commit, Push changes to .env file + run: | + git config --local user.email "action@github.com" + git config --local user.name "Github Actions bot to update .env with latest tags" + if git diff --quiet; then + echo "Latest timestamp already present in .env file, no changes to commit" + else + git add .env + git commit -m "Updated docker image tags in .env file to the latest timestamp" + git push origin + fi dispatch: needs: build runs-on: ubuntu-latest - env: - DOCKER_IMAGE_TAG: ${{ needs.build.outputs.date }} - strategy: matrix: include: @@ -71,7 +87,6 @@ jobs: - uses: actions/checkout@v4 - name: Trigger workflow in admin-dash, public-dash - # TODO: Create Fine-grained token with "Actions: write" permissions run: | curl -L \ -X POST \ @@ -79,4 +94,4 @@ jobs: -H "Authorization: Bearer ${{ secrets.GH_FG_PAT_TAGS }}" \ -H "X-GitHub-Api-Version: 2022-11-28" \ https://api.github.com/repos/${{ matrix.repo }}/actions/workflows/image_build_push.yml/dispatches \ - -d '{"ref":"${{ matrix.branch }}", "inputs": {"docker_image_tag" : "${{ env.DOCKER_IMAGE_TAG }}"}}' + -d '{"ref":"${{ matrix.branch }}"}' diff --git a/.github/workflows/reusable_image_build_push.yml b/.github/workflows/reusable_image_build_push.yml new file mode 100644 index 000000000..5de4eb7eb --- /dev/null +++ b/.github/workflows/reusable_image_build_push.yml @@ -0,0 +1,141 @@ +name: Reusable Docker Image Build and Push + +on: + workflow_call: + inputs: + repo: + required: true + type: string + branch: + required: true + type: string + +env: + DOCKER_USER: ${{secrets.DOCKER_USER}} + DOCKER_PASSWORD: ${{secrets.DOCKER_PASSWORD}} + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + repository: e-mission/${{ inputs.repo }} + ref: ${{ inputs.branch }} + token: ${{ secrets.GH_FG_PAT_TAGS }} + + - name: Fetch server image tag + id: get-server-tag + run: | + if [ "${{ inputs.repo }}" = "op-admin-dashboard" ] || [ "${{ inputs.repo }}" = "em-public-dashboard" ]; then + response=$(curl -s https://raw.githubusercontent.com/e-mission/e-mission-server/refs/heads/master/.env) + SERVER_IMAGE_TAG=$(echo "$response" | grep "SERVER_IMAGE_TAG=" | cut -d'=' -f2) + echo "SERVER_IMAGE_TAG=$SERVER_IMAGE_TAG" >> "$GITHUB_OUTPUT" + fi + + - name: Set docker image tags + id: set-tags + run: | + ls -al + set -a; source .env; set +a + if [ "${{ inputs.repo }}" = "nrel-openpath-join-page" ]; then + echo "JOIN_IMAGE_TAG=${JOIN_IMAGE_TAG}" >> "$GITHUB_OUTPUT" + elif [ "${{ inputs.repo }}" = "op-admin-dashboard" ]; then + echo "ADMIN_DASH_IMAGE_TAG=${ADMIN_DASH_IMAGE_TAG}" >> "$GITHUB_OUTPUT" + elif [ "${{ inputs.repo }}" = "em-public-dashboard" ]; then + echo "PUBLIC_DASH_NOTEBOOK_IMAGE_TAG=${PUBLIC_DASH_NOTEBOOK_IMAGE_TAG}" >> "$GITHUB_OUTPUT" + echo "PUBLIC_DASH_FRONTEND_IMAGE_TAG=${PUBLIC_DASH_FRONTEND_IMAGE_TAG}" >> "$GITHUB_OUTPUT" + fi + + - name: Print input docker image tags + run: | + echo "Event name: ${{ github.event_name }}" + if [ "${{ inputs.repo }}" = "nrel-openpath-join-page" ]; then + echo "Current join-page image tag: ${{ steps.set-tags.outputs.JOIN_IMAGE_TAG }}" + elif [ "${{ inputs.repo }}" = "op-admin-dashboard" ]; then + echo "Current admin-dash image tag: ${{ steps.set-tags.outputs.ADMIN_DASH_IMAGE_TAG }}" + echo "Latest server image tag (${{ github.event_name }}): ${{ steps.get-server-tag.outputs.SERVER_IMAGE_TAG }}" + elif [ "${{ inputs.repo }}" = "em-public-dashboard" ]; then + echo "Current notebook image tag (push): ${{ steps.set-tags.outputs.PUBLIC_DASH_NOTEBOOK_IMAGE_TAG }}" + echo "Current frontend image tag (push): ${{ steps.set-tags.outputs.PUBLIC_DASH_FRONTEND_IMAGE_TAG }}" + echo "Latest server image tag (${{ github.event_name }}): ${{ steps.get-server-tag.outputs.SERVER_IMAGE_TAG }}" + fi + + - name: docker login + run: | + docker login -u $DOCKER_USER -p $DOCKER_PASSWORD + + - name: Get current date # get the date of the build + id: date + run: echo "date=$(date +'%Y-%m-%d--%M-%S')" >> "$GITHUB_OUTPUT" + + - name: Run a one-line script + run: echo running image build for repo ${{ inputs.repo }} branch ${{ inputs.branch }} on ${{ steps.date.outputs.date }} + + - name: build docker image + run: | + if [ "${{ inputs.repo }}" = "nrel-openpath-join-page" ]; then + docker build -t $DOCKER_USER/${{ inputs.repo }}:${{ inputs.branch }}_${{ steps.date.outputs.date }} ./frontend + elif [ "${{ inputs.repo }}" = "op-admin-dashboard" ]; then + SERVER_IMAGE_TAG=${{ steps.get-server-tag.outputs.SERVER_IMAGE_TAG }} docker compose -f docker-compose-prod.yml build + elif [ "${{ inputs.repo }}" = "em-public-dashboard" ]; then + SERVER_IMAGE_TAG=${{ steps.get-server-tag.outputs.SERVER_IMAGE_TAG }} docker compose -f docker-compose.yml build + fi + docker images + + - name: rename docker image + run: | + if [ "${{ inputs.repo }}" = "op-admin-dashboard" ]; then + docker image tag e-mission/opdash:0.0.1 $DOCKER_USER/${{ inputs.repo }}:${{ inputs.branch }}_${{ steps.date.outputs.date }} + elif [ "${{ inputs.repo }}" = "em-public-dashboard" ]; then + if [ "${{ github.event_name }}" == "push" ]; then + docker image tag em-pub-dash-prod/frontend:latest $DOCKER_USER/${{ inputs.repo }}_frontend:${{ inputs.branch }}_${{ steps.date.outputs.date }} + fi + docker image tag em-pub-dash-prod/viz-scripts:latest $DOCKER_USER/${{ inputs.repo }}_notebook:${{ inputs.branch }}_${{ steps.date.outputs.date }} + fi + + - name: push docker image + run: | + if [ "${{ inputs.repo }}" = "op-admin-dashboard" ] || [ "${{ inputs.repo }}" = "nrel-openpath-join-page" ]; then + docker push $DOCKER_USER/${{ inputs.repo }}:${{ inputs.branch }}_${{ steps.date.outputs.date }} + elif [ "${{ inputs.repo }}" = "em-public-dashboard" ]; then + if [ "${{ github.event_name }}" == "push" ]; then + docker push $DOCKER_USER/${{ inputs.repo }}_frontend:${{ inputs.branch }}_${{ steps.date.outputs.date }} + fi + docker push $DOCKER_USER/${{ inputs.repo }}_notebook:${{ inputs.branch }}_${{ steps.date.outputs.date }} + fi + + - name: Update .env file + run: | + if [ "${{ inputs.repo }}" = "nrel-openpath-join-page" ]; then + echo "JOIN_IMAGE_TAG=${{ inputs.branch }}_${{ steps.date.outputs.date }}" > .env + elif [ "${{ inputs.repo }}" = "op-admin-dashboard" ]; then + echo "ADMIN_DASH_IMAGE_TAG=${{ inputs.branch }}_${{ steps.date.outputs.date }}" > .env + echo "SERVER_IMAGE_TAG=${{ steps.get-server-tag.outputs.SERVER_IMAGE_TAG }}" >> .env + elif [ "${{ inputs.repo }}" = "em-public-dashboard" ]; then + echo "PUBLIC_DASH_NOTEBOOK_IMAGE_TAG=${{ inputs.branch }}_${{ steps.date.outputs.date }}" > .env + if [ "${{ github.event_name }}" == "push" ]; then + echo "Push event: Update frontend image tag" + echo "PUBLIC_DASH_FRONTEND_IMAGE_TAG=${{ inputs.branch }}_${{ steps.date.outputs.date }}" >> .env + else + echo "Workflow_dispatch: Reuse existing frontend image tag" + echo "PUBLIC_DASH_FRONTEND_IMAGE_TAG=${{ steps.set-tags.outputs.PUBLIC_DASH_FRONTEND_IMAGE_TAG }}" >> .env + fi + echo "SERVER_IMAGE_TAG=${{ steps.get-server-tag.outputs.SERVER_IMAGE_TAG }}" >> .env + fi + cat .env + + - name: Add, Commit, Push changes to .env file + run: | + git config --local user.email "action@github.com" + git config --local user.name "Github Actions bot to update .env with latest tags" + # echo ${{ github.actor }} + if git diff --quiet; then + echo "Latest timestamp already present in .env file, no changes to commit" + else + git add .env + git commit -m "Updated docker image tags in .env file to the latest timestamp" + git push origin + fi + cat .env diff --git a/.github/workflows/test-with-docker.yml b/.github/workflows/test-with-docker.yml index 1b54ed5ba..d661e6a02 100644 --- a/.github/workflows/test-with-docker.yml +++ b/.github/workflows/test-with-docker.yml @@ -5,14 +5,14 @@ name: test-with-docker # Controls when the action will run. Triggers the workflow on push or pull request # events but only for the master branch on: - push: - branches: [ master ] pull_request: branches: [ master ] schedule: # * is a special character in YAML so you have to quote this string - cron: '5 4 * * 0' + workflow_call: + # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: # This workflow contains a single job called "build" diff --git a/.github/workflows/test-with-manual-install.yml b/.github/workflows/test-with-manual-install.yml index 4a81eb000..75f920682 100644 --- a/.github/workflows/test-with-manual-install.yml +++ b/.github/workflows/test-with-manual-install.yml @@ -5,10 +5,6 @@ name: ubuntu-only-test-with-manual-install # Controls when the action will run. Triggers the workflow on push or pull request # events but only for the master branch on: - push: - branches: - - master - - gis-based-mode-detection pull_request: branches: - master @@ -17,6 +13,8 @@ on: # * is a special character in YAML so you have to quote this string - cron: '5 4 * * 0' + workflow_call: + # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: # This workflow contains a single job called "build" diff --git a/Dockerfile b/Dockerfile index fcea642fd..04dba77f0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,8 +3,6 @@ FROM ubuntu:jammy-20240227 MAINTAINER K. Shankari (shankari@eecs.berkeley.edu) -ADD https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem /etc/ssl/certs/ - WORKDIR /usr/src/app RUN apt-get -y -qq update