Deploy Application #82
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 Application | |
on: workflow_dispatch | |
jobs: | |
build-and-test: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout the project | |
uses: actions/checkout@v4 | |
- name: Set up JDK 21 for x64 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '21' | |
distribution: 'temurin' | |
- name: Setup Gradle | |
uses: gradle/gradle-build-action@v2.4.2 | |
- name: Run the Gradle Build | |
run: ./gradlew clean build | |
- name: Set Project name and version as environment variables | |
run: | | |
echo "${{ secrets.DOCKERHUB_USERNAME}}/$(./gradlew -q projectName):$(./gradlew -q version)" > image_name.txt | |
cat image_name.txt | |
- name: Upload Artifacts | |
uses: actions/upload-artifact@v3 | |
with: | |
name: docker-artifacts | |
path: | | |
build/libs/*.jar | |
image_name.txt | |
compose.yml | |
compose.prod.yml | |
docker-upload: | |
needs: build-and-test | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout the project | |
uses: actions/checkout@v4 | |
- name: Pull cached artifact | |
uses: actions/download-artifact@v3 | |
with: | |
name: docker-artifacts | |
- name: Login to Docker | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_TOKEN }} | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Download Image Name File | |
uses: actions/download-artifact@v3 | |
with: | |
name: docker-artifacts | |
- name: Set variable with docker image name | |
run: | | |
content=$(cat ./image_name.txt) | |
echo "DOCKER_IMAGE_NAME=$content" >> $GITHUB_ENV | |
shell: bash | |
- name: Find and set JAR file | |
id: SET_JAR_FILE | |
run: | | |
JAR_FILE=$(find ./build/libs -type f -name "rivenbot-*.jar") | |
if [ -z "$JAR_FILE" ]; then | |
echo "::debug::No JAR file found in build/libs with the specified pattern" | |
exit 1 | |
fi | |
echo "JAR_FILE=$JAR_FILE" >> "$GITHUB_OUTPUT" | |
shell: bash | |
- name: Build and Push | |
uses: docker/build-push-action@v5 | |
with: | |
push: true | |
platforms: linux/amd64,linux/arm64 | |
tags: ${{ env.DOCKER_IMAGE_NAME }} | |
build-args: JAR_FILE=${{ steps.SET_JAR_FILE.outputs.JAR_FILE }} | |
context: . | |
pre-deploy: | |
needs: docker-upload | |
runs-on: ubuntu-latest | |
steps: | |
- name: Pull Saved Artifacts | |
uses: actions/download-artifact@v3 | |
with: | |
name: docker-artifacts | |
- name: Set variable with docker image name | |
run: | | |
content=$(cat ./image_name.txt) | |
echo "DOCKER_IMAGE_NAME=$content" >> $GITHUB_ENV | |
shell: bash | |
- name: Prepare environment data | |
uses: appleboy/ssh-action@v1.0.0 | |
with: | |
host: ${{ secrets.DROPLET_HOST }} | |
username: ${{ secrets.DROPLET_USERNAME }} | |
key: ${{ secrets.DROPLET_PRIVATE_TOKEN }} | |
script: | | |
# Change to working directory | |
cd riven_of_a_thousand_servers/ | |
# Setup .env file | |
rm -f .env | |
echo "BUNGIE_API_KEY=${{ secrets.BUNGIE_API_KEY }}" > .env | |
echo "BUNGIE_CLIENT_ID=${{ secrets.BUNGIE_CLIENT_ID }}" >> .env | |
echo "BUNGIE_CLIENT_SECRET=${{ secrets.BUNGIE_CLIENT_SECRET }}" >> .env | |
echo "DOCKER_IMAGE_NAME=${{ env.DOCKER_IMAGE_NAME }}" >> .env | |
echo "DISCORD_BOT_PUBLIC_KEY=${{ secrets.DISCORD_BOT_PUBLIC_KEY }}" >> .env | |
echo "DISCORD_BOT_TOKEN=${{ secrets.DISCORD_BOT_TOKEN }}" >> .env | |
echo "DISCORD_CLIENT_ID=${{ secrets.DISCORD_CLIENT_ID }}" >> .env | |
echo "DISCORD_CLIENT_SECRET=${{ secrets.DISCORD_CLIENT_SECRET }}" >> .env | |
echo "DISCORD_APPLICATION_ID=${{ secrets.DISCORD_APPLICATION_ID }}" >> .env | |
echo "DISCORD_BOT_PUBLIC_KEY=${{ secrets.DISCORD_BOT_PUBLIC_KEY }}" >> .env | |
echo "MONGO_DB_ROOT_USERNAME=${{ secrets.MONGO_INITDB_ROOT_USERNAME }}" >> .env | |
echo "MONGO_DB_ROOT_PASSWORD=${{ secrets.MONGO_INITDB_ROOT_PASSWORD }}" >> .env | |
echo "GRAFANA_ADMIN_USERNAME=${{ secrets.GRAFANA_ADMIN_USERNAME }}" >> .env | |
echo "GRAFANA_ADMIN_PASSWORD=${{ secrets.GRAFANA_ADMIN_PASSWORD }}" >> .env | |
# Change permissions on .env file | |
chmod 600 .env | |
- name: Prepare Docker artifacts | |
uses: appleboy/scp-action@v0.1.7 | |
with: | |
host: ${{ secrets.DROPLET_HOST }} | |
username: ${{ secrets.DROPLET_USERNAME }} | |
key: ${{ secrets.DROPLET_PRIVATE_TOKEN }} | |
source: "compose.yml,compose.prod.yml" | |
target: ./riven_of_a_thousand_servers/ | |
deploy: | |
needs: pre-deploy | |
runs-on: ubuntu-latest | |
steps: | |
- name: Prepare environment data | |
uses: appleboy/ssh-action@v1.0.0 | |
with: | |
host: ${{ secrets.DROPLET_HOST }} | |
username: ${{ secrets.DROPLET_USERNAME }} | |
key: ${{ secrets.DROPLET_PRIVATE_TOKEN }} | |
script: | | |
# Checkout working directory | |
cd riven_of_a_thousand_servers/ | |
# Stop current Docker compose | |
docker compose down | |
# Start Docker compose | |
docker compose -f compose.yml -f compose.prod.yml up -d |