update cd.yml #23
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: Deploy | |
on: | |
push: | |
branches: | |
- main | |
env: | |
IMAGE_NAME: ${{ github.event.repository.name }} | |
SSH_HOST: ${{ secrets.SSH_HOST }} | |
SSH_USER: ${{ secrets.SSH_USER }} | |
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} | |
DB_USER_NAME: ${{ secrets.DB_USER_NAME }} # Add here environment variables first and then when reference it when you build the docker image | |
DB_USER_PASSWORD: ${{ secrets.DB_USER_PASSWORD }} # Add here environment variables first and then when reference it when you build the docker image | |
jobs: | |
build-and-push: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Login to GitHub Container Registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.repository_owner }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- id: lower-repo | |
name: Repository to lowercase | |
run: | | |
echo "repository=${GITHUB_REPOSITORY@L}" >> $GITHUB_OUTPUT | |
- name: Build & push docker image | |
uses: docker/build-push-action@v6 | |
with: | |
context: . | |
push: true | |
platforms: linux/amd64,linux/arm64 # Specify the platforms you need | |
tags: ghcr.io/${{ steps.lower-repo.outputs.repository }}:latest | |
file: Dockerfile # Adjust if your Dockerfile is not in the root directory | |
build-args: | | |
DB_USER_NAME=${{ env.DB_USER_NAME }} | |
DB_USER_PASSWORD=${{ env.DB_USER_PASSWORD }} | |
#Add here your environment Variables which you implemented above | |
- name: Image digest | |
run: echo ${{ steps.build-and-push.outputs.digest }} | |
- name: Copy Docker Compose file to server | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ env.SSH_HOST }} | |
username: ${{ env.SSH_USER }} | |
key: ${{ env.SSH_PRIVATE_KEY }} | |
source: "docker-compose.yml" | |
target: "~/" | |
- name: Copy SQL file to server | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ env.SSH_HOST }} | |
username: ${{ env.SSH_USER }} | |
key: ${{ env.SSH_PRIVATE_KEY }} | |
source: "src/main/resources/db/migration/V0_9_0__initial_db_creation.sql" | |
target: "~/" | |
- name: Pull image and start container | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ env.SSH_HOST }} | |
username: ${{ env.SSH_USER }} | |
key: ${{ env.SSH_PRIVATE_KEY }} | |
script: | | |
sed -i 's/DB_USER_NAME/${{ env.DB_USER_NAME }}/g' ~/docker-compose.yml | |
sed -i 's/DB_USER_PASSWORD/${{ env.DB_USER_PASSWORD }}/g' ~/docker-compose.yml | |
docker compose down | |
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u $ --password-stdin | |
docker compose up -d | |
# docker compose down -v | -v removes all volumes as well |