Skip to content

Commit

Permalink
Merge pull request #5 from lapig-ufg/develop
Browse files Browse the repository at this point in the history
homol novo
  • Loading branch information
James-jamames authored Oct 5, 2023
2 parents 8a8e68e + a373bf3 commit ba30e9d
Showing 1 changed file with 137 additions and 61 deletions.
198 changes: 137 additions & 61 deletions .github/workflows/homol.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build and Deploy in Homology Server
name: Build and Deploy in Homol Server Content Hub

on:
push:
Expand All @@ -8,117 +8,193 @@ on:
jobs:
build-and-deploy:
runs-on: ubuntu-latest
env:
REGISTRYSERVER: ${{secrets.REGISTRYHOMOL}}
SERVER_KNOWN: ${{secrets.SERVER_HOMOL_KNOWN}}
DOCKER_SERVER: ${{secrets.SERVER_HOMOL}}
SSH_SERVER: ${{secrets.SERVER_HOMOL_SSH}}
DOCKER_PATH: "homologation"


outputs:
START_TIME: ${{ steps.creat_env.outputs.START_TIME }}
LAST_COMMIT_HASH: ${{ steps.creat_env.outputs.LAST_COMMIT_HASH }}

LAPIG_AUTHOR_NAME: ${{ steps.creat_env.outputs.LAPIG_AUTHOR_NAME }}
LAPIG_AUTHOR_EMAIL: ${{ steps.creat_env.outputs.LAPIG_AUTHOR_EMAIL }}
LAPIG_AUTHOR_DATE: ${{ steps.creat_env.outputs.LAPIG_AUTHOR_DATE }}
LAPIG_PROJECT_NAME: ${{ steps.creat_env.outputs.LAPIG_PROJECT_NAME }}
LAPIG_COMMIT_DATE: ${{ steps.creat_env.outputs.LAPIG_COMMIT_DATE }}

CREATE_STATUS: ${{steps.deploy_container.outputs.CREATE_STATUS}}
DELETE_STATUS: ${{steps.deploy_container.outputs.DELETE_STATUS}}
START_STATUS: ${{steps.start_container.outputs.START_STATUS}}


steps:
- name: Checkout code
uses: actions/checkout@v2


- name: Define a variável application_name
id: creat_env
run: |
echo "branch=${GITHUB_REF##*/}" >> $GITHUB_ENV
echo "START_TIME=$(date -u +%s)" >> "$GITHUB_OUTPUT"
repo_full_name=$GITHUB_REPOSITORY
repo_name=${repo_full_name##*/}
echo "START_TIME=$(date -u +%s)" >> $GITHUB_ENV
echo "Nome do repositório: $repo_name"
echo "application_name=$repo_name" >> $GITHUB_ENV
echo "LAST_COMMIT_HASH=$(git log --pretty=format:%h -n 1)" >> $GITHUB_ENV
echo "LAPIG_AUTHOR_NAME=$(git show -s --pretty=%an)" >> $GITHUB_ENV
echo "LAPIG_AUTHOR_EMAIL=$(git show -s --pretty=%ae)" >> $GITHUB_ENV
echo "LAPIG_AUTHOR_DATE=$(git log -1 --format=%cd --date=local)" >> $GITHUB_ENV
echo "LAPIG_PROJECT_NAME=$(git config --local remote.origin.url)" >> $GITHUB_ENV
echo "LAPIG_LAST_COMMIT=$(git show --summary | grep 'commit' | awk '{print $2}')" >> $GITHUB_ENV
echo "LAPIG_COMMIT_COMMENT=$(git log -1 --pretty=%B)" >> $GITHUB_ENV
echo "LAPIG_COMMIT_DATE=$(git show -s --format=%ci)" >> $GITHUB_ENV
echo "LAPIG_BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_ENV
- name: Use Git outputs
run: |
echo "Author Name: $LAPIG_AUTHOR_NAME"
echo "Author Email: $LAPIG_AUTHOR_EMAIL"
echo "Author Date: $LAPIG_AUTHOR_DATE"
echo "Project Name: $LAPIG_PROJECT_NAME"
echo "Last Commit: $LAPIG_LAST_COMMIT"
echo "Commit Comment: $LAPIG_COMMIT_COMMENT"
echo "Commit Date: $LAPIG_COMMIT_DATE"
echo "Branch Name: $LAPIG_BRANCH_NAME"
echo "LAST_COMMIT_HASH=$(git log --pretty=format:%h -n 1)" >> "$GITHUB_OUTPUT"
echo "LAPIG_AUTHOR_NAME=$(git show -s --pretty=%an)" >> "$GITHUB_OUTPUT"
echo "LAPIG_AUTHOR_EMAIL=$(git show -s --pretty=%ae)" >> "$GITHUB_OUTPUT"
echo "LAPIG_AUTHOR_DATE=$(git log -1 --format=%cd --date=local)" >> "$GITHUB_OUTPUT"
echo "LAPIG_PROJECT_NAME=$repo_name" >> "$GITHUB_OUTPUT"
echo "LAPIG_COMMIT_DATE=$(git show -s --format=%ci)" >> "$GITHUB_OUTPUT"
- name: Create version.json
run: echo '{"commitId":"$LAST_COMMIT_HASH"}' > version.json
run: echo "{\"commitId\":\"$LAST_COMMIT_HASH\"}" > version.json

- name: Determine branch
id: get_branch
run: echo "::set-output name=branch::${GITHUB_REF##*/}"

- name: Validate and Build
- name: Build Docker
run: |
git pull origin develop
docker build -t ${{secrets.REGISTRYHOMOL}}/$application_name:${LAST_COMMIT_HASH} -t ${{secrets.REGISTRYHOMOL}}/$application_name:latest -f docker/homologation/Dockerfile . --no-cache
docker build -t $REGISTRYSERVER/$application_name:${LAST_COMMIT_HASH} -t $REGISTRYSERVER/$application_name:latest -f docker/$DOCKER_PATH/Dockerfile . --no-cache
- name: Login to Docker Registry
run: echo "${{ secrets.HARBOR_PASS }}" | docker login -u "${{ secrets.HARBOR_USER }}" --password-stdin "${{ secrets.URL_PRIVATE_REGISTRY }}"

- name: Push Image to Registry
run: |
docker push ${{secrets.REGISTRYHOMOL}}/$application_name:${LAST_COMMIT_HASH}
docker push ${{secrets.REGISTRYHOMOL}}/$application_name:latest
docker push $REGISTRYSERVER/$application_name:${LAST_COMMIT_HASH}
docker push $REGISTRYSERVER/$application_name:latest
- name: Remove image locally
run: |
docker rmi ${{secrets.REGISTRYHOMOL}}/$application_name:${LAST_COMMIT_HASH}
docker rmi ${{secrets.REGISTRYHOMOL}}/$application_name:latest
docker rmi $REGISTRYSERVER/$application_name:${LAST_COMMIT_HASH}
docker rmi $REGISTRYSERVER/$application_name:latest
- name: Add SSH key to known_hosts
run: |
mkdir -p ~/.ssh
ssh-keyscan -t rsa ${{secrets.SERVER_HOMOL_KNOWN}} >> ~/.ssh/known_hosts 2>/dev/null
ssh-keyscan -t rsa $SERVER_KNOWN >> ~/.ssh/known_hosts 2>/dev/null
chmod 644 ~/.ssh/known_hosts
echo "${{ secrets.SSH_KEY }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
- name: Pull Image on Server
run: |
ssh ${{secrets.SERVER_HOMOL_SSH}} "docker pull ${{secrets.REGISTRYHOMOL}}/$application_name:latest"
ssh $SSH_SERVER "docker pull $REGISTRYSERVER/$application_name:latest"
- name: Deploy Container
id: deploy_container
run: |
delete_status=$(ssh $SSH_SERVER "curl -v -X DELETE -w "%{http_code}" http://$DOCKER_SERVER/containers/$application_name?force=true 2>/dev/null")
create_status=$(ssh $SSH_SERVER "curl -v -X POST -H 'Content-Type: application/json' -d @${{secrets.CONFIG_FILES}}$application_name.json -s -o /dev/null -w "%{http_code}" http://$DOCKER_SERVER/containers/create?name=$application_name 2>/dev/null")
case $delete_status in
204) delete_status_text="OK";;
*) delete_status_text="ERROR";;
esac
delete_status=$(ssh ${{secrets.SERVER_HOMOL_SSH}} "curl -v -X DELETE -w "%{http_code}" http://${{secrets.SERVER_HOMOL}}/containers/$application_name?force=true 2>/dev/null")
echo "Delete Status: $delete_status"
create_status=$(ssh ${{secrets.SERVER_HOMOL_SSH}} "curl -v -X POST -H 'Content-Type: application/json' -d @${{secrets.CONFIG_FILES}}$application_name.json -s -o /dev/null -w "%{http_code}" http://${{secrets.SERVER_HOMOL}}/containers/create?name=$application_name 2>/dev/null")
echo "Create Status: $create_status"
case $create_status in
201) create_status_text="OK";;
*) create_status_text="ERROR";;
esac
echo "del $delete_status"
echo "creat $create_status"
echo "CREATE_STATUS=$create_status_text" >> "$GITHUB_OUTPUT"
echo "DELETE_STATUS=$delete_status_text" >> "$GITHUB_OUTPUT"
echo "CREATE_STATUS=$create_status" >> $GITHUB_ENV
echo "DELETE_STATUS=$delete_status" >> $GITHUB_ENV
- name: Start Container
id: start_container
run: |
start_status=$(ssh ${{secrets.SERVER_HOMOL_SSH}} "curl -v -X POST -s -o /dev/null -w "%{http_code}" http://${{secrets.SERVER_HOMOL}}/containers/$application_name/start 2>/dev/null")
echo "Start Status: $start_status"
echo "START_STATUS=$start_status" >> $GITHUB_ENV
start_status=$(ssh $SSH_SERVER "curl -v -X POST -s -o /dev/null -w "%{http_code}" http://$DOCKER_SERVER/containers/$application_name/start 2>/dev/null")
case $start_status in
204) start_status_text="OK";;
*) start_status_text="ERROR";;
esac
echo $start_status
echo "START_STATUS=$start_status_text" >> "$GITHUB_OUTPUT"

post-deploy:
runs-on: ubuntu-latest
needs: build-and-deploy
if: ${{ always() || needs.build-and-deploy.result == 'failure' }}
steps:
- name: Load env
run: |
echo "LAST_COMMIT_HASH=${{needs.build-and-deploy.outputs.LAST_COMMIT_HASH}}" >> $GITHUB_ENV
echo "LAPIG_AUTHOR_NAME=${{needs.build-and-deploy.outputs.LAPIG_AUTHOR_NAME}}" >> $GITHUB_ENV
echo "LAPIG_AUTHOR_EMAIL=${{needs.build-and-deploy.outputs.LAPIG_AUTHOR_EMAIL}}" >> $GITHUB_ENV
echo "LAPIG_AUTHOR_DATE=${{needs.build-and-deploy.outputs.LAPIG_AUTHOR_DATE}}" >> $GITHUB_ENV
echo "LAPIG_PROJECT_NAME=${{needs.build-and-deploy.outputs.LAPIG_PROJECT_NAME}}" >> $GITHUB_ENV
echo "LAPIG_COMMIT_DATE=${{needs.build-and-deploy.outputs.LAPIG_COMMIT_DATE}}" >> $GITHUB_ENV
echo "CREATE_STATUS=${{needs.build-and-deploy.outputs.CREATE_STATUS}}" >> $GITHUB_ENV
echo "DELETE_STATUS=${{needs.build-and-deploy.outputs.DELETE_STATUS}}" >> $GITHUB_ENV
echo "START_STATUS=${{needs.build-and-deploy.outputs.START_STATUS}}" >> $GITHUB_ENV
- name: Send message to Telegram
run: |
end_time=$(date -u +%s)
echo "LAPIG_DURATION=$((end_time - START_TIME))" >> $GITHUB_ENV
curl -s -X POST "https://api.telegram.org/bot${TOKEN}/sendMessage" -d "chat_id=${CHAT_ID}" -d "parse_mode=markdown" -d "text=*Project*: *${LAPIG_PROJECT_NAME}*\n*Branch*: ${LAPIG_BRANCH_NAME}\n*Author*: *${LAPIG_AUTHOR_NAME}*\n*Author_Email*: *${LAPIG_AUTHOR_EMAIL}*\n*Commit_ID*: *${LAPIG_LAST_COMMIT}*\n*Message_Commit*: *${LAPIG_COMMIT_COMMENT}*\n*Date_Commit*: *${LAPIG_COMMIT_DATE}*\n*Duration*: *${LAPIG_DURATION}*'"
- name: Send message to Discord
echo "LAPIG_DURATION=$((end_time - $START_TIME))" >> $GITHUB_ENV
curl -s -X POST "https://api.telegram.org/bot${{ secrets.TELEGRAM_API_KEY }}/sendMessage" -d "chat_id=${{ secrets.CHAT_ID }}" -d "parse_mode=markdown" -d "text=*Project*: *${LAPIG_PROJECT_NAME}*\
*Status:*${{ needs.build-and-deploy.result }}\
*Branch*: ${LAPIG_BRANCH_NAME}\
*Author*: *${LAPIG_AUTHOR_NAME}*\
*Author_Email*: *${LAPIG_AUTHOR_EMAIL}*\
*Commit_ID*: *${LAST_COMMIT_HASH}*\
*Date_Commit*: *${LAPIG_COMMIT_DATE}*\
*Duration*: *${LAPIG_DURATION}*"
- name: creat message to Discord
run: |
if [[ "${{ job.status }}" == "success" && "$START_STATUS" == "204" && "$DELETE_STATUS" == "204" && "$CREATE_STATUS" == "201" ]]; then
echo "avatar_url='${{vars.LAPIG_SUCCESS}}'" >> $GITHUB_ENV
if [[ "${{ needs.build-and-deploy.result }}" == "success" && "$START_STATUS" == "OK" && "$DELETE_STATUS" == "OK" && "$CREATE_STATUS" == "OK" ]]; then
echo "avatar_url=${{vars.LAPIG_SUCCESS}}" >> $GITHUB_ENV
status_geral="Sucesso"
else
echo "avatar_url='${{vars.LAPIG_ERROR}}'" >> $GITHUB_ENV
echo "avatar_url=${{vars.LAPIG_ERROR}}" >> $GITHUB_ENV
status_geral="Falhou"
fi
echo "discordDesc='Result: ${{ job.status }}\n Project: $LAPIG_PROJECT_NAME\n Commit: $LAPIG_LAST_COMMIT\n Author: $LAPIG_AUTHOR_NAME\n Author_Email: $LAPIG_AUTHOR_EMAIL\n Message: $LAPIG_COMMIT_COMMENT\n Date: $LAPIG_COMMIT_DATE\n Delete Status: $delete_status\n Create Status: $create_status\n Start Status: $start_status\n Duration: $LAPIG_DURATION seconds'" >> $GITHUB_ENV
case $START_STATUS in
"OK") START_STATUS_D="{ \"title\": \"DOCKER START OK!\",\"color\": 65280},";;
*) START_STATUS_D="{ \"title\": \"DOCKER START ERROR!\",\"color\": 16711680},";;
esac
case $DELETE_STATUS in
"OK") DELETE_STATUS_D="{ \"title\": \"DOCKER DELETE OK!\",\"color\": 65280},";;
*) DELETE_STATUS_D="{ \"title\": \"DOCKER DELETE ERROR!\",\"color\": 16711680},";;
esac
case $CREATE_STATUS in
"OK") CREATE_STATUS_D="{ \"title\": \"DOCKER CREATE OK!\",\"color\": 65280},";;
*) CREATE_STATUS_D="{ \"title\": \"DOCKER CREATE ERROR!\",\"color\": 16711680},";;
esac
echo "EMBEDS_STATUS=$DELETE_STATUS_D $CREATE_STATUS_D $START_STATUS_D" >> $GITHUB_ENV
echo "discordDesc=Result: $status_geral\nProject: $LAPIG_PROJECT_NAME\nCommit: $LAST_COMMIT_HASH\nAuthor: $LAPIG_AUTHOR_NAME\nAuthor_Email: $LAPIG_AUTHOR_EMAIL\nDate: $LAPIG_COMMIT_DATE\nDuration: $LAPIG_DURATION seconds" >> $GITHUB_ENV
echo "discordFooter='${{ github.workflow }} (#${{ github.run_number }})'" >> $GITHUB_ENV
echo "discordTitle='${{ github.workflow }} (build #${{ github.run_number }})'" >> $GITHUB_ENV
echo "discordFooter=${{ github.workflow }} (#${{ github.run_number }})" >> $GITHUB_ENV
echo "discordTitle=${{ github.workflow }} (build #${{ github.run_number }})" >> $GITHUB_ENV
curl -H "Content-Type: application/json" -X POST -d '{"content": "'"$discordTitle"'", "embeds": [ { "title": "'"$discordTitle"'", "description": "'"$discordDesc"'", "footer": { "text": "'"$discordFooter"'" } } ], "username": "GitHub Actions", "avatar_url": "'"$avatar_url"'" }' https://discord.com/api/webhooks/${{ secrets.DISCORD_KEY }}

- name: Send menssage to Discord
run: |
echo "{\"content\": \"$discordTitle\", \"embeds\": [ { \"title\": \"$discordTitle\", \"description\": \"$discordDesc\", \"footer\": { \"text\": \"$discordFooter\" }, \"thumbnail\": { \"url\":\"$avatar_url\"} }, $EMBEDS_STATUS] ], \"username\": \"GitHub Actions\", \"avatar_url\": \"$avatar_url\" }"
curl -H "Content-Type: application/json" -X POST -d "{\"content\": \"$discordTitle\", \"embeds\": [ $EMBEDS_STATUS { \"title\": \"$discordTitle\", \"description\": \"$discordDesc\", \"footer\": { \"text\": \"$discordFooter\" }, \"thumbnail\": { \"url\":\"$avatar_url\"} }], \"username\": \"GitHub Actions\", \"avatar_url\": \"$avatar_url\" }" https://discord.com/api/webhooks/${{ secrets.DISCORD_KEY }}

0 comments on commit ba30e9d

Please sign in to comment.