Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CI/CD #136

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
name: CI

on:
push:
branches:
- develop
- staging
- master

jobs:
prepare_build:
runs-on: ubuntu-latest
steps:
- id: build_tag
name: Set build tag
run: |
IMAGE_TAG="${GITHUB_REF_NAME//\//_}"
echo "Using container image tag $IMAGE_TAG"
echo "tag=$IMAGE_TAG" >> "$GITHUB_OUTPUT"
outputs:
build_tag: ${{ steps.build_tag.outputs.tag }}

build_and_push:
needs: [prepare_build]
runs-on: ubuntu-latest
steps:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Checkout code
uses: actions/checkout@v2
- name: DockerHub login
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Push
run: |
docker buildx create --use

# Base part of the command
build_command="docker buildx build \
--progress=plain \
--cache-from ${{ vars.DOCKERHUB_ORGANIZATION }}/hope-dedupe-engine:${{ needs.prepare_build.outputs.build_tag }} \
--cache-from ${{ vars.DOCKERHUB_ORGANIZATION }}/hope-dedupe-engine:latest \
--cache-to ${{ vars.DOCKERHUB_ORGANIZATION }}/hope-dedupe-engine:${{ needs.prepare_build.outputs.build_tag }} \
--cache-to ${{ vars.DOCKERHUB_ORGANIZATION }}/hope-dedupe-engine:latest \
-t ${{ vars.DOCKERHUB_ORGANIZATION }}/hope-dedupe-engine:${{ needs.prepare_build.outputs.build_tag }} \
-t ${{ vars.DOCKERHUB_ORGANIZATION }}/hope-dedupe-engine:latest \
-f ./docker/Dockerfile \
--target dist \
--push ./"

if [ "${{ github.ref }}" = "refs/heads/master" ]; then
version=$(python3 -c "import sys; version=None; [version:=line.split('=')[1].strip().strip('\"') for line in open('pyproject.toml', 'r') if line.strip().startswith('version =')]; print(version if version else sys.exit(1))")
tagged_image=${{ vars.DOCKERHUB_ORGANIZATION }}/hope-dedupe-engine:$version
build_command="$build_command -t $tagged_image"
fi

eval $build_command

deploy:
runs-on: ubuntu-latest
needs: [build_and_push]
if: |
github.event_name == 'push' &&
(
github.ref == 'refs/heads/develop' ||
github.ref == 'refs/heads/staging' ||
github.ref == 'refs/heads/master'
)
steps:
- name: Trigger deploy
run: |
if [ ${{ github.ref }} == 'refs/heads/develop' ]; then
pipelineId=1309
elif [ ${{ github.ref }} == 'refs/heads/staging' ]; then
pipelineId=1410
elif [ ${{ github.ref }} == 'refs/heads/master' ]; then
pipelineId=1440
else
echo "No pipeline to trigger for ref ${{ github.ref }}"
exit 0
fi

IFS=',' read -ra pipelines <<< "$pipelineId"
for pipeline in "${pipelines[@]}"; do
jsonBody='{"variables": {"sha": {"isSecret": false, "value": "${{ github.sha }}"}, "tag": {"isSecret": false, "value": "${{ needs.prepare_build.outputs.build_tag }}"}}}'
contentLength=$(echo -n $jsonBody | wc -c)
project=ICTD-HCT-MIS
organization=unicef

echo Triggering deploy for pipeline $pipeline
echo JSON body: $jsonBody

curl -f -v -L \
-u ":${{ secrets.AZURE_PAT }}" \
-H "Content-Type: application/json" \
-H "Content-Length: $contentLength" \
-d "$jsonBody" \
https://dev.azure.com/$organization/$project/_apis/pipelines/$pipeline/runs?api-version=7.1-preview.1
if [ $? -ne 0 ]; then
echo "Failed to trigger deploy for pipeline $pipeline"
exit 1
fi
done
Loading