[[wiki:github_cli|gh-cli]]\\ ==Show Secrets== echo "${{secrets.PASSWORD}}" | sed 's/./& /g' Name: Deploy Dev on: pull_request: branches: [ dev ] push: branches: [dev ] workflow_dispatch: inputs: run_deploy: description: "run Deploy" required: true type: boolean default: true run_test: description: "run Test" required: true type: boolean default: true env: GIT_BRANCH: ${{ github.ref_name }} PROJECT_DIR: /opt/project SSH_HOST: 10.0.0.1 SSH_user: app SSH_KEY: ${{ secrets.DEV_KEY }} SLACK_CHANNEL: XXXXX SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} SLACK_PREFIX: "*Notification*" jobs: deploy: name: Deploy Dev runs-on: ubuntu-latest steps: - name: Get Code uses: appleboy/ssh-action@master with: host: ${{ env.SSH_HOST }} username: ${{ env.SSH_USER }} key: ${{ env.SSH_KEY }} script_stop: true script: | cd ${{ env.PROJECT_DIR}} git fetch --all git reset --hard origin/${{ env.GIT_BRANCH }} sed -i 's/3000:3000/3005:3000/' docker-compose.yml echo "API_URL=${{ env.API_URL }}" > .env - name: Deploy Docker uses: appleboy/ssh-action@master with: host: ${{ env.SSH_HOST }} username: ${{ env.SSH_USER }} key: ${{ env.SSH_KEY }} command_timeout: 30m script_stop: true script: | cd cd ${{ env.PROJECT_DIR }} docker build -t "project" . docker ps -qf name="project" | xargs -r docker stop docker run --rm -d -p3000:80 --name project project docker system prune -f sleep 5 if [ -z $(docker ps -qf name="project") ]; then echo 'ERROR - Container failed to start'; false; fi; - name: Deploy Docker Compose uses: appleboy/ssh-action@master with: host: ${{ env.SSH_HOST }} username: ${{ env.SSH_USER }} key: ${{ env.SSH_KEY }} script_stop: true script: | cd ${{ env.PROJECT_DIR }} docker system prune -f docker compose build docker compose down docker compose up -d - name: Slack Message uses: slackapi/slack-github-action@v1.19.0 if: always() with: channel-id: ${{ env.SLACK_CHANNEL }} payload: | { "text": "Build result on ${{ env.SLACK_PREFIX }}: *${{ job.status }}*\n${{ github.event.pull_request.html_url || github.event.head_commit.url }}", "blocks": [ { "type": "section", "text": { "type": "mrkdwn", "text": "Build result on ${{ env.SLACK_PREFIX }}: *${{ job.status }}*\n${{ github.event.pull_request.html_url || github.event.head_commit.url }}" } } ] } - name: 'Get vars' run: | for GROUP in $(gcloud compute instance-groups unmanaged list --filter="name~'${{ env.APP_NAME }}-group-${{ env.ENVIRONMENT }}-*'" --format="value[separator='#--zone='](name,zone)"); do for INSTANCE in $(gcloud compute instance-groups unmanaged list-instances ${GROUP//#/ } --format="value(instance)"); do INSTANCE_HOST="$INSTANCE_HOST$(gcloud compute instances list --filter=$INSTANCE --format 'get(networkInterfaces[0].accessConfigs[0].natIP)')," done done echo 'INSTANCE_HOST<> $GITHUB_ENV echo $INSTANCE_HOST >> $GITHUB_ENV echo 'EOF' >> $GITHUB_ENV - name: Cloudflare Clear Cache env: CLOUDFLARE_API_TOKEN: ${{ secrets.CF_PURGE_TOKEN }} run: | curl -X POST "https://api.cloudflare.com/client/v4/zones/xxxxxxxxxxxxxxxxxxx/purge_cache" \ -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ -H "Content-Type: application/json" \ --data '{"purge_everything":true}' test: name: Run Test needs: [deploy] if: >- always() && needs.deploy.result == 'skipped' && inputs.run_test || needs.deploy.result == 'success' && github.event_name == 'push' || needs.deploy.result == 'success' && inputs.run_test runs-on: ubuntu-latest steps: ... docker: name: Build Image runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v3 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Build and push uses: docker/build-push-action@v5 with: file: .github/Dockerfile push: true tags: user/app:latest secrets: | GIT_AUTH_TOKEN=${{ secrets.DOCKERHUB_TOKEN }}