diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml new file mode 100644 index 0000000..b4d0669 --- /dev/null +++ b/.github/workflows/dev.yml @@ -0,0 +1,80 @@ + +name: "Build and Push to GHCR" + +on: + push: + branches: + - main + - twitter_bot + +jobs: + Deploy: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Login to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ secrets.GHCR_USERNAME }} + password: ${{ secrets.GHCR_TOKEN }} + + - name: Build and Push Discord Bot Docker Image + if: github.ref == 'refs/heads/main' + run: | + export REPO_NAME=$(echo $GITHUB_REPOSITORY | tr '[:upper:]' '[:lower:]') + export CURRENT_BRANCH=${GITHUB_REF#refs/heads/} + export TAG=$([[ $CURRENT_BRANCH == $(basename ${{ github.ref }}) ]] && echo $CURRENT_BRANCH || echo "latest") + export GITHUB_REF_IMAGE=ghcr.io/$REPO_NAME:$GITHUB_SHA + export GITHUB_BRANCH_IMAGE=ghcr.io/$REPO_NAME:$TAG + docker build -t $GITHUB_REF_IMAGE -t $GITHUB_BRANCH_IMAGE . + echo "Pushing Image to GitHub Container Registry" + docker push $GITHUB_REF_IMAGE + docker push $GITHUB_BRANCH_IMAGE + + - name: Build and Push Twitter Bot Docker Image + if: github.ref == 'refs/heads/twitter_bot' + run: | + export REPO_NAME=$(echo $GITHUB_REPOSITORY | tr '[:upper:]' '[:lower:]') + export CURRENT_BRANCH=${GITHUB_REF#refs/heads/} + export TAG=$([[ $CURRENT_BRANCH == $(basename ${{ github.ref }}) ]] && echo $CURRENT_BRANCH || echo "latest") + export GITHUB_REF_IMAGE=ghcr.io/$REPO_NAME:$GITHUB_SHA + export GITHUB_BRANCH_IMAGE=ghcr.io/$REPO_NAME:$TAG + docker build -t $GITHUB_REF_IMAGE -t $GITHUB_BRANCH_IMAGE . + echo "Pushing Image to GitHub Container Registry" + docker push $GITHUB_REF_IMAGE + docker push $GITHUB_BRANCH_IMAGE + + - name: Deploy Docker on DEV server + if: github.ref == 'refs/heads/main' + uses: appleboy/ssh-action@v0.1.7 + with: + host: ${{ secrets.DEV_REMOTE_SERVER_ADDRESS }} + username: ${{ secrets.DEV_SERVER_USERNAME }} + key: ${{ secrets.DEV_REMOTE_SERVER_KEY }} + port: ${{ secrets.DEV_SSH_PORT }} + script: | + pwd + cd discord_bot + sudo docker stop netsepio-discord-bot && sudo docker rm netsepio-discord-bot && sudo docker image rm netsepio-discord-bot + echo ${{ secrets.GHCR_TOKEN }} | sudo docker login ghcr.io -u ${{ secrets.GHCR_USERNAME }} --password-stdin + sudo docker pull ghcr.io/netsepio/netsepio-discord-bot:main + sudo docker run --name="netsepio-discord-bot" --env-file=.env -p 9079:8080 --net=bridge -d ghcr.io/netsepio/netsepio-discord-bot:main + + - name: Deploy Docker on Twitter Bot DEV server + if: github.ref == 'refs/heads/twitter_bot' + uses: appleboy/ssh-action@v0.1.7 + with: + host: ${{ secrets.DEV_REMOTE_SERVER_ADDRESS }} + username: ${{ secrets.DEV_SERVER_USERNAME }} + key: ${{ secrets.DEV_REMOTE_SERVER_KEY }} + port: ${{ secrets.DEV_SSH_PORT }} + script: | + pwd + cd twitter_bot + sudo docker stop netsepio-twitter-bot && sudo docker rm netsepio-twitter-bot && sudo docker image rm ghcr.io/netsepio/netsepio-discord-bot:twitter_bot + echo ${{ secrets.GHCR_TOKEN }} | sudo docker login ghcr.io -u ${{ secrets.GHCR_USERNAME }} --password-stdin + sudo docker pull ghcr.io/netsepio/netsepio-discord-bot:twitter_bot + sudo docker run --name="netsepio-twitter-bot" --env-file=.env -p 9079:8080 --net=bridge -d ghcr.io/netsepio/netsepio-discord-bot:twitter_bot \ No newline at end of file