|
| 1 | +# WF for deploying to test/stage envs on PR label 'deploy/test' or 'deploy/stage' |
| 2 | + |
| 3 | +name: Deploy on label |
| 4 | + |
| 5 | +on: |
| 6 | + pull_request_target: |
| 7 | + types: |
| 8 | + - labeled |
| 9 | + |
| 10 | +# Cancel in-progress jobs for the same tag/branch. |
| 11 | +concurrency: |
| 12 | + group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event.label.name }}-deploy |
| 13 | + cancel-in-progress: true |
| 14 | + |
| 15 | +permissions: |
| 16 | + contents: read |
| 17 | + id-token: write |
| 18 | + issues: write |
| 19 | + pull-requests: write |
| 20 | + |
| 21 | +jobs: |
| 22 | + deploy: |
| 23 | + runs-on: ubuntu-latest |
| 24 | + if: ${{ github.event.label.name == 'deploy/test' || github.event.label.name == 'deploy/stage' }} |
| 25 | + steps: |
| 26 | + - name: Checkout code |
| 27 | + uses: actions/checkout@v4 |
| 28 | + with: |
| 29 | + ref: ${{ github.event.pull_request.head.sha }} |
| 30 | + submodules: recursive |
| 31 | + fetch-depth: 0 |
| 32 | + |
| 33 | + - name: Extract environment |
| 34 | + id: env |
| 35 | + run: | |
| 36 | + LABEL="${{ github.event.label.name }}" |
| 37 | + ENV="${LABEL#deploy/}" |
| 38 | + echo "env=$ENV" >> $GITHUB_OUTPUT |
| 39 | +
|
| 40 | + - name: Create deployment comment |
| 41 | + id: comment |
| 42 | + uses: actions/github-script@v7 |
| 43 | + with: |
| 44 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 45 | + script: | |
| 46 | + const env = '${{ steps.env.outputs.env }}'; |
| 47 | + const comment = await github.rest.issues.createComment({ |
| 48 | + owner: context.repo.owner, |
| 49 | + repo: context.repo.repo, |
| 50 | + issue_number: context.payload.pull_request.number, |
| 51 | + body: `## 🚀 Deployment Started\n\n**Environment:** \`${env}\`\n\n_This comment will be updated with deployment status._` |
| 52 | + }); |
| 53 | + core.setOutput('comment_id', comment.data.id); |
| 54 | + console.log(`Created comment: ${comment.data.id}`); |
| 55 | +
|
| 56 | + - name: Validate that Environment variable set |
| 57 | + run: | |
| 58 | + if [ -z "${{ steps.env.outputs.env }}" ]; then |
| 59 | + echo "::error::Can't determine environment name." |
| 60 | + exit 1 |
| 61 | + fi |
| 62 | + echo "Environment: ${{ steps.env.outputs.env }}" |
| 63 | +
|
| 64 | + - name: Import secrets |
| 65 | + id: secrets |
| 66 | + uses: hashicorp/vault-action@v2 |
| 67 | + with: |
| 68 | + url: https://seguro.flant.com |
| 69 | + path: github |
| 70 | + role: deckhouse-web-products |
| 71 | + method: jwt |
| 72 | + jwtGithubAudience: github-access-aud |
| 73 | + secrets: | |
| 74 | + projects/data/6db2f1ee-9b6f-4f4f-8381-2fb43060478a/github/registry_host DECKHOUSE_DEV_REGISTRY_HOST | DECKHOUSE_DEV_REGISTRY_HOST ; |
| 75 | + projects/data/6db2f1ee-9b6f-4f4f-8381-2fb43060478a/github/registry_host DECKHOUSE_REGISTRY_READ_HOST | DECKHOUSE_REGISTRY_READ_HOST ; |
| 76 | + projects/data/6db2f1ee-9b6f-4f4f-8381-2fb43060478a/github/registry_read_token login | DECKHOUSE_REGISTRY_READ_USER ; |
| 77 | + projects/data/6db2f1ee-9b6f-4f4f-8381-2fb43060478a/github/registry_read_token password | DECKHOUSE_REGISTRY_READ_PASSWORD ; |
| 78 | + projects/data/6db2f1ee-9b6f-4f4f-8381-2fb43060478a/github/documentation_deploy_secret KUBECONFIG_BASE64_DEV | KUBECONFIG_BASE64_DEV ; |
| 79 | + projects/data/101ceaca-97cd-462f-aed5-070d9b9de175/dev-registry/writetoken login | DECKHOUSE_DEV_REGISTRY_USER ; |
| 80 | + projects/data/101ceaca-97cd-462f-aed5-070d9b9de175/dev-registry/writetoken password | DECKHOUSE_DEV_REGISTRY_PASSWORD ; |
| 81 | +
|
| 82 | + - name: Check dev registry credentials |
| 83 | + id: check_dev_registry |
| 84 | + env: |
| 85 | + HOST: ${{steps.secrets.outputs.DECKHOUSE_DEV_REGISTRY_HOST}} |
| 86 | + run: | |
| 87 | + if [[ -n $HOST ]]; then |
| 88 | + echo "has_credentials=true" >> $GITHUB_OUTPUT |
| 89 | + echo "web_registry_path=${{steps.secrets.outputs.DECKHOUSE_DEV_REGISTRY_HOST }}/deckhouse/site" >> $GITHUB_OUTPUT |
| 90 | + fi |
| 91 | +
|
| 92 | + - name: Login to dev registry |
| 93 | + uses: docker/login-action@v3 |
| 94 | + if: ${{ steps.check_dev_registry.outputs.has_credentials == 'true' }} |
| 95 | + with: |
| 96 | + registry: ${{ steps.secrets.outputs.DECKHOUSE_DEV_REGISTRY_HOST }} |
| 97 | + username: ${{ steps.secrets.outputs.DECKHOUSE_DEV_REGISTRY_USER }} |
| 98 | + password: ${{ steps.secrets.outputs.DECKHOUSE_DEV_REGISTRY_PASSWORD }} |
| 99 | + logout: false |
| 100 | + |
| 101 | + - name: Check readonly registry credentials |
| 102 | + id: check_readonly_registry |
| 103 | + env: |
| 104 | + HOST: ${{ steps.secrets.outputs.DECKHOUSE_REGISTRY_READ_HOST }} |
| 105 | + run: | |
| 106 | + if [[ -n $HOST ]]; then |
| 107 | + echo "has_credentials=true" >> $GITHUB_OUTPUT |
| 108 | + echo "web_registry_path=${{ steps.secrets.outputs.DECKHOUSE_REGISTRY_READ_HOST }}/deckhouse/site" >> $GITHUB_OUTPUT |
| 109 | + fi |
| 110 | +
|
| 111 | + - name: Login to readonly registry |
| 112 | + uses: docker/login-action@v3 |
| 113 | + if: ${{ steps.check_readonly_registry.outputs.has_credentials == 'true' }} |
| 114 | + with: |
| 115 | + registry: ${{ steps.secrets.outputs.DECKHOUSE_REGISTRY_READ_HOST }} |
| 116 | + username: ${{ steps.secrets.outputs.DECKHOUSE_REGISTRY_READ_USER }} |
| 117 | + password: ${{ steps.secrets.outputs.DECKHOUSE_REGISTRY_READ_PASSWORD }} |
| 118 | + logout: false |
| 119 | + |
| 120 | + - name: Install werf |
| 121 | + uses: werf/actions/install@v2 |
| 122 | + |
| 123 | + - name: Deploy to ${{ steps.env.outputs.env }} |
| 124 | + id: deploy |
| 125 | + env: |
| 126 | + WERF_REPO: ${{ steps.check_dev_registry.outputs.web_registry_path }} |
| 127 | + WERF_ENV: ${{ steps.env.outputs.env }} |
| 128 | + WERF_KUBE_CONFIG_BASE64: ${{ steps.secrets.outputs.KUBECONFIG_BASE64_DEV }} |
| 129 | + WERF_SET_URL: "global.url=deckhouse.${{ steps.env.outputs.env }}.flant.com" |
| 130 | + WERF_SET_URL_RU: "global.url_ru=deckhouse.ru.${{ steps.env.outputs.env }}.flant.com" |
| 131 | + |
| 132 | + run: | |
| 133 | + werf converge |
| 134 | +
|
| 135 | + - name: Update comment - deployment succeeded |
| 136 | + if: success() |
| 137 | + uses: actions/github-script@v7 |
| 138 | + with: |
| 139 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 140 | + script: | |
| 141 | + const env = '${{ steps.env.outputs.env }}'; |
| 142 | + const commentId = ${{ steps.comment.outputs.comment_id }}; |
| 143 | + const repoName = context.repo.repo; |
| 144 | + const appName = repoName.startsWith('website-') ? repoName.replace(/^website-/, '') : repoName; |
| 145 | + const urlEn = `https://deckhouse.${env}.flant.com/products/${appName}/documentation/`; |
| 146 | + const urlRu = `https://deckhouse.ru.${env}.flant.com/products/${appName}/documentation/`; |
| 147 | + |
| 148 | + await github.rest.issues.updateComment({ |
| 149 | + owner: context.repo.owner, |
| 150 | + repo: context.repo.repo, |
| 151 | + comment_id: commentId, |
| 152 | + body: `## ✅ Deployment Succeeded\n\n**Environment:** \`${env}\`\n\n**Access URLs:**\n\n- 🇬🇧 [English](${urlEn})\n- 🇷🇺 [Russian](${urlRu})` |
| 153 | + }); |
| 154 | + console.log(`Updated comment ${commentId} with success status`); |
| 155 | +
|
| 156 | + - name: Update comment - deployment failed |
| 157 | + if: failure() |
| 158 | + uses: actions/github-script@v7 |
| 159 | + with: |
| 160 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 161 | + script: | |
| 162 | + const env = '${{ steps.env.outputs.env }}'; |
| 163 | + const commentId = ${{ steps.comment.outputs.comment_id }}; |
| 164 | + |
| 165 | + // Get the job ID for the failed job |
| 166 | + const jobs = await github.rest.actions.listJobsForWorkflowRun({ |
| 167 | + owner: context.repo.owner, |
| 168 | + repo: context.repo.repo, |
| 169 | + run_id: context.runId |
| 170 | + }); |
| 171 | + |
| 172 | + const job = jobs.data.jobs.find(j => j.name === 'deploy'); |
| 173 | + const jobId = job ? job.id : null; |
| 174 | + |
| 175 | + let logsUrl; |
| 176 | + if (jobId) { |
| 177 | + logsUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}/job/${jobId}`; |
| 178 | + } else { |
| 179 | + logsUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`; |
| 180 | + } |
| 181 | + |
| 182 | + await github.rest.issues.updateComment({ |
| 183 | + owner: context.repo.owner, |
| 184 | + repo: context.repo.repo, |
| 185 | + comment_id: commentId, |
| 186 | + body: `## ❌ Deployment Failed\n\n**Environment:** \`${env}\`\n\nPlease check the [workflow logs](${logsUrl}) for details.` |
| 187 | + }); |
| 188 | + console.log(`Updated comment ${commentId} with failure status`); |
| 189 | +
|
| 190 | + - name: Remove deploy label |
| 191 | + if: always() |
| 192 | + uses: actions/github-script@v7 |
| 193 | + with: |
| 194 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 195 | + script: | |
| 196 | + const label = context.payload.label.name; |
| 197 | + if (label && (label === 'deploy/test' || label === 'deploy/stage')) { |
| 198 | + await github.rest.issues.removeLabel({ |
| 199 | + owner: context.repo.owner, |
| 200 | + repo: context.repo.repo, |
| 201 | + issue_number: context.payload.pull_request.number, |
| 202 | + name: label |
| 203 | + }); |
| 204 | + console.log(`Removed label: ${label}`); |
| 205 | + } |
| 206 | +
|
0 commit comments