diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..3c0ee4e --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,146 @@ +name: Deploy Infrastructure + +# Deploy after CI succeeds on main, or allow manual execution. +on: + workflow_run: + workflows: [CI] + types: [completed] + branches: [main] + workflow_dispatch: + +# Prevent overlapping infrastructure deployments. +# Do not cancel an active deployment, since OpenTofu may be modifying resources. +concurrency: + group: infrastructure-deployment + cancel-in-progress: false + +permissions: + contents: read + +jobs: + deploy-test: + name: Deploy TEST + # Only deploy after successful CI on a push to main, + # or when manually triggered from main. + if: >- + (github.event_name == 'workflow_run' && + github.event.workflow_run.conclusion == 'success' && + github.event.workflow_run.event == 'push' && + github.event.workflow_run.head_branch == 'main') || + (github.event_name == 'workflow_dispatch' && + github.ref == 'refs/heads/main') + + runs-on: [self-hosted, linux, assignment2] + timeout-minutes: 45 + environment: TEST + + # Authenticate to SkyHiGh using environment-specific credentials. + env: + OS_AUTH_TYPE: v3applicationcredential + OS_AUTH_URL: ${{ vars.OS_AUTH_URL }} + OS_REGION_NAME: ${{ vars.OS_REGION_NAME }} + OS_IDENTITY_API_VERSION: "3" + OS_INTERFACE: public + OS_APPLICATION_CREDENTIAL_ID: ${{ secrets.OS_APPLICATION_CREDENTIAL_ID }} + OS_APPLICATION_CREDENTIAL_SECRET: ${{ secrets.OS_APPLICATION_CREDENTIAL_SECRET }} + + KOPS_STATE_STORE: swift://demo-kops + + steps: + # Checkout the exact commit validated by CI. + - name: Checkout + uses: actions/checkout@v5 + with: + ref: ${{ github.event_name == 'workflow_run' && github.event.workflow_run.head_sha || github.sha }} + persist-credentials: false + + # Use the same OpenTofu version as the CI workflow. + - name: Setup OpenTofu + uses: opentofu/setup-opentofu@v2 + with: + tofu_version: "1.12.6" + tofu_wrapper: false + + # Verify OpenTofu and TEST stack configurations are available. + # Actual deployment steps will be added after credentials are configured. + - name: Preflight checks + shell: bash + run: | + tofu version + test -f environments/test/network/providers.tf + test -f environments/test/frontend/providers.tf + + : "${OS_AUTH_URL:?Missing OS_AUTH_URL}" + : "${OS_REGION_NAME:?Missing OS_REGION_NAME}" + : "${OS_APPLICATION_CREDENTIAL_ID:?Missing application credential ID}" + : "${OS_APPLICATION_CREDENTIAL_SECRET:?Missing application credential secret}" + + echo "TEST deployment prerequisites are configured." + + # Install tools into the job's temporary directory. + - name: Install kOps and kubectl + shell: bash + run: | + set -euo pipefail + + BIN_DIR="$RUNNER_TEMP/tofu-tools" + mkdir -p "$BIN_DIR" + + # Kubernetes API server + KUBECTL_VERSION="v1.36.4" + + curl -fsSL -o "$BIN_DIR/kubectl" \ + "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl" + + curl -fsSL -o "$BIN_DIR/kubectl.sha256" \ + "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl.sha256" + + ( + cd "$BIN_DIR" + echo "$(cat kubectl.sha256) kubectl" | sha256sum --check + ) + + # Download kOps and verify its checksum. + KOPS_VERSION="v1.36.2" + + curl -fsSL -o "$BIN_DIR/kops" \ + "https://github.com/kubernetes/kops/releases/download/${KOPS_VERSION}/kops-linux-amd64" + + curl -fsSL -o "$BIN_DIR/kops.sha256" \ + "https://github.com/kubernetes/kops/releases/download/${KOPS_VERSION}/kops-linux-amd64.sha256" + + ( + cd "$BIN_DIR" + echo "$(cat kops.sha256) kops" | sha256sum --check + ) + + chmod +x "$BIN_DIR/kubectl" "$BIN_DIR/kops" + + # Make both tools available to subsequent steps. + echo "$BIN_DIR" >> "$GITHUB_PATH" + + - name: Verify installed tools + shell: bash + run: | + kops version + kubectl version --client + + - name: Verify kOps state store access + shell: bash + env: + # Override TEST deployment credentials for this step only. + OS_APPLICATION_CREDENTIAL_ID: ${{ secrets.KOPS_APPLICATION_CREDENTIAL_ID }} + OS_APPLICATION_CREDENTIAL_SECRET: ${{ secrets.KOPS_APPLICATION_CREDENTIAL_SECRET }} + run: | + set -euo pipefail + + : "${OS_APPLICATION_CREDENTIAL_ID:?Missing kOps credential ID}" + : "${OS_APPLICATION_CREDENTIAL_SECRET:?Missing kOps credential secret}" + : "${KOPS_STATE_STORE:?Missing kOps state store}" + + kops get clusters --state="$KOPS_STATE_STORE" + + kops get cluster einalien.k8s.local \ + --state="$KOPS_STATE_STORE" > /dev/null + + echo "kOps state store access verified." \ No newline at end of file diff --git a/bootstrap/cloud_init/runner.yaml b/bootstrap/cloud_init/runner.yaml index fe47620..68f0f56 100644 --- a/bootstrap/cloud_init/runner.yaml +++ b/bootstrap/cloud_init/runner.yaml @@ -9,4 +9,6 @@ packages: - jq - tar - unzip - - libicu76 \ No newline at end of file + - libicu76 + - python3 + - python3-venv \ No newline at end of file