feat: use docker swarm on prod with 3 apps #9
Workflow file for this run
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: Backend CD Production | |
on: | |
workflow_dispatch: | |
push: | |
branches: | |
- be/feat/423 | |
concurrency: | |
group: ${{ github.ref }} | |
cancel-in-progress: true | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
environment: test | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
- name: Set up JDK 21 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: 21 | |
distribution: temurin | |
cache: gradle | |
- name: Cache Gradle | |
uses: actions/cache@v4 | |
with: | |
path: | | |
~/.gradle/caches | |
~/.gradle/wrapper | |
key: ${{ runner.os }}-gradle-${{ hashFiles('**/build.gradle') }} | |
restore-keys: | | |
${{ runner.os }}-gradle- | |
- name: Add execute permissions | |
working-directory: ./backend | |
run: chmod +x ./gradlew | |
- name: Create bootJar | |
working-directory: ./backend | |
run: ./gradlew bootJar | |
- name: Build Docker image | |
working-directory: ./backend | |
run: | | |
docker build \ | |
-f Dockerfile-prod \ | |
-t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }}:prod \ | |
. | |
- name: Login to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Push Docker image | |
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }}:prod | |
deploy: | |
needs: build | |
runs-on: ubuntu-latest | |
environment: prod | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
- name: Send docker compose | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SSH_HOST }} | |
username: ${{ secrets.SSH_USERNAME }} | |
key: ${{ secrets.SSH_KEY }} | |
strip_components: 2 | |
source: backend/docker/docker-compose-prod.yaml | |
target: ~/${{ github.event.repository.name }} | |
- name: Rename docker compose file | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SSH_HOST }} | |
username: ${{ secrets.SSH_USERNAME }} | |
key: ${{ secrets.SSH_KEY }} | |
script: | | |
cd ~/${{ github.event.repository.name }} | |
mv docker-compose-prod.yaml docker-compose.yaml | |
- name: Set up environment variables | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SSH_HOST }} | |
username: ${{ secrets.SSH_USERNAME }} | |
key: ${{ secrets.SSH_KEY }} | |
script: | | |
cd ~/${{ github.event.repository.name }} | |
echo "MYSQL_DATABASE=${{ secrets.MYSQL_DATABASE }}" > .env | |
echo "MYSQL_ROOT_PASSWORD=${{ secrets.MYSQL_ROOT_PASSWORD }}" >> .env | |
echo "JASYPT_PASSWORD=${{ secrets.JASYPT_PASSWORD }}" >> .env | |
- name: Run new docker compose | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SSH_HOST }} | |
username: ${{ secrets.SSH_USERNAME }} | |
key: ${{ secrets.SSH_KEY }} | |
script: | | |
cd ~/${{ github.event.repository.name }} | |
docker compose pull | |
STACK_EXISTS=$(docker stack ls --filter name=^${{ secrets.DOCKERHUB_IMAGE_NAME }}$ --format '{{.Name}}') | |
if [ -z "$STACK_EXISTS" ]; then | |
docker stack deploy -c docker-compose.yml ${{ secrets.DOCKERHUB_IMAGE_NAME }} | |
else | |
docker service update --image ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }}:prod ${{ secrets.DOCKERHUB_IMAGE_NAME }}_app | |
fi | |
docker image prune -af |