CD - Deploy Backend #12
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 - Deploy Backend | |
on: | |
workflow_dispatch: | |
push: | |
branches: | |
-main | |
paths: | |
- backend/** | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
services: | |
postgres: | |
image: postgres:15.4 | |
env: | |
POSTGRES_USER: amigoscode | |
POSTGRES_PASSWORD: password | |
POSTGRES_DB: customer | |
ports: | |
- 5332:5432 | |
options: >- | |
--health-cmd pg_isready | |
--health-interval 10s | |
--health-timeout 5s | |
--health-retries 5 | |
defaults: | |
run: | |
working-directory: ./backend | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Send Slack Message | |
run: > | |
curl -X POST -H 'Content-type: application/json' --data '{"text":"Deployment started :progress_bar: :fingerscrossed:"}' ${{ secrets.SLACK_WEBHOOK_URL }} | |
- uses: actions/setup-java@v4 | |
with: | |
distribution: 'temurin' | |
java-version: '19' | |
cache: 'maven' | |
- name: Login to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Set build number | |
id: build-number | |
run: echo "BUILD_NUMBER=$(date '+%d.%m.%Y.%H.%M.%S')" >> $GITHUB_OUTPUT | |
- name: Send Slack Message | |
run: > | |
curl -X POST -H 'Content-type: application/json' | |
--data ' | |
{"text":" :maven: Building with maven"} | |
' | |
${{ secrets.SLACK_WEBHOOK_URL }} | |
- name: Build Package with maven | |
run: mvn -ntp -B verify -D docker.image.tag=${{steps.build-number.outputs.BUILD_NUMBER}} jib:build | |
- name: Send Slack Message | |
run: > | |
curl -X POST -H 'Content-type: application/json' | |
--data ' | |
{"text":":docker: Image tag: ${{steps.build-number.outputs.BUILD_NUMBER}} pushed to docker hub"} | |
' | |
${{ secrets.SLACK_WEBHOOK_URL }} | |
- name: Update Dockerrun.aws.json api image tag with build number | |
run: | | |
echo "Dockerrun.aws.json before updating tag" | |
cat Dockerrun.aws.json | |
sed -i -E 's_(pg47101/amigoscode-api:)([^"]*)_\1'${{steps.build-number.outputs.BUILD_NUMBER}}'_' Dockerrun.aws.json | |
echo "Dockerrun.aws.json after updating tag" | |
cat Dockerrun.aws.json | |
- name: Send Slack Message | |
run: > | |
curl -X POST -H 'Content-type: application/json' | |
--data ' | |
{"text":":aws: Starting deployment to EBS"} | |
' | |
${{ secrets.SLACK_WEBHOOK_URL }} | |
- name: Deploy to Elastic Bean stalk | |
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_APPLICATION_NAME }} | |
environment_name: ${{ secrets.EB_ENVIRONMENT_NAME }} | |
version_label: ${{ steps.build-number.outputs.BUILD_NUMBER }} | |
version_description: ${{ github.SHA }} | |
region: ${{ secrets.EB_REGION }} | |
deployment_package: backend/Dockerrun.aws.json | |
- name: Send Slack Message | |
run: > | |
curl -X POST -H 'Content-type: application/json' | |
--data ' | |
{"text":":githubloading: Commiting to repo"} | |
' | |
${{ secrets.SLACK_WEBHOOK_URL }} | |
- name: Commit push Dockerrun.aws.json | |
run: | | |
git config user.name github-actions | |
git config user.email [email protected] | |
git add . | |
git commit -m "Update Dockerrun.aws.json docker image with new tag ${{ steps.build-number.outputs.BUILD_NUMBER }}" | |
git push | |
- name: Send Slack Message | |
run: > | |
curl -X POST -H 'Content-type: application/json' | |
--data ' | |
{"text":":party_blob Deployment and commit complete"} | |
' | |
${{ secrets.SLACK_WEBHOOK_URL }} | |