From 7f390cea4ecf3a767a732a3c103b8f2c1b9b811a Mon Sep 17 00:00:00 2001 From: "Mahadik, Mukul Chandrakant" Date: Tue, 1 Oct 2024 18:25:41 -0700 Subject: [PATCH] Task A-3: Image build/push on tags/release instead of on every push MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Modified server to use reusable workflow as well - Lots of repeated code in this workflow as well. 2. Added tags as push event triggers - Github documentation for pattern matching - https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions#patterns-to-match-branches-and-tags - v[0-9]+.[0-9]+.[0-9]+ 3. Using github context data to select correct branch “git push” command failed in reusable workflow since it wasn’t listing any branch and by default it picked up the tag version and not any branch name. Used ${{ github.event.base_ref }} that has the source branch from which the release tag was created. - https://github.com/orgs/community/discussions/27154 - https://github.com/orgs/community/discussions/26243 Needed to extract only branch name to ignore “ref/heads/“ Added a separate job that extracts this using bash commands - https://stackoverflow.com/questions/16623835/remove-a-fixed-prefix-suffix-from-a-string-in-bash ——— 4. Server workflow runs on both push:branches and push:tags events. This is because we want tests to run before push. Using “github.ref_type” for checking whether event type was “branch” or “tag” - Documentation - https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/accessing-contextual-information-about-workflow-runs Server run was failing: "git push” needs the branch name - https://github.com/MukuFlash03/e-mission-server/actions/runs/11134355511/job/30942421505 - Add branch name in reusable workflow The error message in the run logs suggest using this: - https://github.com/MukuFlash03/e-mission-server/actions/runs/11134355511/job/30942421505 - $ git push origin HEAD: ———— 5. Using github.ref_type to check “branch” or “tag” and select correct branch When server release is created, it triggers dispatch to other workflows. However these don’t have “github.ref_type” = “tag” in this case but instead “github.ref_type” = “branch”. This case github.event.base_ref value to be empty, hence branch name extraction didn’t work. I’m using the ref_type to pass either GITHUB_REF or the extraction logic using github.event.base_ref. This is done in the workflows that call reusable workflow. The check should be fine since ref_type has only two values: branch or tag - https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/accessing-contextual-information-about-workflow-runs --- .github/workflows/image_build_push.yml | 75 +++++++------------ .../workflows/reusable_image_build_push.yml | 40 +++++----- 2 files changed, 47 insertions(+), 68 deletions(-) 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