|
| 1 | +name: Create Hetzner Server |
| 2 | +run-name: Create server for ${{ inputs.environment }} environment |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + environment: |
| 7 | + description: "Short server name (3–5 letters)" |
| 8 | + required: true |
| 9 | + type: |
| 10 | + description: "Environment type (single or multi node)" |
| 11 | + required: false |
| 12 | + type: choice |
| 13 | + default: 'single-node' |
| 14 | + options: |
| 15 | + - single-node |
| 16 | + - multi-node |
| 17 | + backup_enabled: |
| 18 | + type: boolean |
| 19 | + description: Backup enabled |
| 20 | + default: false |
| 21 | + required: false |
| 22 | + workflow_call: |
| 23 | + inputs: |
| 24 | + environment: |
| 25 | + type: string |
| 26 | + description: Environment to deploy to |
| 27 | + required: true |
| 28 | + type: |
| 29 | + type: string |
| 30 | + description: Select group tag you want to execute |
| 31 | + default: 'single-node' |
| 32 | + backup_enabled: |
| 33 | + type: boolean |
| 34 | + description: Backup enabled |
| 35 | + default: false |
| 36 | + |
| 37 | +env: |
| 38 | + GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} |
| 39 | + TF_PATH: infrastructure/provision-server/hetzner-cloud-empty-server |
| 40 | + TF_VAR_hcloud_token: ${{ secrets.HCLOUD_TOKEN }} |
| 41 | + TF_VAR_country_name: ${{ vars.COUNTRY_NAME }} |
| 42 | + TF_VAR_env_name: ${{ inputs.environment }} |
| 43 | + TF_VAR_env_type: ${{ inputs.type }} |
| 44 | + TF_VAR_cloudflare_api_token: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
| 45 | + TF_VAR_cloudflare_zone_id: ${{ secrets.CLOUDFLARE_ZONE_ID }} |
| 46 | + TERRAFORM_REPO: opencrvs/terraform-state |
| 47 | + type: ${{ inputs.type }} |
| 48 | +jobs: |
| 49 | + create-environment: |
| 50 | + name: Create New HCloud Environment |
| 51 | + runs-on: ubuntu-24.04 |
| 52 | + environment: ${{ inputs.environment }} |
| 53 | + steps: |
| 54 | + - name: Checkout repo ${{ github.repository }} |
| 55 | + uses: actions/checkout@v4 |
| 56 | + with: |
| 57 | + # Token permissions: read:org, read:public_key, repo, workflow |
| 58 | + token: ${{ secrets.GH_TOKEN }} |
| 59 | + fetch-depth: 0 |
| 60 | + ref: ${{ github.ref_name }} |
| 61 | + - name: Configure git client for ${{ github.repository }} |
| 62 | + run: | |
| 63 | + git config user.name "github-actions[bot]" |
| 64 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 65 | + - name: Checkout repo ${{ env.TERRAFORM_REPO }} |
| 66 | + uses: actions/checkout@v4 |
| 67 | + with: |
| 68 | + repository: ${{ env.TERRAFORM_REPO }} |
| 69 | + ref: main |
| 70 | + token: ${{ secrets.GH_TOKEN }} |
| 71 | + path: terraform-state |
| 72 | + - name: Configure git client for ${{ env.TERRAFORM_REPO }} |
| 73 | + working-directory: terraform-state/ |
| 74 | + run: | |
| 75 | + git config user.name "github-actions[bot]" |
| 76 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 77 | + - name: Pull SSH key pair files from github |
| 78 | + run: | |
| 79 | + ssh_key_path=$TF_PATH/.ssh |
| 80 | + mkdir -p $ssh_key_path |
| 81 | + echo "${{ secrets.SSH_PRIVATE_KEY }}" > $ssh_key_path/id_rsa |
| 82 | + echo "${{ secrets.SSH_PUBLIC_KEY }}" > $ssh_key_path/id_rsa.pub |
| 83 | + chmod 600 $ssh_key_path/id_rsa |
| 84 | + chmod 644 $ssh_key_path/id_rsa.pub |
| 85 | +
|
| 86 | + - name: Restore terraform state |
| 87 | + run: | |
| 88 | + mkdir -p terraform-state/${{ vars.COUNTRY_NAME }} |
| 89 | + [ -f terraform-state/${{ vars.COUNTRY_NAME }}/${{ inputs.environment }}-${{ env.type }}.tfstate ] && \ |
| 90 | + cp terraform-state/${{ vars.COUNTRY_NAME }}/${{ inputs.environment }}-${{ env.type }}.tfstate ${{ env.TF_PATH }}/terraform.tfstate || \ |
| 91 | + echo "Terraform state file not found. Creating a new one." |
| 92 | +
|
| 93 | + - name: Setup Terraform |
| 94 | + uses: hashicorp/setup-terraform@v3 |
| 95 | + with: |
| 96 | + terraform_version: 1.5.7 |
| 97 | + |
| 98 | + - name: Terraform Init |
| 99 | + working-directory: ${{ env.TF_PATH }} |
| 100 | + run: terraform init |
| 101 | + |
| 102 | + - name: Terraform Apply |
| 103 | + working-directory: ${{ env.TF_PATH }} |
| 104 | + run: | |
| 105 | + terraform apply -auto-approve -input=false |
| 106 | +
|
| 107 | + - name: Store variables from terraform state file |
| 108 | + id: output |
| 109 | + working-directory: ${{ env.TF_PATH }} |
| 110 | + run: | |
| 111 | + echo "hostname=$TF_VAR_country_name-$TF_VAR_env_name" >> $GITHUB_OUTPUT |
| 112 | + echo "public_ip=$(terraform output -raw public_ip)" >> $GITHUB_OUTPUT |
| 113 | + echo "master_ip=$(terraform output -raw master_ip)" >> $GITHUB_OUTPUT |
| 114 | + if [ ${{ inputs.type }} == 'single-node' ] |
| 115 | + then |
| 116 | + echo "master_hostname=$TF_VAR_country_name-$TF_VAR_env_name" >> $GITHUB_OUTPUT |
| 117 | + else |
| 118 | + echo "master_hostname=$TF_VAR_country_name-$TF_VAR_env_name-master" >> $GITHUB_OUTPUT |
| 119 | + echo "worker_hostname=$TF_VAR_country_name-$TF_VAR_env_name-worker" >> $GITHUB_OUTPUT |
| 120 | + # echo "backup_hostname=$TF_VAR_country_name-$TF_VAR_env_name-backup" >> $GITHUB_OUTPUT |
| 121 | + echo "worker_ip=$(terraform output -raw worker_ip)" >> $GITHUB_OUTPUT |
| 122 | + # echo "backup_ip=$(terraform output -raw backup_ip)" >> $GITHUB_OUTPUT |
| 123 | + fi |
| 124 | +
|
| 125 | + - name: Update terraform state file in ${{ env.TERRAFORM_REPO }} |
| 126 | + run: | |
| 127 | + cp ${{ env.TF_PATH }}/terraform.tfstate terraform-state/${{ vars.COUNTRY_NAME }}/${{ inputs.environment }}-${{ env.type }}.tfstate |
| 128 | + cd terraform-state/ |
| 129 | + if [[ -n "$(git status --porcelain)" ]]; then |
| 130 | + git add ${{ vars.COUNTRY_NAME }}/${{ inputs.environment }}-${{ env.type }}.tfstate |
| 131 | + git commit -m "Add environment file for ${{ inputs.environment }} env with type ${{ env.type }}" |
| 132 | + git push |
| 133 | + else |
| 134 | + echo "No changes to commit" |
| 135 | + fi |
| 136 | +
|
| 137 | + - name: Create environment file for ansible |
| 138 | + env: |
| 139 | + ENV: ${{ inputs.environment }} |
| 140 | + MASTER_IP: ${{ steps.output.outputs.master_ip }} |
| 141 | + MASTER_HOSTNAME: ${{ steps.output.outputs.master_hostname }} |
| 142 | + WORKER_IP: ${{ steps.output.outputs.worker_ip }} |
| 143 | + WORKER_HOSTNAME: ${{ steps.output.outputs.worker_hostname }} |
| 144 | + # BACKUP_IP: ${{ steps.output.outputs.backup_ip }} |
| 145 | + # BACKUP_HOSTNAME: ${{ steps.output.outputs.backup_hostname }} |
| 146 | + run: | |
| 147 | + TARGET_ENV_BACKUP=$ENV |
| 148 | + SOURCE_ENV_BACKUP=${ENV/staging/prod} |
| 149 | + [ ${{ inputs.backup_enabled }} == 'true' ] && \ |
| 150 | + INVENTORY_TEMPLATE_FILE=infrastructure/provision-server/templates/${{ env.type }}-with-backup-ansible-env.yml || \ |
| 151 | + INVENTORY_TEMPLATE_FILE=infrastructure/provision-server/templates/${{ env.type }}-ansible-env.yml |
| 152 | + cat $INVENTORY_TEMPLATE_FILE | \ |
| 153 | + sed -e "s#SSH_HOST_MASTER#$MASTER_IP#" \ |
| 154 | + -e "s#HOSTNAME_MASTER#$MASTER_HOSTNAME#" \ |
| 155 | + -e "s#SSH_HOST_WORKER#$WORKER_IP#" \ |
| 156 | + -e "s#HOSTNAME_WORKER#$WORKER_HOSTNAME#" \ |
| 157 | + -e "s#TARGET_ENV_BACKUP#$TARGET_ENV_BACKUP#" \ |
| 158 | + -e "s#SOURCE_ENV_BACKUP#$SOURCE_ENV_BACKUP#" \ |
| 159 | + -e "s#ENV_BACKUP#$${{ inputs.environment }}#" \ |
| 160 | + > infrastructure/server-setup/inventory/${{ inputs.environment }}.yml && \ |
| 161 | + echo "Environment file created: infrastructure/server-setup/inventory/${{ inputs.environment }}.yml" |
| 162 | + - name: Create docker compose |
| 163 | + run: | |
| 164 | + [ ! -f infrastructure/docker-compose.${{ inputs.environment }}-deploy.yml ] && \ |
| 165 | + cp infrastructure/provision-server/templates/docker-compose.${{ env.type }}.yml infrastructure/docker-compose.${{ inputs.environment }}-deploy.yml && \ |
| 166 | + echo "Docker-compose created" || \ |
| 167 | + echo "Docker-compose already exists" |
| 168 | +
|
| 169 | + - name: Update workflows |
| 170 | + run: | |
| 171 | + workflows=( |
| 172 | + ".github/workflows/provision.yml" |
| 173 | + ".github/workflows/deploy.yml" |
| 174 | + ".github/workflows/seed-data.yml" |
| 175 | + ".github/workflows/clear-environment.yml" |
| 176 | + ) |
| 177 | + path=".on.workflow_dispatch.inputs.environment.options" |
| 178 | +
|
| 179 | + # Check if option already exists in first workflows file |
| 180 | + if ! yq e "$path" "$workflows" | grep -qc "${{ inputs.environment }}"; then |
| 181 | + echo "Adding new option '${{ inputs.environment }}' to workflows: ${workflows[@]}" |
| 182 | + for workflow in ${workflows[@]} |
| 183 | + do |
| 184 | + yq e "$path += [\"${{ inputs.environment }}\"]" -i "$workflow" |
| 185 | + echo "Updated workflow $workflow" |
| 186 | + done |
| 187 | + else |
| 188 | + echo "Option '${{ inputs.environment }}' already exists in workflows ${workflows[@]}" |
| 189 | + fi |
| 190 | +
|
| 191 | + - name: Create environment variables and secrets on GitHub |
| 192 | + env: |
| 193 | + MASTER_IP: ${{ steps.output.outputs.master_ip }} |
| 194 | + DOMAIN: ${{ inputs.environment }}.opencrvs.dev |
| 195 | + GH_TOKEN: ${{ secrets.GH_TOKEN }} |
| 196 | + ENVIRONMENT: ${{ inputs.environment }} |
| 197 | + run: | |
| 198 | + cat infrastructure/provision-server/templates/environment.variables.${{ env.type }}.tpl | \ |
| 199 | + sed -e "s/#SSH_HOST#/$MASTER_IP/" \ |
| 200 | + -e "s/#DOMAIN#/$DOMAIN/" \ |
| 201 | + > infrastructure/environment.variables |
| 202 | + echo "Environment variables file created: infrastructure/environment.variables" |
| 203 | + while read line; do |
| 204 | + if [[ $line == *"="* ]]; then |
| 205 | + key=$(echo "$line" | cut -d '=' -f 1) |
| 206 | + value=$(echo "$line" | cut -d '=' -f 2-) |
| 207 | + echo "Adding variable: $key" |
| 208 | + gh variable set --env "$ENVIRONMENT" $key --body "$value" |
| 209 | + fi |
| 210 | + done < infrastructure/environment.variables |
| 211 | + existing_secrets=$(gh secret list --env "$ENVIRONMENT" --json name -q '.[].name') |
| 212 | + while read line; do |
| 213 | + key=$(echo "$line" | cut -d '=' -f 1) |
| 214 | + if echo "$existing_secrets" | grep -qw "$key"; then |
| 215 | + echo "Secret $key already exists, skipping." |
| 216 | + continue; |
| 217 | + fi |
| 218 | +
|
| 219 | + if [[ $line == *"="* ]]; then |
| 220 | + value=$(echo "$line" | cut -d '=' -f 2-) |
| 221 | + echo "Adding secret with predefined value: $key" |
| 222 | + else |
| 223 | + value=`openssl rand -base64 25 | tr -cd '[:alnum:]._-' ; echo ''` |
| 224 | + echo "Adding secret with random value: $key" |
| 225 | + fi |
| 226 | + gh secret set "$key" --env "$ENVIRONMENT" --body "$value" |
| 227 | + done < infrastructure/provision-server/templates/environment.secrets.tpl |
| 228 | + gh secret set SSH_KEY --env ${{ inputs.environment }} < $TF_PATH/.ssh/id_rsa || echo "Failed" |
| 229 | + - name: Update known-hosts |
| 230 | + env: |
| 231 | + SSH_PORT: 22 |
| 232 | + MASTER_IP: ${{ steps.output.outputs.master_ip }} |
| 233 | + MASTER_HOSTNAME: ${{ steps.output.outputs.master_hostname }} |
| 234 | + WORKER_IP: ${{ steps.output.outputs.worker_ip }} |
| 235 | + WORKER_HOSTNAME: ${{ steps.output.outputs.worker_hostname }} |
| 236 | + # BACKUP_IP: ${{ steps.output.outputs.backup_ip }} |
| 237 | + # BACKUP_HOSTNAME: ${{ steps.output.outputs.backup_hostname }} |
| 238 | + run: | |
| 239 | + echo "Wait few seconds for server to be available" && sleep 10 |
| 240 | + bash ./infrastructure/environments/update-known-hosts.sh ${{ env.MASTER_IP }} ${{ env.SSH_PORT }} |
| 241 | + if [ ${{ inputs.type }} == 'multi-node' ] |
| 242 | + then |
| 243 | + bash ./infrastructure/environments/update-known-hosts.sh ${{ env.WORKER_IP }} ${{ env.SSH_PORT }} |
| 244 | + fi |
| 245 | +
|
| 246 | + - name: Commit and push changes to ${{ github.repository }} |
| 247 | + run: | |
| 248 | + git add infrastructure/server-setup/inventory/${{ inputs.environment }}.yml \ |
| 249 | + infrastructure/known-hosts \ |
| 250 | + .github \ |
| 251 | + infrastructure/docker-compose.${{ inputs.environment }}-deploy.yml |
| 252 | + git status |
| 253 | + git commit -m "Add environment files for ${{ inputs.environment }}" |
| 254 | + git push |
0 commit comments