Skip to content

Commit

Permalink
Enable github workflow (#804)
Browse files Browse the repository at this point in the history
* Enable github workflow which allows us to autocommit

* Adopt the python based version string identifier format

* Go back to pre stable versioning to align with semantic versioning

* Only build dataapi on pushes to master
  • Loading branch information
hellais authored Feb 15, 2024
1 parent 969ae93 commit cfa43b8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
62 changes: 50 additions & 12 deletions .github/workflows/build_dataapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ name: Build and Deploy Docker Image for dataapi
on:
push:
branches:
- backend-v2024.2.0
#paths:
# - "api/fastapi/**"
- master
paths:
- "api/fastapi/**"
- ".github/workflows/build_dataapi.yml"

env:
oonidataapi_dir: ./api/fastapi
IMAGE_NAME: ooni/dataapi
DOCKERFILE_PATH: ./api/fastapi/

jobs:
build_and_push:
Expand All @@ -24,25 +25,62 @@ jobs:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}

- name: Format version number
- name: Format version information
id: version
run: |
DATE=$(date +'%Y%m%d')
SHORT_SHA=$(echo ${{ github.sha }} | cut -c1-4)
TAG="v${DATE}-${SHORT_SHA}"
echo "tag=$TAG" >> "$GITHUB_OUTPUT"
SHORT_SHA=$(echo ${{ github.sha }} | cut -c1-8)
BUILD_LABEL="${DATE}-${SHORT_SHA}"
echo "build_label=$BUILD_LABEL" >> "$GITHUB_OUTPUT"
VERSION_NUMBER=$(cat ${{ env.oonidataapi_dir }}/pyproject.toml | grep 'version =' | awk -F '"' '{print $2}')
echo "version_number=$VERSION_NUMBER" >> "$GITHUB_OUTPUT"
- name: Build and Push Docker Image
env:
DOCKERFILE_PATH: ${{ env.oonidataapi_dir }}
run: |
BUILD_LABEL=${{ steps.version.outputs.tag }}
TAG_LATEST=$IMAGE_NAME:latest
TAG_ENVIRONMENT=$IMAGE_NAME:production
TAG_VERSION=$IMAGE_NAME:$BUILD_LABEL
TAG_BUILD_LABEL=$IMAGE_NAME:${{ steps.version.outputs.build_label }}
TAG_VERSION=$IMAGE_NAME:v${{ steps.version.outputs.version_number }}
# Build Docker image with multiple tags
docker build --build-arg BUILD_LABEL=$BUILD_LABEL -t $TAG_LATEST -t $TAG_VERSION -t $TAG_ENVIRONMENT $DOCKERFILE_PATH
docker build --build-arg BUILD_LABEL=${{ steps.version.outputs.build_label }} \
-t $TAG_BUILD_LABEL \
-t $TAG_ENVIRONMENT \
-t $TAG_LATEST \
-t $TAG_VERSION \
$DOCKERFILE_PATH
# Push all tags
docker push $TAG_BUILD_LABEL
docker push $TAG_ENVIRONMENT
docker push $TAG_LATEST
docker push $TAG_VERSION
docker push $TAG_ENVIRONMENT
- name: Checkout ooni/devops
uses: actions/checkout@v2
with:
repository: "ooni/devops" # Replace with your repository's name
ssh-key: ${{ secrets.OONI_DEVOPS_DEPLOYKEY }}
path: "ooni-devops"
ref: "tf-actions"

- name: Bump version of dataapi
run: |
jq --arg value "v${{ steps.version.outputs.version_number }}" \
'(.ooni_service_config.dataapi_version) = $value' \
${BASE_DIR}/terraform.tfvars.json > ${BASE_DIR}/terraform.tfvars.json.tmp \
&& mv ${BASE_DIR}/terraform.tfvars.json.tmp ${BASE_DIR}/terraform.tfvars.json
env:
BASE_DIR: "ooni-devops/tf/environments/production"

- name: Commit changes
id: commit
run: |
cd ooni-devops
git config --global user.email "[email protected]"
git config --global user.name "OONI Github Actions Bot"
git add .
git commit -m "auto: update oonidataapi package version to v${{ steps.version.outputs.version_number }}" || echo "No changes to commit"
git push origin
4 changes: 2 additions & 2 deletions api/fastapi/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ RUN curl -sSL https://install.python-poetry.org | POETRY_HOME=/opt/poetry python
COPY . /app
WORKDIR /app
ENV PATH=/opt/poetry/bin:$PATH
RUN echo "$BUILD_LABEL" > /app/dataapi/BUILD_LABEL
RUN echo "$BUILD_LABEL" > /app/oonidataapi/BUILD_LABEL
RUN poetry config virtualenvs.in-project true && poetry install --no-interaction --no-ansi

### Actual image running on the host
Expand All @@ -26,5 +26,5 @@ ENV PYTHONUNBUFFERED 1

COPY --from=py-build /app /app
WORKDIR /app
CMD ["/app/.venv/bin/uvicorn", "dataapi.main:app", "--host", "0.0.0.0", "--port", "80"]
CMD ["/app/.venv/bin/uvicorn", "oonidataapi.main:app", "--host", "0.0.0.0", "--port", "80"]
EXPOSE 80
2 changes: 1 addition & 1 deletion api/fastapi/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ Running:

```
poetry install
poetry run uvicorn dataapi.main:app
poetry run uvicorn oonidataapi.main:app
```
2 changes: 1 addition & 1 deletion api/fastapi/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "oonidataapi"
version = "0.1.0"
version = "0.2.0.dev1"
description = ""
authors = ["OONI <[email protected]>"]
readme = "Readme.md"
Expand Down

0 comments on commit cfa43b8

Please sign in to comment.