Skip to content

Deploy Application

Deploy Application #61

Workflow file for this run

name: Deployment to EC2 Instance
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)" > imageName.txt
cat imageName.txt
- name: Upload Artifacts
uses: actions/upload-artifact@v3
with:
name: docker-image-name
path: |
imageName.txt
build/libs/**
dockerfile
Docker-Upload:
needs: build-and-test
runs-on: ubuntu-latest
steps:
- 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-image-name
- name: Set variable with docker image name
run: |
content=$(cat ./imageName.txt)
echo "DOCKER_IMAGE_NAME=$content" >> $GITHUB_ENV
shell: bash
- name: Find and set JAR file
id: find_jar_file
run: |
JAR_FILE=$(find ./build/libs -type f -name "destiny2bot-*.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.find_jar_file.outputs.JAR_FILE }}
context: .
Deployment:
needs: Docker-Upload
runs-on: ubuntu-latest
steps:
- name: Pull Saved Artifacts
uses: actions/download-artifact@v3
with:
name: docker-image-name
- name: Set variable with docker image name
run: |
content=$(cat ./imageName.txt)
echo "DOCKER_IMAGE_NAME=$content" >> $GITHUB_ENV
shell: bash
- name: Excute Commands in EC2 instance
uses: appleboy/ssh-action@v1.0.0
with:
host: ${{ secrets.AWS_EC2_HOST }}
username: ${{ secrets.AWS_EC2_USERNAME }}
key: ${{ secrets.AWS_PRIVATE_KEY }}
script: |
# Change to correct directory
cd Projects/destiny2bot/
# Pull latest image
sudo docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_TOKEN }}
sudo docker pull ${{ env.DOCKER_IMAGE_NAME }} # Pull latest Destiny2Bot image
# Setup variables for bot application
echo "DISCORD_BOT_TOKEN=${{ secrets.DISCORD_BOT_TOKEN }}" > .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 "KEYSTORE_PASSWORD=${{ secrets.KEYSTORE_PASSWORD }}" >> .env
echo "DISCORD_BOT_PUBLIC_KEY=${{ secrets.DISCORD_BOT_PUBLIC_KEY }}" >> .env
echo "DISCORD_CLIENT_ID=${{ secrets.DISCORD_CLIENT_ID }}" >> .env
echo "DISCORD_CLIENT_SECRET=${{ secrets.DISCORD_CLIENT_SECRET }}" >> .env
echo "IMAGE_NAME=${{ env.DOCKER_IMAGE_NAME }}" >> .env
echo "MONGO_INITDB_ROOT_USERNAME=${{ secrets. MONGO_INITDB_ROOT_USERNAME }}" >> .env
echo "MONGO_INITDB_ROOT_PASSWORD=${{ secrets.MONGO_INITDB_ROOT_PASSWORD }}" >> .env
# Start up the cocker compose app
sudo docker compose up -d