Skip to content

Commit 804afb1

Browse files
committed
initial
0 parents  commit 804afb1

File tree

9 files changed

+365
-0
lines changed

9 files changed

+365
-0
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# github-actions
2+
3+
Use an action from this repo in your workflow like this:
4+
5+
```
6+
- name: Pull MindsDB Github Actions
7+
uses: actions/checkout@v4
8+
with:
9+
repository: mindsdb/github-actions
10+
path: github-actions
11+
ssh-key: ${{ secrets.GH_ACTIONS_PULL_SSH }}
12+
- uses: ./github-actions/<action-name>
13+
```
14+
15+
**NOTE: This needs to go AFTER any `actions/checkout` step for the current repo**

build-push-ecr/action.yml

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Builds a docker image, then tags it with the github sha and pushes it to our Amazon ECR registry
2+
3+
inputs:
4+
module-name:
5+
description: "Name of the module to build. Used as the default image name and src dir unless 'image-name' or 'src-path' are used."
6+
required: true
7+
build-for-environment:
8+
description: "The backend environment we are building for (API calls are pointed to). This should be one of (development, staging, production)."
9+
required: true
10+
extra-build-args:
11+
description: "Extra args passed to 'docker build'."
12+
required: false
13+
src-path:
14+
description: "What folder to be (generally to find the Dockerfile in) default is root of repo"
15+
required: false
16+
image-ref:
17+
description: "The version number or sha used in creating image tag"
18+
required: false
19+
20+
21+
runs:
22+
using: 'composite'
23+
steps:
24+
- uses: FranzDiebold/github-env-vars-action@v2
25+
# https://github.com/aws-actions/amazon-ecr-login
26+
- name: Set up Docker Buildx
27+
uses: docker/setup-buildx-action@v3
28+
- name: Login to Amazon ECR
29+
id: login-ecr
30+
uses: aws-actions/amazon-ecr-login@v2
31+
- shell: bash
32+
run: |
33+
# Env var parsing
34+
35+
INPUT_SRC_PATH=${{ inputs.src-path }}
36+
SRC_PATH=${INPUT_SRC_PATH:-"./"}
37+
INPUT_IMAGE_REF=${{ inputs.image-ref }}
38+
IMAGE_REF=${INPUT_IMAGE_REF:-$CI_SHA}
39+
IMAGE_NAME=${{ inputs.module-name }}
40+
REPO_IMAGE=${{ steps.login-ecr.outputs.registry }}/$IMAGE_NAME
41+
DOCKER_BUILDKIT=1
42+
ENVIRONMENT=${{ inputs.build-for-environment }}
43+
BRANCH_NAME=${{env.ENV_NAME}}
44+
IMAGE_TAG=$ENVIRONMENT-$IMAGE_REF
45+
46+
# Create repo if needed
47+
aws ecr create-repository --repository-name $IMAGE_NAME && \
48+
aws ecr set-repository-policy --repository-name $IMAGE_NAME --policy-text "$(cat ${{ github.action_path }}/shared-ecr-policy.json)" || \
49+
true # Just let this fail if the repo already exists
50+
51+
docker buildx create --name=remote-buildkit-agent --driver=remote --use tcp://remote-buildkit-agent.infrastructure.svc.cluster.local:80 || true # Create the builder (might already exist)
52+
53+
cd $SRC_PATH
54+
BUILD_ARGS="--build-arg BUILD_FOR_ENVIRONMENT=$ENVIRONMENT --build-arg IMAGE_TAG=$IMAGE_TAG"
55+
56+
# Finally, build our runner container
57+
docker buildx build ${{ inputs.extra-build-args }} $BUILD_ARGS -t $REPO_IMAGE:$IMAGE_TAG -t $REPO_IMAGE:latest --push .

build-push-ecr/shared-ecr-policy.json

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Sid": "AllowAllAccountsWhichNeedReadAccess",
6+
"Effect": "Allow",
7+
"Principal": {
8+
"AWS": [
9+
"arn:aws:iam::454861456664:root",
10+
"arn:aws:iam::551913903968:root",
11+
"arn:aws:iam::040931223031:root"
12+
]
13+
},
14+
"Action": [
15+
"ecr:BatchCheckLayerAvailability",
16+
"ecr:BatchGetImage",
17+
"ecr:GetDownloadUrlForLayer"
18+
]
19+
},
20+
{
21+
"Sid": "AllowMasterAccountOrRolesWhichNeedsWriteAccess",
22+
"Effect": "Allow",
23+
"Principal": {
24+
"AWS": "arn:aws:iam::454861456664:root"
25+
},
26+
"Action": [
27+
"ecr:BatchCheckLayerAvailability",
28+
"ecr:BatchGetImage",
29+
"ecr:CompleteLayerUpload",
30+
"ecr:DescribeImages",
31+
"ecr:DescribeRepositories",
32+
"ecr:GetAuthorizationToken",
33+
"ecr:GetDownloadUrlForLayer",
34+
"ecr:GetRepositoryPolicy",
35+
"ecr:InitiateLayerUpload",
36+
"ecr:ListImages",
37+
"ecr:PutImage",
38+
"ecr:UploadLayerPart"
39+
]
40+
}
41+
]
42+
}

docker-bake-cache/action.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Builds our docker bake file without pushing to a repo, and pushes the layers to repo cache
2+
# This is separated into its own action so that it can be done in parallel with other actions after the build is finished
3+
4+
runs:
5+
using: 'composite'
6+
steps:
7+
# Get clean environment variables via https://github.com/marketplace/actions/github-environment-variables-action
8+
- uses: FranzDiebold/github-env-vars-action@v2
9+
- name: Set up Docker Buildx
10+
uses: docker/setup-buildx-action@v3
11+
- name: Login to Amazon ECR
12+
uses: aws-actions/amazon-ecr-login@v2
13+
- name: Build and push
14+
shell: bash
15+
run: |
16+
# Get a githash or tag name to use as image prefix
17+
TAG_NAME=${{ github.event.release.tag_name }}
18+
GIT_SHA=${{ env.CI_SHA }}
19+
IMAGE_PREFIX=${TAG_NAME:-$GIT_SHA}
20+
21+
# Configure our buildkit builders
22+
docker buildx create --name remote-buildkit-agent --node mdb_amd64 --platform linux/amd64 --driver=remote --use tcp://remote-buildkit-agent.infrastructure.svc.cluster.local:80 || true # Create the builder (might already exist)
23+
docker buildx create --name=remote-buildkit-agent --node mdb_arm64 --platform linux/arm64 --append --driver=remote --use tcp://remote-buildkit-agent-arm.infrastructure.svc.cluster.local:80 || true # Same for ARM
24+
25+
# Build each platform individually and don't push (bake file has logic to push cache when only one platform is built)
26+
VERSION=$IMAGE_PREFIX BRANCH=${{ env.CI_ACTION_REF_NAME }} PLATFORMS=linux/amd64 docker buildx bake --progress plain -f docker/docker-bake.hcl --print
27+
VERSION=$IMAGE_PREFIX BRANCH=${{ env.CI_ACTION_REF_NAME }} PLATFORMS=linux/arm64 docker buildx bake --progress plain -f docker/docker-bake.hcl --print
28+
VERSION=$IMAGE_PREFIX BRANCH=${{ env.CI_ACTION_REF_NAME }} PLATFORMS=linux/amd64 docker buildx bake --progress plain -f docker/docker-bake.hcl
29+
VERSION=$IMAGE_PREFIX BRANCH=${{ env.CI_ACTION_REF_NAME }} PLATFORMS=linux/arm64 docker buildx bake --progress plain -f docker/docker-bake.hcl

docker-bake/action.yml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Builds our docker bake file
2+
inputs:
3+
push-to-dockerhub:
4+
description: "Whether to push to Dockerhub as well as ECR"
5+
required: false
6+
default: false
7+
8+
runs:
9+
using: 'composite'
10+
steps:
11+
# Get clean environment variables via https://github.com/marketplace/actions/github-environment-variables-action
12+
- uses: FranzDiebold/github-env-vars-action@v2
13+
- name: Set up Docker Buildx
14+
uses: docker/setup-buildx-action@v3
15+
- name: Login to Amazon ECR
16+
uses: aws-actions/amazon-ecr-login@v2
17+
- name: Build and push
18+
shell: bash
19+
run: |
20+
# Get a githash or tag name to use as image prefix
21+
TAG_NAME=${{ github.event.release.tag_name }}
22+
GIT_SHA=${{ env.CI_SHA }}
23+
IMAGE_PREFIX=${TAG_NAME:-$GIT_SHA}
24+
25+
# Configure our buildkit builders
26+
docker buildx create --name remote-buildkit-agent --node mdb_amd64 --platform linux/amd64 --driver=remote --use tcp://remote-buildkit-agent.infrastructure.svc.cluster.local:80 || true # Create the builder (might already exist)
27+
docker buildx create --name=remote-buildkit-agent --node mdb_arm64 --platform linux/arm64 --append --driver=remote --use tcp://remote-buildkit-agent-arm.infrastructure.svc.cluster.local:80 || true # Same for ARM
28+
29+
# Print what bake is going to do
30+
VERSION=$IMAGE_PREFIX BRANCH=${{ env.CI_ACTION_REF_NAME }} PUSH_TO_DOCKERHUB=${{ inputs.push-to-dockerhub }} PLATFORM="" docker buildx bake -f docker/docker-bake.hcl --print
31+
32+
# Build amd64 and arm64 images and push to repos
33+
VERSION=$IMAGE_PREFIX BRANCH=${{ env.CI_ACTION_REF_NAME }} PUSH_TO_DOCKERHUB=${{ inputs.push-to-dockerhub }} docker buildx bake --progress plain --push -f docker/docker-bake.hcl

get-deploy-labels/action.yml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Get a list of environments we want to deploy to, and save it as an output
2+
outputs:
3+
deploy-envs:
4+
value: ${{ steps.make-label-list.outputs.deploy-envs }}
5+
6+
runs:
7+
using: "composite"
8+
steps:
9+
- id: make-label-list
10+
shell: bash
11+
run: |
12+
# Get a json list of labels for this PR, and discard anything not starting with "deploy-to-"
13+
DEPLOY_ENVS=`echo '${{ toJson(github.event.pull_request.labels.*.name) }}' | jq -c 'map(select(test("^deploy-to-")))'`
14+
15+
# Delete "deploy-to-" so we're just left with the env names
16+
DEPLOY_ENVS=${DEPLOY_ENVS//deploy-to-/}
17+
18+
# Remove empty strings
19+
# DEPLOY_ENVS=${DEPLOY_ENVS//\"\"/}
20+
21+
# Lowercase the whole list and output it
22+
echo "deploy-envs=${DEPLOY_ENVS,,}" >> "$GITHUB_OUTPUT"

helm-deploy/action.yml

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Deploy a single service via helm
2+
inputs:
3+
image-tag:
4+
description: "The image tag to use in the deployments."
5+
required: true
6+
k8s-namespace:
7+
description: "Deployment namespace in kubernetes."
8+
required: true
9+
environment-slug:
10+
description: "Short name of deployment environment. Should be like 'dev', 'prod'. Set this if you have a values-<env>.yaml."
11+
required: false
12+
helm-extra-args:
13+
description: "Add additional/custom helm arguments/commands."
14+
required: false
15+
helm-chart-name:
16+
description: "Helm chart name (eg: mindsdb-gateway)"
17+
required: true
18+
dry-run:
19+
description: "Skip the actual deployment and just show a diff."
20+
required: false
21+
default: false
22+
timeout:
23+
description: "The timeout time for helm operations."
24+
required: false
25+
default: 300s
26+
27+
runs:
28+
using: 'composite'
29+
steps:
30+
- shell: bash
31+
run: |
32+
33+
(kubectl --help &> /dev/null && helm diff version &> /dev/null) || (echo "Please install kubectl, helm, and helm-diff in your runner. Alternatively use one of our docker-based versions of this action: https://github.com/DevOps-Nirvana/" && exit 1)
34+
35+
HELM_IMAGE_TAG=${{ inputs.image-tag }}
36+
HELM_K8S_NAMESPACE=${{ inputs.k8s-namespace }}
37+
HELM_ENVIRONMENT_SLUG=${{ inputs.environment-slug }}
38+
HELM_DRY_RUN=${{ inputs.dry-run }}
39+
HELM_EXTRA_ARGS=${{ inputs.helm-extra-args }}
40+
HELM_TIMEOUT=${{ inputs.timeout }}
41+
CURRENT_HELM_CHART=${{ inputs.helm-chart-name }}
42+
43+
cd deployment
44+
45+
# Creating namespace if necessary
46+
kubectl create namespace $HELM_K8S_NAMESPACE || true
47+
48+
# Setup our helm args
49+
export HELM_EXTRA_ARGS="$HELM_EXTRA_ARGS --set image.tag=$HELM_IMAGE_TAG --set global.image.tag=$HELM_IMAGE_TAG --set global.namespace=$HELM_K8S_NAMESPACE";
50+
51+
52+
echo "Update our helm chart dependencies"
53+
helm dependency update $CURRENT_HELM_CHART || true
54+
55+
# Discover values files
56+
VALUES_ENV_FILE=`find $CURRENT_HELM_CHART -name values-${HELM_ENVIRONMENT_SLUG}.yaml`
57+
VALUES_FILE_ARGS="-f $CURRENT_HELM_CHART/values.yaml${VALUES_ENV_FILE:+ -f $VALUES_ENV_FILE}"
58+
59+
echo "--- HELM DIFF ---"
60+
helm diff upgrade --allow-unreleased --namespace $HELM_K8S_NAMESPACE $HELM_UPDIFF_EXTRA_ARGS $CURRENT_HELM_CHART ./$CURRENT_HELM_CHART \
61+
$VALUES_FILE_ARGS \
62+
$HELM_EXTRA_ARGS
63+
64+
if [ "$HELM_DRY_RUN" = "false" ]; then
65+
echo "--- HELM UPGRADE ---"
66+
helm upgrade --install --atomic --timeout $HELM_TIMEOUT --namespace $HELM_K8S_NAMESPACE $CURRENT_HELM_CHART ./$CURRENT_HELM_CHART \
67+
$VALUES_FILE_ARGS \
68+
$HELM_EXTRA_ARGS;
69+
fi

setup-env/action.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Set up all of the CI env vars required for MindsDB
2+
3+
runs:
4+
using: "composite"
5+
steps:
6+
# Get clean environment variables via https://github.com/marketplace/actions/github-environment-variables-action
7+
- uses: FranzDiebold/github-env-vars-action@v2
8+
- shell: bash
9+
run: |
10+
echo "REF_SLUG=${CI_REF_NAME_SLUG:-$CI_HEAD_REF_SLUG}" >> $GITHUB_ENV # Use whichever env ref is supplied (push or merge).
11+
- id: set-envs
12+
shell: bash
13+
run: |
14+
echo "SLUG=$CI_SHA" >> $GITHUB_ENV
15+
# Figure out the namespace and environment name, which should be up to 63 chars in length for Kubernetes
16+
# This will also strip tailing dash characters, necessary because we can't have dash suffixed strings in Kubernetes
17+
export STRIPPED_STRING=`echo "${{ env.CI_REPOSITORY_NAME }}-${{ env.REF_SLUG }}" | head -c 63 | sed 's/-*$//g'`
18+
echo "ENV_NAME=$STRIPPED_STRING" >> $GITHUB_ENV
19+
echo "ENV_URL=https://${STRIPPED_STRING}.dev.mindsdb.com" >> $GITHUB_ENV

slack-deploy-msg/action.yml

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
inputs:
2+
channel-id:
3+
description: "Slack channel ID"
4+
required: true
5+
status:
6+
description: "Status of the deployment"
7+
required: true
8+
color:
9+
description: "Color of message"
10+
required: true
11+
env-name:
12+
description: "Deloy env name"
13+
required: true
14+
env-url:
15+
description: "Deloy env url"
16+
required: true
17+
slack-token:
18+
description: "Slack bot token"
19+
required: true
20+
update-message-id:
21+
description: "ID of the slack message to update"
22+
required: false
23+
default: ""
24+
25+
outputs:
26+
ts:
27+
description: "Slack message ID"
28+
value: ${{ steps.slack.outputs.ts }}
29+
30+
runs:
31+
using: 'composite'
32+
steps:
33+
- name: Notify of deployment
34+
id: slack
35+
uses: slackapi/[email protected]
36+
with:
37+
channel-id: ${{ inputs.channel-id }}
38+
update-ts: ${{ inputs.update-message-id }}
39+
payload: |
40+
{
41+
"attachments": [
42+
{
43+
"color": "${{ inputs.color }}",
44+
"blocks": [
45+
{
46+
"type": "section",
47+
"text": {
48+
"type": "mrkdwn",
49+
"text": "*<${{ github.event.repository.html_url }}|${{ github.event.repository.name }}>* has ${{ inputs.status }} deploying to *<${{ inputs.env-url }}|${{ inputs.env-name }}>*"
50+
},
51+
"fields": [
52+
{
53+
"type": "mrkdwn",
54+
"text": "*PR*\n<${{ github.event.pull_request._links.html.href }}|${{ github.event.pull_request.title }} (#${{ github.event.number }})>"
55+
},
56+
{
57+
"type": "mrkdwn",
58+
"text": "*User*\n${{ github.triggering_actor }}"
59+
},
60+
{
61+
"type": "mrkdwn",
62+
"text": "*Commit*\n<${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }}|${{ github.sha }}>"
63+
},
64+
{
65+
"type": "mrkdwn",
66+
"text": "*Workflow Run*\n<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|${{ github.workflow }}>"
67+
},
68+
{
69+
"type": "mrkdwn",
70+
"text": "*Branch*\n<${{ github.server_url }}/${{ github.repository }}/tree/${{ github.head_ref || github.ref_name }}|${{ github.head_ref || github.ref_name }}>"
71+
}
72+
]
73+
}
74+
]
75+
}
76+
]
77+
}
78+
env:
79+
SLACK_BOT_TOKEN: ${{ inputs.slack-token }}

0 commit comments

Comments
 (0)