|
| 1 | +name: Release |
| 2 | +permissions: |
| 3 | + packages: write |
| 4 | + contents: write |
| 5 | +on: |
| 6 | + # Triggered on new GitHub Release |
| 7 | + release: |
| 8 | + types: [published] |
| 9 | + # Triggered on every successful Build action |
| 10 | + workflow_run: |
| 11 | + workflows: ["Build"] |
| 12 | + branches: [main,master] |
| 13 | + types: |
| 14 | + - completed |
| 15 | + # Manual trigger for rollback to specific release or redeploy latest |
| 16 | + workflow_dispatch: |
| 17 | + inputs: |
| 18 | + version: |
| 19 | + default: latest |
| 20 | + description: Tag you want to release. |
| 21 | + required: true |
| 22 | + |
| 23 | +jobs: |
| 24 | + push_to_registry: |
| 25 | + runs-on: ubuntu-22.04 |
| 26 | + if: ${{ github.event.workflow_run.conclusion != 'failure' }} |
| 27 | + steps: |
| 28 | + # Checkout latest or specific tag |
| 29 | + - name: checkout |
| 30 | + if: ${{ github.event.inputs.version == '' || github.event.inputs.version == 'latest' }} |
| 31 | + uses: actions/checkout@v3 |
| 32 | + - name: checkout tag |
| 33 | + if: ${{ github.event.inputs.version != '' && github.event.inputs.version != 'latest' }} |
| 34 | + uses: actions/checkout@v3 |
| 35 | + with: |
| 36 | + ref: refs/tags/${{ github.event.inputs.version }} |
| 37 | + |
| 38 | + # Assign environment variables used in subsequent steps |
| 39 | + - name: Env variable assignment |
| 40 | + run: echo "image_repository_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV |
| 41 | + # TAG_NAME defaults to 'latest' if not a release or manual deployment |
| 42 | + - name: Assign version |
| 43 | + run: | |
| 44 | + echo "TAG_NAME=latest" >> $GITHUB_ENV |
| 45 | + if [ "${{ github.event.release.tag_name }}" != "" ]; then |
| 46 | + echo "TAG_NAME=${{ github.event.release.tag_name }}" >> $GITHUB_ENV |
| 47 | + fi; |
| 48 | + if [ "${{ github.event.inputs.version }}" != "" ]; then |
| 49 | + echo "TAG_NAME=${{ github.event.inputs.version }}" >> $GITHUB_ENV |
| 50 | + fi; |
| 51 | +
|
| 52 | + - name: Setup node |
| 53 | + uses: actions/setup-node@v3 |
| 54 | + with: |
| 55 | + node-version: 18 |
| 56 | + # Run postinstall script |
| 57 | + - name: Install and build npm |
| 58 | + run: | |
| 59 | + npm install |
| 60 | + working-directory: ./ExampleOpenAiPlugin |
| 61 | + |
| 62 | + # Publish .NET Project |
| 63 | + - name: Publish dotnet project |
| 64 | + working-directory: ./ExampleOpenAiPlugin |
| 65 | + run: | |
| 66 | + dotnet publish -c Release /p:APP_TASKS=prerender |
| 67 | +
|
| 68 | + # Authenticate, build and push to GitHub Container Registry (ghcr.io) |
| 69 | + - name: Login to GitHub Container Registry |
| 70 | + uses: docker/login-action@v2 |
| 71 | + with: |
| 72 | + registry: ghcr.io |
| 73 | + username: ${{ github.actor }} |
| 74 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 75 | + |
| 76 | + # Build and push new docker image, skip for manual redeploy other than 'latest' |
| 77 | + - name: Build and push Docker images |
| 78 | + uses: docker/build-push-action@v3 |
| 79 | + if: ${{ github.event.inputs.version == '' || github.event.inputs.version == 'latest' }} |
| 80 | + with: |
| 81 | + file: Dockerfile |
| 82 | + context: . |
| 83 | + push: true |
| 84 | + tags: ghcr.io/${{ env.image_repository_name }}:${{ env.TAG_NAME }} |
| 85 | + |
| 86 | + deploy_via_ssh: |
| 87 | + needs: push_to_registry |
| 88 | + runs-on: ubuntu-22.04 |
| 89 | + if: ${{ github.event.workflow_run.conclusion != 'failure' }} |
| 90 | + steps: |
| 91 | + # Checkout latest or specific tag |
| 92 | + - name: checkout |
| 93 | + if: ${{ github.event.inputs.version == '' || github.event.inputs.version == 'latest' }} |
| 94 | + uses: actions/checkout@v3 |
| 95 | + - name: checkout tag |
| 96 | + if: ${{ github.event.inputs.version != '' && github.event.inputs.version != 'latest' }} |
| 97 | + uses: actions/checkout@v3 |
| 98 | + with: |
| 99 | + ref: refs/tags/${{ github.event.inputs.version }} |
| 100 | + |
| 101 | + - name: repository name fix and env |
| 102 | + run: | |
| 103 | + echo "image_repository_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $GITHUB_ENV |
| 104 | + echo "domain=${{ secrets.DEPLOY_HOST }}" >> $GITHUB_ENV |
| 105 | + echo "letsencrypt_email=${{ secrets.LETSENCRYPT_EMAIL }}" >> $GITHUB_ENV |
| 106 | + echo "TAG_NAME=latest" >> $GITHUB_ENV |
| 107 | + if [ "${{ github.event.release.tag_name }}" != "" ]; then |
| 108 | + echo "TAG_NAME=${{ github.event.release.tag_name }}" >> $GITHUB_ENV |
| 109 | + fi; |
| 110 | + if [ "${{ github.event.inputs.version }}" != "" ]; then |
| 111 | + echo "TAG_NAME=${{ github.event.inputs.version }}" >> $GITHUB_ENV |
| 112 | + fi; |
| 113 | +
|
| 114 | + # Populate docker-compose.yml with variables from build process, including TAG_NAME. |
| 115 | + - name: docker-compose file prep |
| 116 | + uses: danielr1996/[email protected] |
| 117 | + env: |
| 118 | + RELEASE_VERSION: ${{ env.TAG_NAME }} |
| 119 | + IMAGE_REPO: ${{ env.image_repository_name }} |
| 120 | + APP_NAME: ${{ github.event.repository.name }} |
| 121 | + HOST_DOMAIN: ${{ secrets.DEPLOY_HOST }} |
| 122 | + LETSENCRYPT_EMAIL: ${{ env.letsencrypt_email }} |
| 123 | + with: |
| 124 | + input: .deploy/docker-compose-template.yml |
| 125 | + output: .deploy/${{ github.event.repository.name }}-docker-compose.yml |
| 126 | + |
| 127 | + # Copy only the docker-compose.yml to remote server home folder |
| 128 | + - name: copy compose file via scp |
| 129 | + |
| 130 | + with: |
| 131 | + host: ${{ secrets.DEPLOY_HOST }} |
| 132 | + username: ${{ secrets.DEPLOY_USERNAME }} |
| 133 | + port: 22 |
| 134 | + key: ${{ secrets.DEPLOY_KEY }} |
| 135 | + source: ".deploy/${{ github.event.repository.name }}-docker-compose.yml" |
| 136 | + target: "~/" |
| 137 | + |
| 138 | + - name: Run remote db migrations |
| 139 | + |
| 140 | + env: |
| 141 | + APPTOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 142 | + USERNAME: ${{ secrets.DEPLOY_USERNAME }} |
| 143 | + with: |
| 144 | + host: ${{ secrets.DEPLOY_HOST }} |
| 145 | + username: ${{ secrets.DEPLOY_USERNAME }} |
| 146 | + key: ${{ secrets.DEPLOY_KEY }} |
| 147 | + port: 22 |
| 148 | + envs: APPTOKEN,USERNAME |
| 149 | + script: | |
| 150 | + echo $APPTOKEN | docker login ghcr.io -u $USERNAME --password-stdin |
| 151 | + docker compose -f ~/.deploy/${{ github.event.repository.name }}-docker-compose.yml pull |
| 152 | + docker compose -f ~/.deploy/${{ github.event.repository.name }}-docker-compose.yml up ${{ github.event.repository.name }}-migration --exit-code-from ${{ github.event.repository.name }}-migration |
| 153 | +
|
| 154 | + # Deploy Docker image with ServiceStack application using `docker compose up` remotely |
| 155 | + - name: remote docker-compose up via ssh |
| 156 | + |
| 157 | + env: |
| 158 | + APPTOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 159 | + USERNAME: ${{ secrets.DEPLOY_USERNAME }} |
| 160 | + with: |
| 161 | + host: ${{ secrets.DEPLOY_HOST }} |
| 162 | + username: ${{ secrets.DEPLOY_USERNAME }} |
| 163 | + key: ${{ secrets.DEPLOY_KEY }} |
| 164 | + port: 22 |
| 165 | + envs: APPTOKEN,USERNAME |
| 166 | + script: | |
| 167 | + echo $APPTOKEN | docker login ghcr.io -u $USERNAME --password-stdin |
| 168 | + docker-compose -f ~/.deploy/${{ github.event.repository.name }}-docker-compose.yml pull |
| 169 | + docker-compose -f ~/.deploy/${{ github.event.repository.name }}-docker-compose.yml up -d |
0 commit comments