diff options
| author | Aleksa Vuckovic <aleksa@vuckovic.cc> | 2024-08-14 07:44:53 +0200 |
|---|---|---|
| committer | Aleksa Vuckovic <aleksa@vuckovic.cc> | 2024-08-18 00:27:13 +0200 |
| commit | 9bfe79d2ebe68f3b9dc2bb713dedc110fcd9babc (patch) | |
| tree | ceb4fc476e5b1912dcbd6fc9f53ddcb7fe87c27f | |
| -rw-r--r-- | .gitea/workflows/workflow.yml | 93 | ||||
| -rw-r--r-- | Dockerfile | 9 | ||||
| -rw-r--r-- | README.md | 1 | ||||
| -rw-r--r-- | main.py | 19 | ||||
| -rw-r--r-- | requirements.txt | 1 |
5 files changed, 123 insertions, 0 deletions
diff --git a/.gitea/workflows/workflow.yml b/.gitea/workflows/workflow.yml new file mode 100644 index 0000000..c1ae90c --- /dev/null +++ b/.gitea/workflows/workflow.yml @@ -0,0 +1,93 @@ +name: Build and Push Docker Image + +on: + push: + branches: + - master + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Log in to Gitea Docker registry + uses: docker/login-action@v2 + with: + registry: gitea.s1.ctf.rs + username: ${{ secrets.REGISTRY_USERNAME }} + password: ${{ secrets.REGISTRY_PASSWORD }} + + - name: Convert repository name to lowercase + id: convert_repo_name + run: | + REPO_NAME=${{ github.repository }} + LOWER_REPO_NAME=$(echo "$REPO_NAME" | awk '{print tolower($0)}') + echo "LOWER_REPO_NAME=$LOWER_REPO_NAME" >> $GITHUB_ENV + + - name: Get commit details + id: commit + run: | + echo "COMMIT_HASH=$(git rev-parse HEAD)" >> $GITHUB_ENV + echo "COMMIT_NAME=$(git log -1 --pretty=%B | base64)" >> $GITHUB_ENV + echo "COMMIT_DATE=$(date -d "$(git log -1 --pretty=format:%ci)" -u +"%Y-%m-%dT%H:%M:%SZ")" >> $GITHUB_ENV + + - name: Build and push Docker image + id: build + uses: docker/build-push-action@v3 + with: + context: . + push: true + tags: | + gitea.s1.ctf.rs/${{ env.LOWER_REPO_NAME }}:latest + gitea.s1.ctf.rs/${{ env.LOWER_REPO_NAME }}:${{ github.sha }} + + - name: Send POST request + env: + REPO_NAME: ${{ github.repository }} + COMMIT_HASH: ${{ env.COMMIT_HASH }} + COMMIT_NAME: ${{ env.COMMIT_NAME }} + COMMIT_DATE: ${{ env.COMMIT_DATE }} + DOCKER_URL: gitea.s1.ctf.rs/${{ env.LOWER_REPO_NAME }}:${{ github.sha }} + run: | + RETRY_DELAY=20 + + while true; do + echo "Sending POST request..." + + # Create JSON payload + JSON_PAYLOAD=$(jq -n \ + --arg repo_name "$REPO_NAME" \ + --arg commit_hash "$COMMIT_HASH" \ + --arg commit_message "$COMMIT_NAME" \ + --arg commit_date "$COMMIT_DATE" \ + --arg docker_url "$DOCKER_URL" \ + '{ + repo_name: $repo_name, + commit_hash: $commit_hash, + commit_message: $commit_message, + commit_date: $commit_date, + docker_url: $docker_url + }' + ) + + # Capture HTTP response code + RESPONSE=$(curl --max-time 30 -k -s -o /dev/null -w "%{http_code}" -X POST http://advantage.s1.ctf.rs/api/docker_deploy \ + -H "Content-Type: application/json" \ + -d "$JSON_PAYLOAD" + ) + + # Check HTTP status code + if [ "$RESPONSE" -eq 200 ]; then + echo "Request succeeded." + break + else + echo "Request failed with HTTP status $RESPONSE. Retrying in $RETRY_DELAY seconds..." + sleep $RETRY_DELAY + fi + done diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5700c97 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.11-slim + +WORKDIR /app +COPY requirements.txt requirements.txt +RUN pip install --no-cache-dir -r requirements.txt +COPY . . +EXPOSE 3000 + +CMD ["python", "main.py"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..e29e006 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Testing CI/CD pipeline @@ -0,0 +1,19 @@ +from flask import Flask, request, jsonify + +app = Flask(__name__) + +@app.route('/') +def home(): + return "Hello, World!" + +@app.route('/greet/<name>', methods=['GET']) +def greet(name): + return jsonify(message=f"Hello, {name}!") + +@app.route('/post_example', methods=['POST']) +def post_example(): + data = request.json + return jsonify(data) + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=3000) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7e10602 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +flask |
