name: Build, Push, and Deploy to Nomad on: push: branches: - main jobs: docker-nomad: runs-on: self-hosted steps: - name: Check out code uses: actions/checkout@v3 - name: Log in to Container Registry run: echo ${{ secrets.password }} | docker login registry.i80.dk -u ${{ secrets.username }} --password-stdin - name: Build Docker Image run: | COMMIT_HASH=$(git rev-parse --short HEAD) docker build -t registry.i80.dk/gitea/lifefaq:latest -t registry.i80.dk/gitea/lifefaq:${COMMIT_HASH} . - name: Push Docker Image run: | COMMIT_HASH=$(git rev-parse --short HEAD) echo "registry.i80.dk/gitea/lifefaq:latest" echo "registry.i80.dk/gitea/lifefaq:${COMMIT_HASH}" docker push registry.i80.dk/gitea/lifefaq:${COMMIT_HASH} docker push registry.i80.dk/gitea/lifefaq:latest - name: Validate Nomad Job env: NOMAD_ADDR: https://nomad.i80.dk run: nomad job validate .gitea/workflows/nomad-job.hcl - name: Apply Nomad Job env: NOMAD_ADDR: https://nomad.i80.dk run: nomad job run .gitea/workflows/nomad-job.hcl - name: Wait for New Job to Become Healthy env: NOMAD_ADDR: https://nomad.i80.dk run: | echo "Waiting for the new job to become healthy..." # Fetch the latest allocation ID for the job NEW_ALLOC=$(nomad job status lifefaq | awk '/ID/ {getline; print $1}' | tail -n 1) echo "Latest allocation ID: $NEW_ALLOC" # Wait for the new allocation to become healthy until nomad alloc status $NEW_ALLOC | grep -q "Client Status.*running"; do echo "New allocation not healthy yet, retrying..." sleep 5 done echo "New allocation is healthy!" - name: Stop old deployment env: NOMAD_ADDR: https://nomad.i80.dk run: nomad job stop -purge -no-shutdown-delay lifefaq continue-on-error: true - name: Update Nginx Configuration run: ssh runner@nomad sudo /opt/nginx_updater/venv/bin/python3 /opt/nginx_updater/nginx_updater.py lifefaq - name: Update Forwarder Configuration run: ssh runner@nomad sudo /opt/nginx_updater/venv/bin/python3 /opt/nginx_updater/update_forwarder.py --subdomain lifefaq # - name: Restart Nomad Job # env: # NOMAD_ADDR: https://nomad.i80.dk # run: | # nomad job stop lifefaq # sleep 5 # Optional: Wait to ensure the old allocation is stopped # nomad job run .gitea/workflows/nomad-job.hcl