Revert "Update deploy.yml" #13
This file contains hidden or 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: Build and Deploy Dockerized Bot | |
| on: | |
| push: | |
| branches: | |
| - 'master' # Any branch -> development deploy | |
| tags: | |
| - 'v*' # Any tag starting with v -> production deploy | |
| jobs: | |
| build-and-deploy: | |
| runs-on: ubuntu-latest | |
| # 👇 Dynamically select the right environment | |
| environment: ${{ startsWith(github.ref, 'refs/tags/') && 'prod' || 'dev' }} | |
| steps: | |
| # safeguard: make sure tag is from master | |
| - name: Ensure tag is on master | |
| if: startsWith(github.ref, 'refs/tags/') | |
| run: | | |
| BRANCH=$(git branch --contains $GITHUB_SHA) | |
| if [[ "$BRANCH" != *master* ]]; then | |
| echo "Tag is not on master branch. Aborting." | |
| exit 1 | |
| fi | |
| # Checkout your code | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Generate version.js | |
| run: | | |
| COMMIT_ID=$(git rev-parse --short HEAD | cut -c1-7) | |
| YEAR=$(date +%Y) | |
| VERSION=$(jq -r .version package.json) | |
| TAG=$(jq -r .version_tag package.json) | |
| if [[ "${GITHUB_REF}" == refs/tags/* ]]; then | |
| SUFFIX="prod" | |
| else | |
| SUFFIX="dev" | |
| fi | |
| if [ "$TAG" != "null" ]; then | |
| FULL_VERSION="${YEAR}.${VERSION}-${TAG}-${SUFFIX}" | |
| else | |
| FULL_VERSION="${YEAR}.${VERSION}-${SUFFIX}" | |
| fi | |
| echo "const version = \"$FULL_VERSION\"; const commit_id = \"$COMMIT_ID\"; module.exports = { version, commit_id };" > src/version.js | |
| # Set up Docker Buildx (for build cache + multi-platform) | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| # Log in to GitHub Container Registry (GHCR) | |
| - name: Log in to GHCR | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.PACKAGE_TOKEN }} | |
| # Build and push image (dev or prod tag automatically) | |
| - name: Build and Push Docker image | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: . | |
| push: true | |
| tags: | | |
| ghcr.io/050soft/musicbot:${{ startsWith(github.ref, 'refs/tags/') && 'prod' || 'dev' }} | |
| # Deploy to your remote server via SSH | |
| - name: Deploy to Server | |
| uses: appleboy/[email protected] | |
| with: | |
| host: ${{ secrets.SERVER_IP }} | |
| username: ${{ secrets.SERVER_USER }} | |
| key: ${{ secrets.SERVER_SSH_KEY }} | |
| script: | | |
| # Determine environment | |
| if [[ "${GITHUB_REF}" == refs/tags/* ]]; then | |
| TAG="prod" | |
| NAME="mybot-prod" | |
| PORT=8080 | |
| echo "🚀 Deploying PRODUCTION" | |
| else | |
| TAG="dev" | |
| NAME="mybot-dev" | |
| PORT=8081 | |
| echo "🧩 Deploying DEVELOPMENT" | |
| fi | |
| echo "${{ secrets.PACKAGE_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin | |
| # Pull latest image | |
| docker pull ghcr.io/050soft/musicbot:$TAG | |
| # Stop and remove old container if exists | |
| docker stop $NAME || true | |
| docker rm $NAME || true | |
| # Run new container with environment secrets | |
| docker run -d --name $NAME \ | |
| -e TOKEN=${{ secrets.TOKEN }} \ | |
| -e PUBLIC_KEY=${{ secrets.PUBLIC_KEY }} \ | |
| -e CLIENT_SECRET=${{ secrets.CLIENT_SECRET }} \ | |
| -e LASTFM_API_KEY=${{ secrets.LASTFM_API_KEY }} \ | |
| -e LASTFM_SHARED_SECRET=${{ secrets.LASTFM_SHARED_SECRET }} \ | |
| -e MONGODB_DATABASE_URL=${{ secrets.MONGODB_DATABASE_URL }} \ | |
| -e MONGODB_DATABASE_USER=${{ secrets.MONGODB_DATABASE_USER }} \ | |
| -e MONGODB_DATABASE_PASSWORD=${{ secrets.MONGODB_DATABASE_PASSWORD }} \ | |
| -p $PORT:8080 \ | |
| ghcr.io/050soft/musicbot:$TAG |