diff --git a/.github/workflows/deploy-docker-to-tst.yml b/.github/workflows/deploy-docker-to-tst.yml new file mode 100644 index 0000000..43af209 --- /dev/null +++ b/.github/workflows/deploy-docker-to-tst.yml @@ -0,0 +1,139 @@ +name: Docker Image CI + +on: + push: + branches: + - develop + tags: + - "docker-build-*" + +jobs: + build-and-push: + runs-on: ubuntu-latest + + steps: + # Stap 1: Code ophalen + - uses: actions/checkout@v4 + + # Stap 2: Versienummer ophalen uit pom.xml en opslaan als artifact + - name: Extract Quarkus version from pom.xml + run: | + VERSION=$(xmlstarlet sel -t -v "/project/version" pom.xml) + echo $VERSION > version.txt + - name: Save version as artifact + uses: actions/upload-artifact@v3 + with: + name: version + path: version.txt + + # Stap 3: Notify Mattermost via Bot (Build gestart) + - name: Notify Mattermost via Bot + env: + VERSION: ${{ env.VERSION }} + REPO: ${{ gitea.repository }} + BRANCH: ${{ gitea.ref }} + MATTERMOST_BOT_TOKEN: ${{ secrets.MATTERMOST_BOT_TOKEN }} + run: | + curl --fail -X POST -H "Authorization: Bearer $MATTERMOST_BOT_TOKEN" \ + -H 'Content-Type: application/json' \ + -d '{ + "channel_id": "9a8obynkd7rctk6qf8rfe6oppy", + "message": "@all 🚀 *Build gestart!* Een nieuwe build ['"$VERSION"'-SNAPSHOT] is begonnen voor de repository *'"$REPO"'* op branch *'"$BRANCH"'*." + }' \ + https://mattermost.melvanveen.nl/api/v4/posts + + # Stap 4: Inloggen bij Docker Hub + - name: Log in to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + # Stap 5: Quarkus JAR bouwen (via Maven) en Docker-image bouwen + - name: Build the Quarkus JAR and Docker image + run: | + VERSION=$(cat version.txt) + mvn clean package -DskipTests + docker buildx build . --file Dockerfile --tag veenm/paypoint-backend-jvm:$VERSION-SNAPSHOT --platform linux/amd64 + + # Stap 6: Docker-image pushen naar Docker Hub (huidige versie tag) + - name: Push the Docker image (version-snapshot) + run: | + VERSION=$(cat version.txt) + docker push veenm/paypoint-backend-jvm:$VERSION-SNAPSHOT + + deploy: + needs: build-and-push + runs-on: ubuntu-latest + + steps: + # Stap 1: Artifact ophalen + - name: Download version artifact + uses: actions/download-artifact@v3 + with: + name: version + + # Stap 2: Lees versie uit het artifact + - name: Read version + id: read_version + run: echo "VERSION=$(cat version.txt)" >> $GITHUB_ENV + + - name: Inject private key into resources + run: | + mkdir -p src/main/resources + echo "${{ secrets.PRIVATE_KEY }}" > src/main/resources/privateKey.pem + chmod 600 src/main/resources/privateKey.pem + + - name: Inject public key into resources + run: | + mkdir -p src/main/resources + echo "${{ secrets.PUBLIC_KEY }}" > src/main/resources/publicKey.pem + chmod 600 src/main/resources/publicKey.pem + + # Stap 3: Maak verbinding via SSH naar de Alpine server en update de container + - name: SSH into Alpine and update Docker container + uses: appleboy/ssh-action@v0.1.10 + with: + host: ${{ secrets.ALPINE_HOST }} + username: ${{ secrets.ALPINE_USER }} + password: ${{ secrets.ALPINE_PASSWORD }} + script: | + VERSION=${{ env.VERSION }} + echo "Gekozen versie: $VERSION-SNAPSHOT" + + # Stop en verwijder de huidige container + docker stop paypoint-backend || true + docker rm paypoint-backend || true + + # Haal de nieuwste image binnen + docker pull veenm/paypoint-backend-jvm:$VERSION-SNAPSHOT + + # Start een nieuwe container + docker run -d --name paypoint-backend --restart unless-stopped -p 15000:8080 \ + -e DB_HOST=${{ secrets.ALPINE_HOST }} \ + -e DB_PORT=${{ secrets.DB_PORT_TEST }} \ + -e DB_USERNAME=${{ secrets.DB_USERNAME_TEST }} \ + -e DB_PASSWORD=${{ secrets.DB_PASSWORD_TEST }} \ + -e CORS_ORIGINS=${{ secrets.CORS_ORIGINS_TEST }} \ + -e MAILER_FROM=${{ secrets.MAILER_FROM }} \ + -e MAILER_HOST=${{ secrets.MAILER_HOST }} \ + -e MAILER_PORT=${{ secrets.MAILER_PORT }} \ + -e MAILER_USERNAME=${{ secrets.MAILER_USERNAME }} \ + -e MAILER_PASSWORD=${{ secrets.MAILER_PASSWORD }} \ + veenm/paypoint-backend-jvm:$VERSION-SNAPSHOT + + # Opruimen oude images + docker image prune -f + + # Stap 4: Notify Mattermost via Bot (Build is geslaagd) + - name: Notify Mattermost via Bot + env: + VERSION: ${{ env.VERSION }} + run: | + curl --fail -X POST -H "Authorization: Bearer $MATTERMOST_BOT_TOKEN" \ + -H 'Content-Type: application/json' \ + -d '{ + "channel_id": "9a8obynkd7rctk6qf8rfe6oppy", + "message": "@all ✅ *Build is geslaagd!* Versie '"$VERSION"'-SNAPSHOT staat klaar op https://test-paypoint.melvanveen.nl" + }' \ + https://mattermost.melvanveen.nl/api/v4/posts