|
| 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 | + if [ ! -z "${{ secrets.APPSETTINGS_PATCH }}" ]; then |
| 52 | + echo "HAS_APPSETTINGS_PATCH=true" >> $GITHUB_ENV |
| 53 | + else |
| 54 | + echo "HAS_APPSETTINGS_PATCH=false" >> $GITHUB_ENV |
| 55 | + fi; |
| 56 | + |
| 57 | + - name: Login to GitHub Container Registry |
| 58 | + uses: docker/login-action@v2 |
| 59 | + with: |
| 60 | + registry: ghcr.io |
| 61 | + username: ${{ github.actor }} |
| 62 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 63 | + |
| 64 | + - name: setup .net core |
| 65 | + uses: actions/setup-dotnet@v3 |
| 66 | + with: |
| 67 | + dotnet-version: '8.0' |
| 68 | + |
| 69 | + - name: Install x tool |
| 70 | + if: env.HAS_APPSETTINGS_PATCH == 'true' |
| 71 | + run: dotnet tool install -g x |
| 72 | + |
| 73 | + - name: Apply Production AppSettings |
| 74 | + if: env.HAS_APPSETTINGS_PATCH == 'true' |
| 75 | + working-directory: ./MyApp |
| 76 | + run: | |
| 77 | + cat <<EOF >> appsettings.json.patch |
| 78 | + ${{ secrets.APPSETTINGS_PATCH }} |
| 79 | + EOF |
| 80 | + x patch appsettings.json.patch |
| 81 | + cat appsettings.json |
| 82 | + |
| 83 | + # Build and push new docker image, skip for manual redeploy other than 'latest' |
| 84 | + - name: Build and push Docker image |
| 85 | + run: | |
| 86 | + dotnet publish --os linux --arch x64 -c Release -p:ContainerRepository=${{ env.image_repository_name }} -p:ContainerRegistry=ghcr.io -p:ContainerImageTags=${{ env.TAG_NAME }} -p:ContainerPort=80 |
| 87 | +
|
| 88 | + deploy_via_ssh: |
| 89 | + needs: push_to_registry |
| 90 | + runs-on: ubuntu-22.04 |
| 91 | + if: ${{ github.event.workflow_run.conclusion != 'failure' }} |
| 92 | + steps: |
| 93 | + # Checkout latest or specific tag |
| 94 | + - name: checkout |
| 95 | + if: ${{ github.event.inputs.version == '' || github.event.inputs.version == 'latest' }} |
| 96 | + uses: actions/checkout@v3 |
| 97 | + - name: checkout tag |
| 98 | + if: ${{ github.event.inputs.version != '' && github.event.inputs.version != 'latest' }} |
| 99 | + uses: actions/checkout@v3 |
| 100 | + with: |
| 101 | + ref: refs/tags/${{ github.event.inputs.version }} |
| 102 | + |
| 103 | + - name: repository name fix and env |
| 104 | + run: | |
| 105 | + echo "image_repository_name=$(echo ${{ github.repository }} | tr '[:upper:]' '[:lower:]')" >> $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 | + - name: Create .env file |
| 115 | + run: | |
| 116 | + echo "Generating .env file" |
| 117 | + |
| 118 | + echo "# Autogenerated .env file" > .deploy/.env |
| 119 | + echo "HOST_DOMAIN=${{ secrets.DEPLOY_HOST }}" >> .deploy/.env |
| 120 | + echo "LETSENCRYPT_EMAIL=${{ secrets.LETSENCRYPT_EMAIL }}" >> .deploy/.env |
| 121 | + echo "APP_NAME=${{ github.event.repository.name }}" >> .deploy/.env |
| 122 | + echo "IMAGE_REPO=${{ env.image_repository_name }}" >> .deploy/.env |
| 123 | + echo "RELEASE_VERSION=${{ env.TAG_NAME }}" >> .deploy/.env |
| 124 | + |
| 125 | + # Copy only the docker-compose.yml to remote server home folder |
| 126 | + - name: copy files to target server via scp |
| 127 | + |
| 128 | + with: |
| 129 | + host: ${{ secrets.DEPLOY_HOST }} |
| 130 | + username: ${{ secrets.DEPLOY_USERNAME }} |
| 131 | + port: 22 |
| 132 | + key: ${{ secrets.DEPLOY_KEY }} |
| 133 | + strip_components: 2 |
| 134 | + source: "./.deploy/docker-compose.yml,./.deploy/.env" |
| 135 | + target: "~/.deploy/${{ github.event.repository.name }}/" |
| 136 | + |
| 137 | + - name: Run remote db migrations |
| 138 | + |
| 139 | + env: |
| 140 | + APPTOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 141 | + USERNAME: ${{ secrets.DEPLOY_USERNAME }} |
| 142 | + with: |
| 143 | + host: ${{ secrets.DEPLOY_HOST }} |
| 144 | + username: ${{ secrets.DEPLOY_USERNAME }} |
| 145 | + key: ${{ secrets.DEPLOY_KEY }} |
| 146 | + port: 22 |
| 147 | + envs: APPTOKEN,USERNAME |
| 148 | + script: | |
| 149 | + set -e |
| 150 | + echo $APPTOKEN | docker login ghcr.io -u $USERNAME --password-stdin |
| 151 | + cd ~/.deploy/${{ github.event.repository.name }} |
| 152 | + docker compose pull |
| 153 | + export APP_ID=$(docker compose run --entrypoint "id -u" --rm app) |
| 154 | + docker compose run --entrypoint "chown $APP_ID:$APP_ID /app/App_Data" --user root --rm app |
| 155 | + docker compose up app-migration --exit-code-from app-migration |
| 156 | +
|
| 157 | + # Deploy Docker image with your application using `docker compose up` remotely |
| 158 | + - name: remote docker-compose up via ssh |
| 159 | + |
| 160 | + env: |
| 161 | + APPTOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 162 | + USERNAME: ${{ secrets.DEPLOY_USERNAME }} |
| 163 | + with: |
| 164 | + host: ${{ secrets.DEPLOY_HOST }} |
| 165 | + username: ${{ secrets.DEPLOY_USERNAME }} |
| 166 | + key: ${{ secrets.DEPLOY_KEY }} |
| 167 | + port: 22 |
| 168 | + envs: APPTOKEN,USERNAME |
| 169 | + script: | |
| 170 | + echo $APPTOKEN | docker login ghcr.io -u $USERNAME --password-stdin |
| 171 | + cd ~/.deploy/${{ github.event.repository.name }} |
| 172 | + docker compose pull |
| 173 | + docker compose up app -d |
0 commit comments