Skip to content

Commit

Permalink
ci: PLT-286: Add deployment pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitabelonogov committed Mar 25, 2024
1 parent 978ea4b commit d806555
Show file tree
Hide file tree
Showing 4 changed files with 515 additions and 0 deletions.
187 changes: 187 additions & 0 deletions .github/workflows/argocd-create-app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
name: "ArgoCD: Create Application"

on:
workflow_call:
inputs:
docker_image_version:
required: true
type: string
branch_name:
required: true
type: string
workflow_dispatch:
inputs:
docker_image_version:
description: 'Docker image version'
required: true
type: string
branch_name:
description: 'Branch name'
required: true
type: string

env:
INFRA_REPO: "HumanSignal/infra"
APP_BASE_NAME: "adala"
NAMESPACE: "prompt"
DOMAIN: "dev.heartex.com"
TEMPLATE_DIR: "vars/aws/dev.heartex.com/k8s/prompt/templates/adala"
APPS_DIR: "vars/aws/dev.heartex.com/k8s/prompt"
DOCKER_IMAGE_VALUES_YAML_KEY: ".app.deployment.image.tag"
REPLICA_COUNT_VALUES_YAML_KEY: ".app.deployment.replicaCount"

jobs:

deploy:
timeout-minutes: 15
runs-on: ubuntu-latest
steps:
- uses: hmarr/[email protected]

- name: "GitHub deployment: Start"
id: github-deployment-start
uses: bobheadxi/deployments@v1
with:
step: start
token: ${{ secrets.GIT_PAT }}
env: ${{ inputs.branch_name }}
ref: ${{ inputs.branch_name }}

- name: Get GitHub user details
id: get-github-user
uses: actions/github-script@v7
env:
ACTOR_USERNAME: ${{ github.event.sender.login }}
with:
github-token: ${{ secrets.GIT_PAT }}
script: |
const actor_username = process.env.ACTOR_USERNAME;
let user_name = 'robot-ci-heartex';
let user_email = '[email protected]';
try {
const {data: user} = await github.rest.users.getByUsername({
username: actor_username,
});
user_name = user.login;
user_email = user.email;
} catch (e) {
console.log(e)
}
core.setOutput('user_name', user_name);
core.setOutput('user_email', user_email);
- name: Configure git
shell: bash
run: |
set -xeuo pipefail
git config --global user.name '${{ steps.get-github-user.outputs.user_name }}'
git config --global user.email '${{ steps.get-github-user.outputs.user_email }}'
- name: Checkout
uses: actions/checkout@v4
with:
repository: ${{ env.INFRA_REPO }}
token: ${{ secrets.GIT_PAT }}
fetch-depth: 1

- name: Commit
id: commit
shell: bash
env:
DOCKER_IMAGE_VERSION: ${{ inputs.docker_image_version }}
BRANCH_NAME: ${{ inputs.branch_name }}
run: |
set -xeuo pipefail
pretty_branch_name="$(echo -n "${BRANCH_NAME#refs/heads/}" | sed 's#/#-#g' | sed 's#_#-#g'| sed 's#\.#-#g' | tr '[:upper:]' '[:lower:]')"
APP_NAME="${pretty_branch_name}-${APP_BASE_NAME}"
APP_DIR="${APPS_DIR}/${APP_NAME}"
APP_VALUES_FILE="${APP_DIR}/values.yaml"
export DOCKER_IMAGE_TAG="${DOCKER_IMAGE_VERSION}"
echo "DOCKER_IMAGE_TAG=${DOCKER_IMAGE_TAG}" >> $GITHUB_OUTPUT
export HOSTNAME="${pretty_branch_name}-${APP_BASE_NAME}.${NAMESPACE}.${DOMAIN}"
echo "HOSTNAME=${HOSTNAME}" >> $GITHUB_OUTPUT
export RELEASE_NAME="${pretty_branch_name}"
echo "RELEASE_NAME=${RELEASE_NAME}" >> $GITHUB_OUTPUT
if [ -f "${APP_VALUES_FILE}" ]; then
yq e --inplace "${DOCKER_IMAGE_VALUES_YAML_KEY} |= \"${DOCKER_IMAGE_TAG}\"" "${APP_VALUES_FILE}"
else
mkdir -p "${APP_DIR}"
for template_file_name in $(ls "${TEMPLATE_DIR}"); do
file_name="${template_file_name%.template}"
envsubst < "${TEMPLATE_DIR}/${template_file_name}" > "${APP_DIR}/${file_name}"
done
fi
yq e --inplace "${REPLICA_COUNT_VALUES_YAML_KEY} |= 1" "${APP_VALUES_FILE}"
git add "${APP_DIR}"
git status
git commit -m "${DOMAIN}: Create ${APP_NAME}@${NAMESPACE}" -m 'Workflow run: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}'
- name: Push
id: push
shell: bash
run: |
set -xeuo pipefail
attempt=0
max_attempts=5
while ! git push; do
attempt=$((attempt+1))
if [ $attempt -ge $max_attempts ]; then
echo "Max attempts reached. Exiting."
exit 1
fi
sleep_time=$((RANDOM % 10 + 1))
echo "Push failed. Attempting retry (${attempt}/${max_attempts}) in ${sleep_time} seconds..."
sleep $sleep_time
git pull --rebase
done
echo "commit_sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
- name: Comment Pull Request
uses: actions/github-script@v7
env:
PR_NUMBER: ${{ github.event.number }}
COMMIT_SHA: ${{ steps.push.outputs.commit_sha }}
with:
github-token: ${{ secrets.GIT_PAT }}
script: |
const { repo, owner } = context.repo;
const [infraOwner, infraRepo] = process.env.INFRA_REPO.split('/');
const prNumber = process.env.PR_NUMBER;
const commit_sha = process.env.COMMIT_SHA;
if (prNumber && commit_sha) {
const { data: commit } = await github.rest.git.getCommit({
owner: infraOwner,
repo: infraRepo,
commit_sha: commit_sha,
});
await github.rest.issues.createComment({
owner: owner,
repo: repo,
issue_number: prNumber,
body: `Commit ${commit.html_url} is created.`,
});
}
- name: "GitHub deployment: Finish"
id: github-deployment-finish
if: always()
uses: bobheadxi/deployments@v1
with:
step: finish
token: ${{ secrets.GIT_PAT }}
status: ${{ job.status }}
deployment_id: ${{ steps.github-deployment-start.outputs.deployment_id }}
env: ${{ steps.github-deployment-start.outputs.env }}
env_url: "https://${{ steps.commit.outputs.HOSTNAME }}"
151 changes: 151 additions & 0 deletions .github/workflows/argocd-delete-app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
name: "ArgoCD: Delete Application"

on:
workflow_call:
inputs:
branch_name:
required: true
type: string
workflow_dispatch:
inputs:
branch_name:
description: 'Branch name'
required: true
type: string
pull_request_target:
types:
- closed
branches:
- master
- '**'


env:
INFRA_REPO: "HumanSignal/infra"
APP_BASE_NAME: "adala"
NAMESPACE: "prompt"
DOMAIN: "dev.heartex.com"
TEMPLATE_DIR: "vars/aws/dev.heartex.com/k8s/prompt/templates/adala"
APPS_DIR: "vars/aws/dev.heartex.com/k8s/prompt"

jobs:

destroy:
timeout-minutes: 15
runs-on: ubuntu-latest
steps:
- uses: hmarr/[email protected]

- name: Get GitHub user details
id: get-github-user
uses: actions/github-script@v7
env:
ACTOR_USERNAME: ${{ github.event.sender.login }}
with:
github-token: ${{ secrets.GIT_PAT }}
script: |
const actor_username = process.env.ACTOR_USERNAME;
let user_name = 'robot-ci-heartex';
let user_email = '[email protected]';
try {
const {data: user} = await github.rest.users.getByUsername({
username: actor_username,
});
user_name = user.login;
user_email = user.email;
} catch (e) {
console.log(e)
}
core.setOutput('user_name', user_name);
core.setOutput('user_email', user_email);
- name: Configure git
shell: bash
run: |
set -xeuo pipefail
git config --global user.name '${{ steps.get-github-user.outputs.user_name }}'
git config --global user.email '${{ steps.get-github-user.outputs.user_email }}'
- name: Checkout
uses: actions/checkout@v4
with:
repository: ${{ env.INFRA_REPO }}
token: ${{ secrets.GIT_PAT }}
fetch-depth: 1

- name: Commit
shell: bash
env:
DOCKER_IMAGE_VERSION: ${{ inputs.docker_image_version }}
BRANCH_NAME: ${{ inputs.branch_name || github.event.pull_request.head.ref || github.ref_name }}
run: |
set -xeuo pipefail
pretty_branch_name="$(echo -n "${BRANCH_NAME#refs/heads/}" | sed 's#/#-#g' | sed 's#_#-#g'| sed 's#\.#-#g' | tr '[:upper:]' '[:lower:]')"
APP_NAME="${pretty_branch_name}-${APP_BASE_NAME}"
APP_DIR="${APPS_DIR}/${APP_NAME}"
git rm -r "${APP_DIR}"
git status
git commit -m "${DOMAIN}: Delete ${APP_NAME}@${NAMESPACE}" -m 'Workflow run: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}'
- name: Push
id: push
shell: bash
run: |
set -xeuo pipefail
attempt=0
max_attempts=5
while ! git push; do
attempt=$((attempt+1))
if [ $attempt -ge $max_attempts ]; then
echo "Max attempts reached. Exiting."
exit 1
fi
sleep_time=$((RANDOM % 10 + 1))
echo "Push failed. Attempting retry (${attempt}/${max_attempts}) in ${sleep_time} seconds..."
sleep $sleep_time
git pull --rebase
done
echo "commit_sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT
- name: Comment Pull Request
uses: actions/github-script@v7
env:
PR_NUMBER: ${{ github.event.number }}
COMMIT_SHA: ${{ steps.push.outputs.commit_sha }}
with:
github-token: ${{ secrets.GIT_PAT }}
script: |
const { repo, owner } = context.repo;
const [infraOwner, infraRepo] = process.env.INFRA_REPO.split('/');
const prNumber = process.env.PR_NUMBER;
const commit_sha = process.env.COMMIT_SHA;
if (prNumber && commit_sha) {
const { data: commit } = await github.rest.git.getCommit({
owner: infraOwner,
repo: infraRepo,
commit_sha: commit_sha,
});
await github.rest.issues.createComment({
owner: owner,
repo: repo,
issue_number: prNumber,
body: `Commit ${commit.html_url} is created.`,
});
}
- name: "GitHub deployment: Delete"
id: github-deployment-delete
uses: bobheadxi/deployments@v1
with:
step: delete-env
token: ${{ secrets.GIT_PAT }}
env: ${{ inputs.branch_name || github.event.pull_request.head.ref || github.ref_name }}
Loading

0 comments on commit d806555

Please sign in to comment.