diff --git a/.github/workflows/image_build_push.yml b/.github/workflows/image_build_push.yml index bbc092cd8..89d8eed16 100644 --- a/.github/workflows/image_build_push.yml +++ b/.github/workflows/image_build_push.yml @@ -3,6 +3,8 @@ name: docker image on: push: branches: [ master, gis-based-mode-detection ] + tags: + - "v[0-9]+.[0-9]+.[0-9]+" #Dockerhub credentials are set as environment variables env: @@ -16,60 +18,33 @@ jobs: test-with-manual-install: uses: e-mission/e-mission-server/.github/workflows/test-with-manual-install.yml@master - build: + get-branch-name: runs-on: ubuntu-latest - needs: [test-with-docker, test-with-manual-install] - outputs: - date: ${{ steps.date.outputs.date }} - + branch_name: ${{ steps.get-branch-name.outputs.branch_name }} 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 "date=$(date +'%Y-%m-%d--%M-%S')" >> "$GITHUB_OUTPUT" + - name: Get branch name based on ref_type + id: get-branch-name + run: | + echo "Ref: ${{ github.ref }}" + echo "Ref name: ${{ github.ref_name }}" + echo "Ref type: ${{ github.ref_type }}" + echo "Github event base ref: ${{ github.event.base_ref }}" + if [ "${{ github.ref_type }}" == "tag" ]; then + base_ref=${{ github.event.base_ref }} + echo "branch_name=${base_ref#refs/heads/}" >> "$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: Update .env file - run: | - echo "SERVER_IMAGE_TAG=${GITHUB_REF##*/}_${{ steps.date.outputs.date }}" > .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" - 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 + build: + if: ${{ !contains(github.event.head_commit.author.name, 'Github Actions bot to update .env with latest tags') && github.ref_type == 'tag' }} + 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 diff --git a/.github/workflows/reusable_image_build_push.yml b/.github/workflows/reusable_image_build_push.yml index 5de4eb7eb..6cc591f88 100644 --- a/.github/workflows/reusable_image_build_push.yml +++ b/.github/workflows/reusable_image_build_push.yml @@ -25,21 +25,21 @@ jobs: ref: ${{ inputs.branch }} token: ${{ secrets.GH_FG_PAT_TAGS }} - - name: Fetch server image tag - id: get-server-tag + - name: Fetch latest server image tag + if: ${{ inputs.repo == 'op-admin-dashboard' || inputs.repo == 'em-public-dashboard' }} 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 + 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" - 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 + if [ "${{ inputs.repo }}" = "e-mission-server" ]; then + echo "SERVER_IMAGE_TAG=${SERVER_IMAGE_TAG}" >> "$GITHUB_OUTPUT" + elif [ "${{ 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" @@ -47,11 +47,13 @@ jobs: 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 + 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 }}" @@ -75,7 +77,9 @@ jobs: - name: build docker image run: | - if [ "${{ inputs.repo }}" = "nrel-openpath-join-page" ]; then + if [ "${{ inputs.repo }}" = "e-mission-server" ]; then + docker build -t $DOCKER_USER/${{ inputs.repo }}:${{ inputs.branch }}_${{ steps.date.outputs.date }} . + elif [ "${{ 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 @@ -97,18 +101,20 @@ jobs: - 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 [ "${{ 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 }} + else + docker push $DOCKER_USER/${{ inputs.repo }}:${{ inputs.branch }}_${{ steps.date.outputs.date }} fi - + - name: Update .env file run: | - if [ "${{ inputs.repo }}" = "nrel-openpath-join-page" ]; then + if [ "${{ inputs.repo }}" = "e-mission-server" ]; then + echo "SERVER_IMAGE_TAG=${{ inputs.branch }}_${{ steps.date.outputs.date }}" > .env + elif [ "${{ 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 @@ -130,7 +136,6 @@ jobs: 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 @@ -138,4 +143,3 @@ jobs: git commit -m "Updated docker image tags in .env file to the latest timestamp" git push origin fi - cat .env