diff --git a/.github/workflows/deploy-docker-to-ont.yml b/.github/workflows/deploy-docker-to-ont.yml new file mode 100644 index 0000000..fd9a6fa --- /dev/null +++ b/.github/workflows/deploy-docker-to-ont.yml @@ -0,0 +1,135 @@ +name: Docker Image CI + +on: + push: + branches: + - feature/** + +jobs: + build-and-push: + runs-on: ubuntu-latest + + steps: + # Stap 1: Code ophalen + - uses: actions/checkout@v4 + + # Stap 2: Versienummer ophalen uit package.json en opslaan als artifact + - name: Extract Angular version + run: | + echo "$(cat package.json | jq -r '.version')" > version.txt + - name: Save version as artifact + uses: actions/upload-artifact@v3 + with: + name: version + path: version.txt + - name: Notify Mattermost via Bot + env: + 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": "@veenm 🚀 *Build gestart!* Een nieuwe build is begonnen voor de repository *'"$REPO"'* op branch *'"$BRANCH"'*." + }' \ + https://mattermost.melvanveen.nl/api/v4/posts + + + # Stap 3: 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 4: Docker-image bouwen en taggen met Angular-versie + - name: Build the Docker image + run: | + VERSION=$(cat version.txt) + docker buildx build . --file Dockerfile-tst --tag veenm/paypoint:$VERSION-SNAPSHOT --platform linux/amd64 + + # Stap 5: 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:$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 + + # 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_ONT }} + 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-frontend || true + docker rm paypoint-frontend || true + + # Haal de nieuwste image binnen + docker pull veenm/paypoint:$VERSION-SNAPSHOT + + # Start een nieuwe container + docker run -d --name paypoint-frontend --restart unless-stopped -p 15000:80 veenm/paypoint:$VERSION-SNAPSHOT + + # Opruimen oude images + docker image prune -f + + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Notify Mattermost via Bot + env: + VERSION: ${{ env.VERSION }} + run: | + COMMITS=$(git log $(git describe --tags --abbrev=0)..HEAD --pretty=format:"- %s") + curl --fail -X POST -H "Authorization: Bearer tspcwdn5rbdk8kkmnex6h1nfha" \ + -H 'Content-Type: application/json' \ + -d '{ + "channel_id": "9a8obynkd7rctk6qf8rfe6oppy", + "message": "@veenm ✅ *Build is geslaagd!* Versie '"$VERSION"'-SNAPSHOT staat klaar op ontwikkel." + }' \ + https://mattermost.melvanveen.nl/api/v4/posts + + notify-failure: + needs: [ build-and-push, deploy ] + runs-on: ubuntu-latest + if: failure() + + steps: + - name: Notify Mattermost via Bot on failure + env: + MATTERMOST_BOT_TOKEN: ${{ secrets.MATTERMOST_BOT_TOKEN }} + REPO: ${{ gitea.repository }} + BRANCH: ${{ gitea.ref }} + run: | + curl --fail -X POST -H "Authorization: Bearer $MATTERMOST_BOT_TOKEN" \ + -H 'Content-Type: application/json' \ + -d '{ + "channel_id": "9a8obynkd7rctk6qf8rfe6oppy", + "message": "@all ❌ *Build gefaald!* De pipeline is stukgelopen voor *'"$REPO"'* op branch *'"$BRANCH"'*." + }' \ + https://mattermost.melvanveen.nl/api/v4/posts