check status #5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: CD-Backend | |
on: | |
workflow_dispatch: | |
push: | |
branches: | |
- main | |
paths: | |
- server/** | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
services: | |
postgres: | |
image: postgres:15.3 | |
env: | |
POSTGRES_HOST: localhost | |
POSTGRES_USER: loctran | |
POSTGRES_PASSWORD: Loc123456 | |
POSTGRES_DB: blog | |
POSTGRES_PORT: 5432 | |
POSTGRES_HOST_AUTH_METHOD: password | |
options: >- | |
--health-cmd pg_isready | |
--health-interval 10s | |
--health-timeout 5s | |
--health-retries 5 | |
ports: | |
- 5432:5432 | |
defaults: | |
run: | |
working-directory: ./server | |
steps: | |
- uses: actions/checkout@v3 | |
- uses: actions/setup-java@v3 | |
with: | |
distribution: 'temurin' | |
java-version: '17' | |
cache: 'maven' | |
- uses: stCarolas/setup-maven@v.4.5 | |
with: | |
maven-version: 3.8.2 | |
- name: Login to docker | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Generate Build Number | |
id: build-number | |
run: echo "BUILD_NUMBER=$(date '+%d.%m.%Y.%H.%M.%S')" >> $GITHUB_OUTPUT | |
- name: Build Package with Maven | |
run: mvn -ntp -B verify -Ddocker.image.tag=${{steps.build-number.outputs.BUILD_NUMBER}} jib:build | |
- name: Update Dockerrun.aws.json image tag with new build number | |
run: | | |
sed -i -E 's_(loctran070699/blog-api:)([^"]*)_\1'${{steps.build-number.outputs.BUILD_NUMBER}}'_' ../Dockerrun.aws.json | |
cat ../Dockerrun.aws.json | |
- name: Deploy to EB | |
uses: einaregilsson/beanstalk-deploy@v21 | |
with: | |
aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
application_name: ${{ secrets.EB_APP_NAME }} | |
environment_name: ${{ secrets.EB_ENV_NAME }} | |
version_label: ${{ steps.build-number.outputs.BUILD_NUMBER }} | |
region: ${{ secrets.EB_REGION }} | |
deployment_package: server/Dockerrun.aws.json | |
- name: Commit and Push Dockerrun.aws.json | |
run: | | |
git config user.name github-actions | |
git config user.email github-actions@github.com | |
git add ../Dockerrun.aws.json | |
git commit -m "Update Dockerrun.aws.json docker image with new tag ${{steps.build-number.outputs.BUILD_NUMBER}}" | |
git push | |
- name: Send Telegram message on success | |
uses: appleboy/telegram-action@master | |
with: | |
to: ${{ secrets.TELEGRAM_BLOG_CHANEL_ID }} | |
token: ${{ secrets.TELEGRAM_BOT_TOKEN }} | |
message: | | |
<strong>SUCCESS ✅</strong> | |
Actor: <strong>${{ github.actor }}</strong> | |
Branch: <strong>${{ steps.extract_branch.outputs.branch }}</strong> | |
Repository: <strong>${{ github.repository }}</strong> | |
<a href="https://github.com/${{ github.repository }}/commit/${{github.sha}}"><strong>Open URL</strong></a> | |
format: html | |
disable_web_page_preview: true | |
- name: Check last job status | |
id: lastJobStatus | |
if: always() | |
run: | | |
LAST_JOB_STATUS=$(gh run list --workflow backend-cd.yaml | grep -oh "completed.*" | head -1 | awk '{print $2}') | |
THIS_JOB_STATUS="${{ job.status }}" | |
if [ "$LAST_JOB_STATUS" != "$THIS_JOB_STATUS" ]; then | |
echo "status changed from $LAST_JOB_STATUS to $THIS_JOB_STATUS" | |
echo "::set-output name=changedState::true" | |
echo "::set-output name=stateMessage::Test status changed from '$LAST_COMPLETED_JOB_STATUS' to '$THIS_JOB_STATUS'." | |
else | |
echo "status is still $THIS_JOB_STATUS" | |
echo "::set-output name=changedState::false" | |
echo "::set-output name=stateMessage::$THIS_JOB_STATUS" | |
fi | |
- name: Showcase output variable | |
if: always() && steps.lastJobStatus.outputs.changedState == 'true' | |
run: echo "CHANGED STATE!!!" | |
send-telegram-message: | |
needs: deploy | |
runs-on: ubuntu-latest | |
if: failure() | |
steps: | |
- name: Send Telegram message on fail | |
if: ${{ steps.check-status.outcome == 'failure' }} | |
uses: appleboy/telegram-action@master | |
with: | |
to: ${{ secrets.TELEGRAM_BLOG_CHANEL_ID }} | |
token: ${{ secrets.TELEGRAM_BOT_TOKEN }} | |
message: | | |
<strong >FAILURE ❌</strong> | |
Actor: <strong>${{ github.actor }}</strong> | |
Branch: <strong>${{ steps.extract_branch.outputs.branch }}</strong> | |
Repository: <strong>${{ github.repository }}</strong> | |
<a href="https://github.com/${{ github.repository }}/commit/${{github.sha}}"><strong>Open URL</strong></a> | |
format: html | |
disable_web_page_preview: true |