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