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

Cleanup (Server) #3 - Tasks: A-1, 3 #988

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SERVER_IMAGE_TAG=2024-09-20--06-45
75 changes: 31 additions & 44 deletions .github/workflows/image_build_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,51 @@ name: docker image

on:
push:
branches: [ master, gis-based-mode-detection ]
branches: [ master, gis-based-mode-detection, cleanup-cicd ]
release:
types: [published]

#Dockerhub credentials are set as environment variables
env:
DOCKER_USER: ${{secrets.DOCKER_USER}}
DOCKER_PASSWORD: ${{secrets.DOCKER_PASSWORD}}

jobs:
build:
runs-on: ubuntu-latest
test-with-docker:
uses: e-mission/e-mission-server/.github/workflows/test-with-docker.yml@master

outputs:
date: ${{ steps.date.outputs.date }}
test-with-manual-install:
uses: e-mission/e-mission-server/.github/workflows/test-with-manual-install.yml@master

get-branch-name:
runs-on: ubuntu-latest
outputs:
branch_name: ${{ steps.get-branch-name.outputs.branch_name }}
steps:
- uses: actions/checkout@v2
- 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')"
- name: Get branch name based on ref_type
id: get-branch-name
run: |
if [ "${{ github.ref_type }}" == "tag" ]; then
target_branch=${{ github.event.release.target_commitish }}
echo "branch_name=${target_branch}" >> "$GITHUB_OUTPUT"
elif [ "${{ github.ref_type }}" == "branch" ]; then
echo "branch_name=${GITHUB_REF##*/}" >> "$GITHUB_OUTPUT"
fi

#Runs a single command using the runners shell
- name: Run a one-line script
run: echo running in repo ${GITHUB_REPOSITORY#*/} branch ${GITHUB_REF##*/} on ${{ steps.date.outputs.date }}

# Runs a set of commands using the runners shell
- name: build docker image
run: |
docker build -t $DOCKER_USER/${GITHUB_REPOSITORY#*/}:${GITHUB_REF##*/}_${{ steps.date.outputs.date }} .
docker images

- name: push docker image
run: |
docker push $DOCKER_USER/${GITHUB_REPOSITORY#*/}:${GITHUB_REF##*/}_${{ steps.date.outputs.date }}

- name: Create a text file
run: |
echo ${{ steps.date.outputs.date }} > tag_file.txt
echo "Created tag text file"

- name: Upload Artifact
uses: actions/upload-artifact@v4
with:
name: docker-image-tag
path: tag_file.txt
overwrite: true
build:
if: ${{ !contains(github.event.head_commit.author.name, 'Github Actions bot') }}
needs: [test-with-docker, test-with-manual-install, get-branch-name]
uses: e-mission/e-mission-server/.github/workflows/reusable_image_build_push.yml@master
with:
repo: ${{ github.event.repository.name }}
branch: ${{ needs.get-branch-name.outputs.branch_name }}
secrets: inherit

dispatch:
needs: build
if: ${{ github.ref_type == 'tag' }}
needs: [build]
runs-on: ubuntu-latest

env:
DOCKER_IMAGE_TAG: ${{ needs.build.outputs.date }}

strategy:
matrix:
include:
Expand All @@ -71,12 +59,11 @@ 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 \
-H "Accept: application/vnd.github+json" \
-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 }}"}'
281 changes: 281 additions & 0 deletions .github/workflows/reusable_image_build_push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
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 }}
fetch-depth: 0

- name: Fetch latest repository release tag
id: fetch-latest-release-tags
run: |
response=$(curl -L -H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GH_FG_PAT_TAGS }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/e-mission/${{ inputs.repo }}/releases/latest")

tag_name=$(echo "$response" | jq -r '.tag_name')
target_commitish=$(echo "$response" | jq -r '.target_commitish')

echo "tag_name=$(echo "$response" | jq -r '.tag_name')" >> "$GITHUB_OUTPUT"
echo "target_commitish=$(echo "$response" | jq -r '.target_commitish')" >> "$GITHUB_OUTPUT"

- name: Fetch latest server image tag
id: get-server-tag
if: ${{ inputs.repo == 'op-admin-dashboard' || inputs.repo == 'em-public-dashboard' }}
run: |
response=$(curl -L -H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GH_FG_PAT_TAGS }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/e-mission/e-mission-server/releases")

tags=$(echo "$response" | jq -r '.[].tag_name')
current_server_release_tag=$(echo "$tags" | sort -V | tail -n 1)
previous_server_release_tag=$(echo "$tags" | sort -V | tail -n 2 | head -n 1)

echo "SERVER_IMAGE_TAG=$current_server_release_tag" >> "$GITHUB_OUTPUT"
echo "PREVIOUS_SERVER_TAG=$previous_server_release_tag" >> "$GITHUB_OUTPUT"

echo "Latest server release tag: $current_server_release_tag"
echo "Previous server release tag: $previous_server_release_tag"

- name: Bump up release tag
if: ${{ github.event_name == 'workflow_dispatch' }}
id: increment-release-tag
run: |
current_server_tag=${{ steps.get-server-tag.outputs.SERVER_IMAGE_TAG }}
previous_server_tag=${{ steps.get-server-tag.outputs.PREVIOUS_SERVER_TAG }}

echo "Current server tag: $current_server_tag"
echo "Previous server tag: $previous_server_tag"

current_server_tag_only=${current_server_tag:1}
previous_server_tag_only=${previous_server_tag:1}

IFS='.' read -r -a current <<< "${current_server_tag_only}"
IFS='.' read -r -a previous <<< "${previous_server_tag_only}"

if [[ ${#current[@]} -eq 3 && ${#previous[@]} -eq 3 ]]; then
if [ "${current[0]}" -gt "${previous[0]}" ]; then
version_change=major
elif [ "${current[0]}" -eq "${previous[0]}" ] && [ "${current[1]}" -gt "${previous[1]}" ]; then
version_change=minor
elif [ "${current[0]}" -eq "${previous[0]}" ] && [ "${current[1]}" -eq "${previous[1]}" ] && [ "${current[2]}" -gt "${previous[2]}" ]; then
version_change=patch
fi
else
echo "Version format error"
exit 1
fi

echo "name=version_change::$version_change"

current_repo_version=${{ steps.fetch-latest-release-tags.outputs.tag_name }}
current_repo_version_only=${current_repo_version:1}

IFS='.' read -r -a version_parts <<< "${current_repo_version_only}"

case "$version_change" in
major)
version_parts[0]=$((version_parts[0] + 1))
version_parts[1]=0
version_parts[2]=0
;;
minor)
version_parts[1]=$((version_parts[1] + 1))
version_parts[2]=0
;;
patch)
version_parts[2]=$((version_parts[2] + 1))
;;
esac

new_repo_version="v${version_parts[0]}.${version_parts[1]}.${version_parts[2]}"
echo "new_repo_version::$new_repo_version"
echo "new_repo_version=$new_repo_version" >> "$GITHUB_OUTPUT"

- name: Print fetched tags
run: |
echo "Current image tag: ${{ steps.fetch-latest-release-tags.outputs.tag_name }} ; target_commit branch: ${{ steps.fetch-latest-release-tags.outputs.target_commitish }}"
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
echo "Incremented image tag: ${{ steps.increment-release-tag.outputs.new_repo_version }}"
fi

- name: Set docker image tags
id: set-tags
run: |
ls -al
set -a; source .env; set +a
if [ "${{ inputs.repo }}" = "e-mission-server" ]; then
echo "SERVER_IMAGE_TAG=${SERVER_IMAGE_TAG}" >> "$GITHUB_OUTPUT"
echo "CURRENT_REPO_TAG=${SERVER_IMAGE_TAG}" >> "$GITHUB_OUTPUT"
elif [ "${{ inputs.repo }}" = "nrel-openpath-join-page" ]; then
echo "JOIN_IMAGE_TAG=${JOIN_IMAGE_TAG}" >> "$GITHUB_OUTPUT"
echo "CURRENT_REPO_TAG=${JOIN_IMAGE_TAG}" >> "$GITHUB_OUTPUT"
elif [ "${{ inputs.repo }}" = "op-admin-dashboard" ]; then
echo "ADMIN_DASH_IMAGE_TAG=${ADMIN_DASH_IMAGE_TAG}" >> "$GITHUB_OUTPUT"
echo "CURRENT_REPO_TAG=${ADMIN_DASH_IMAGE_TAG}" >> "$GITHUB_OUTPUT"
elif [ "${{ inputs.repo }}" = "em-public-dashboard" ]; then
echo "CURRENT_REPO_TAG=${PUBLIC_DASH_NOTEBOOK_IMAGE_TAG}" >> "$GITHUB_OUTPUT"
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 }} | ${{ github.ref_type }}"
if [ "${{ inputs.repo }}" = "e-mission-server" ]; then
echo "Current server image tag: ${{ steps.set-tags.outputs.SERVER_IMAGE_TAG }}"
elif [ "${{ 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: ${{ steps.set-tags.outputs.PUBLIC_DASH_NOTEBOOK_IMAGE_TAG }}"
echo "Current frontend image tag: ${{ 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: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- 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 }}" = "e-mission-server" ]; then
docker build -t ghcr.io/e-mission/${{ inputs.repo }}:${{ steps.fetch-latest-release-tags.outputs.tag_name }} .
elif [ "${{ inputs.repo }}" = "nrel-openpath-join-page" ]; then
docker build -t ghcr.io/e-mission/${{ inputs.repo }}:${{ steps.fetch-latest-release-tags.outputs.tag_name }} ./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
if: ${{ github.ref_type == 'tag' || github.event_name == 'workflow_dispatch' }}
run: |
if [ "${{ inputs.repo }}" = "op-admin-dashboard" ]; then
if [ "${{ github.event_name }}" == "release" ]; then
docker tag e-mission/opdash:0.0.1 ghcr.io/e-mission/${{ inputs.repo }}:${{ steps.fetch-latest-release-tags.outputs.tag_name }}
elif [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
docker tag e-mission/opdash:0.0.1 ghcr.io/e-mission/${{ inputs.repo }}:${{ steps.increment-release-tag.outputs.new_repo_version }}
fi
elif [ "${{ inputs.repo }}" = "em-public-dashboard" ]; then
if [ "${{ github.event_name }}" == "release" ]; then
docker tag em-pub-dash-prod/frontend:latest ghcr.io/e-mission/${{ inputs.repo }}_frontend:${{ steps.fetch-latest-release-tags.outputs.tag_name }}
docker tag em-pub-dash-prod/viz-scripts:latest ghcr.io/e-mission/${{ inputs.repo }}_notebook:${{ steps.fetch-latest-release-tags.outputs.tag_name }}
elif [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
docker tag em-pub-dash-prod/viz-scripts:latest ghcr.io/e-mission/${{ inputs.repo }}_notebook:${{ steps.increment-release-tag.outputs.new_repo_version }}
fi
fi

- name: push docker image
if: ${{ github.ref_type == 'tag' || github.event_name == 'workflow_dispatch' }}
run: |
if [ "${{ inputs.repo }}" = "em-public-dashboard" ]; then
if [ "${{ github.event_name }}" == "release" ]; then
docker push ghcr.io/e-mission/${{ inputs.repo }}_frontend:${{ steps.fetch-latest-release-tags.outputs.tag_name }}
docker push ghcr.io/e-mission/${{ inputs.repo }}_notebook:${{ steps.fetch-latest-release-tags.outputs.tag_name }}
elif [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
docker push ghcr.io/e-mission/${{ inputs.repo }}_notebook:${{ steps.increment-release-tag.outputs.new_repo_version }}
fi
elif [ "${{ inputs.repo }}" = "op-admin-dashboard" ]; then
if [ "${{ github.event_name }}" == "release" ]; then
docker push ghcr.io/e-mission/${{ inputs.repo }}:${{ steps.fetch-latest-release-tags.outputs.tag_name }}
elif [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
docker push ghcr.io/e-mission/${{ inputs.repo }}:${{ steps.increment-release-tag.outputs.new_repo_version }}
fi
else
docker push ghcr.io/e-mission/${{ inputs.repo }}:${{ steps.fetch-latest-release-tags.outputs.tag_name }}
fi

- name: Update .env file
if: ${{ github.ref_type == 'tag' || github.event_name == 'workflow_dispatch' }}
run: |
if [ "${{ inputs.repo }}" = "e-mission-server" ]; then
echo "SERVER_IMAGE_TAG=${{ steps.fetch-latest-release-tags.outputs.tag_name }}" > .env
elif [ "${{ inputs.repo }}" = "nrel-openpath-join-page" ]; then
echo "JOIN_IMAGE_TAG=${{ steps.fetch-latest-release-tags.outputs.tag_name }}" > .env
elif [ "${{ inputs.repo }}" = "op-admin-dashboard" ]; then
if [ "${{ github.event_name }}" == "release" ]; then
echo "ADMIN_DASH_IMAGE_TAG=${{ steps.fetch-latest-release-tags.outputs.tag_name }}" > .env
elif [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
echo "ADMIN_DASH_IMAGE_TAG=${{ steps.increment-release-tag.outputs.new_repo_version }}" > .env
fi
echo "SERVER_IMAGE_TAG=${{ steps.get-server-tag.outputs.SERVER_IMAGE_TAG }}" >> .env
elif [ "${{ inputs.repo }}" = "em-public-dashboard" ]; then
if [ "${{ github.event_name }}" == "release" ]; then
echo "PUBLIC_DASH_NOTEBOOK_IMAGE_TAG=${{ steps.fetch-latest-release-tags.outputs.tag_name }}" > .env
echo "PUBLIC_DASH_FRONTEND_IMAGE_TAG=${{ steps.fetch-latest-release-tags.outputs.tag_name }}" >> .env
elif [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
echo "PUBLIC_DASH_NOTEBOOK_IMAGE_TAG=${{ steps.increment-release-tag.outputs.new_repo_version }}" > .env
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: Create, Push new tag and Publish release
if: ${{ github.event_name == 'workflow_dispatch' }}
run: |
git config --local user.email "action@github.com"
git config --local user.name "Github Actions bot to create new tags and publish releases"
git tag -a ${{ steps.increment-release-tag.outputs.new_repo_version }} -m"$(git log --pretty=format:%s ${{ steps.set-tags.outputs.CURRENT_REPO_TAG }}...HEAD --no-merges)"
git push origin ${{ steps.increment-release-tag.outputs.new_repo_version }}

echo "Logging latest created and pushed tag"
git describe --abbrev=0 --tags

curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GH_FG_PAT_TAGS }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/e-mission/${{ inputs.repo }}/releases \
-d '{"tag_name":"${{ steps.increment-release-tag.outputs.new_repo_version }}","target_commitish":"${{ inputs.branch }}","name":"${{ steps.increment-release-tag.outputs.new_repo_version }}","body":"Triggered on e-mission-server new release. Bumping up release version.","generate_release_notes":true}'

- name: Add, Commit, Push changes to .env file
if: ${{ github.ref_type == 'tag' || github.event_name == 'workflow_dispatch' }}
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
Loading
Loading