From d98f75c8ba36a8b69c547f30e8840b6af773c7a5 Mon Sep 17 00:00:00 2001 From: "Mahadik, Mukul Chandrakant" Date: Fri, 3 May 2024 13:07:19 -0700 Subject: [PATCH] Artifact + Matrix - 10 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added another TODO. evious Push event triggers run failed Error occurred in GitHub actions git add, commit, push step. If file with no changes operated upon, it leaves an error: “nothing to commit, working tree clean Error: Process completed with exit code 1.” Need to fix. —— Quick fix is to make changes to .env file only if workflow_dispatch event is the trigger. Don’t do anything for push event. So, in case anyone modifies .env file on their own by using their own timestamp during testing, and pushes it as a part of their PR, then Shankari will have to ask them to revert the changes. Else, their custom timestamp will make it to the repo code base. Found something: https://www.reddit.com/r/github/comments/ju3ipr/commit_from_github_action_only_when_changes_exist/ It should work but there’s a drawback of using “exit 0” - it will mask all errors generated during “git commit”. This is bad and we won’t be able to see the reason why something wrong happened as the workflow would be shown as successful with a green tick. Found a solution with git diff: https://github.com/simonw/til/blob/main/github-actions/commit-if-file-changed.md $ git diff --quiet || (git add README.md && git commit -m "Updated README") However, I won’t be able to log any message saying that no changes to commit, tag not modified. Hence, will possibly use just “git diff —quiet” with an if-else block. Expected results: - Push event triggers workflow. - It writes DOCKER_IMAGE_TAG_1 fetched from last successful completed run to .env file. - It sees that there is a difference in the latest committed .env file in the dashboard repo which includes older timestamp. - Hence it runs git commit part of the script to reset to latest server timestamp. --- .env | 2 +- .github/workflows/image_build_push.yml | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/.env b/.env index 03219ee..881e743 100644 --- a/.env +++ b/.env @@ -1 +1 @@ -DOCKER_IMAGE_TAG=2024-05-03--14-37 +DOCKER_IMAGE_TAG=2024-05-02--16-40 diff --git a/.github/workflows/image_build_push.yml b/.github/workflows/image_build_push.yml index fa584c6..1f4e446 100644 --- a/.github/workflows/image_build_push.yml +++ b/.github/workflows/image_build_push.yml @@ -75,6 +75,7 @@ jobs: with: name: docker-image-tag # TODO: Create a token with basic repo permissions + # TODO: Change user / organization to emission instead of MukuFlash03 github-token: ${{ secrets.GH_PAT_TAG }} repository: MukuFlash03/e-mission-server run-id: ${{ env.RUN_ID }} @@ -133,9 +134,13 @@ jobs: run: | git config --local user.email "action@github.com" git config --local user.name "GitHub Action" - git add .env - git commit -m "Updated docker image tag in .env to the latest timestamp" - git push origin + 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 tag in .env file to the latest timestamp" + git push origin + fi - name: docker login run: | # log into docker hub account