diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..182a742 --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,97 @@ +name: Python CI Pipeline + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +env: + PYTHON_VERSION: 3.11 + +jobs: + # Step 1: Install and Build + build: + runs-on: ubuntu-latest + defaults: + run: + working-directory: app + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: ${{ env.PYTHON_VERSION }} + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + + # Step 2: Testing + test: + runs-on: ubuntu-latest + needs: build + defaults: + run: + working-directory: app + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Install test dependencies and run tests + run: | + pip install -r requirements.txt + + # Step 3: Docker image build and push (optional) + docker: + runs-on: ubuntu-latest + needs: [ test] + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . + file: docker/Dockerfile + push: true + tags: manojkumar8008/myapp1:latest + deploy: + name: Deploy to EC2 + runs-on: ubuntu-latest + needs: docker + steps: + - name: Setup SSH key + run: | + mkdir -p ~/.ssh + echo -e "${{ secrets.EC2_SSH_KEY }}" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + + - name: Add EC2 host to known hosts + run: | + ssh-keyscan -H ${{ vars.EC2_HOST }} >> ~/.ssh/known_hosts + + - name: Deploy Docker container on EC2 + run: | + ssh -i ~/.ssh/id_rsa ${{ vars.EC2_USER }}@${{ vars.EC2_HOST }} << 'EOF' + docker pull manojkumar8008/myapp1:latest + docker stop myapp || true + docker rm myapp || true + docker run -d --name myapp -p 80:8000 manojkumar8008/myapp1:latest + EOF + + diff --git a/.github/workflows/newaws.yml b/.github/workflows/newaws.yml new file mode 100644 index 0000000..bd10e3c --- /dev/null +++ b/.github/workflows/newaws.yml @@ -0,0 +1,111 @@ +name: Python CI Pipeline + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +env: + PYTHON_VERSION: 3.11 + AWS_REGION: us-east-1 + ECR_REPOSITORY: meta/hello-world + LAMBDA_FUNCTION_NAME: python-app + +permissions: + id-token: write + contents: read + +jobs: + # Step 1: Install and Build + build: + runs-on: ubuntu-latest + defaults: + run: + working-directory: app + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: ${{ env.PYTHON_VERSION }} + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + + # Step 2: Testing + test: + runs-on: ubuntu-latest + needs: build + defaults: + run: + working-directory: app + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Install test dependencies and run tests + run: | + pip install -r requirements.txt + + # Step 3: Docker image build and push (optional) + docker: + runs-on: ubuntu-latest + outputs: + image-uri: ${{ steps.build-image.outputs.image }} + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + role-to-assume: ${{ secrets.AWS_ROLE_ARN }} + role-session-name: GitHubActions + aws-region: ${{ env.AWS_REGION }} + + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v2 + + - name: Build, tag, and push image to Amazon ECR + id: build-image + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + IMAGE_TAG: ${{ github.sha }} + run: | + # Build a docker container and push it to ECR + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG + echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT + deploy: + needs: docker + runs-on: ubuntu-latest + + steps: + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + role-to-assume: ${{ secrets.AWS_ROLE_ARN }} + role-session-name: GitHubActions-Deploy + aws-region: ${{ env.AWS_REGION }} + + - name: Update Lambda function + env: + IMAGE_URI: ${{ needs.docker.outputs.image-uri }} + run: | + aws lambda update-function-code \ + --function-name ${{ env.LAMBDA_FUNCTION_NAME }} \ + --image-uri $IMAGE_URI + + - name: Wait for Lambda function update to complete + run: | + aws lambda wait function-updated \ + --function-name ${{ env.LAMBDA_FUNCTION_NAME }} + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4ffc93e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM public.ecr.aws/lambda/python:3.11 + +# Copy function code +COPY app.py ${LAMBDA_TASK_ROOT} + +# Copy requirements and install +COPY requirements.txt . +RUN pip install -r requirements.txt --target "${LAMBDA_TASK_ROOT}" + +# Set the CMD to your handler +CMD [ "app.hello" ] \ No newline at end of file diff --git a/README.md b/README.md index 8434dfa..cae4549 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# hello-python +# hello-python11 Very simple hello world python Flask application. diff --git a/app.py b/app.py new file mode 100644 index 0000000..d067241 --- /dev/null +++ b/app.py @@ -0,0 +1,10 @@ +import json + +def hello(event, context): + return { + 'statusCode': 200, + 'body': json.dumps({ + 'message': 'Hello Lambda!!', + 'version': '1.0' + }) + } diff --git a/app/main.py b/app/main.py index 5aff7f2..2dde181 100644 --- a/app/main.py +++ b/app/main.py @@ -3,7 +3,7 @@ @app.route("/") def hello(): - return "Hello from Python!" + return "Hellopy!" if __name__ == "__main__": - app.run(host='0.0.0.0') + app.run(host='0.0.0.0', port=8000) diff --git a/app/requirements.txt b/app/requirements.txt index 2077213..663bd1f 100644 --- a/app/requirements.txt +++ b/app/requirements.txt @@ -1 +1 @@ -Flask \ No newline at end of file +requests \ No newline at end of file diff --git a/docker/Dockerfile b/docker/Dockerfile index ce620c2..79293e9 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,9 +1,9 @@ FROM python:3.7 -RUN mkdir /app -WORKDIR /app -ADD . /app/ +RUN mkdir /wd +COPY /app/ /wd/ +WORKDIR /wd RUN pip install -r requirements.txt EXPOSE 5000 -CMD ["python", "/app/main.py"] \ No newline at end of file +CMD ["python", "main.py"] diff --git a/docker/pipeline.yaml b/docker/pipeline.yaml new file mode 100644 index 0000000..b79a24b --- /dev/null +++ b/docker/pipeline.yaml @@ -0,0 +1,34 @@ +name: Docker Image java + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Build the Docker image + run: docker build . --file Dockerfile --tag + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + +- name: Log in to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + +- name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . + push: true + tags: username/myapp:latest + diff --git a/jenkins.properties b/jenkins.properties new file mode 100644 index 0000000..5f024bf --- /dev/null +++ b/jenkins.properties @@ -0,0 +1,2 @@ +IMAGE_NAME=manojkumar8008/hello-world-pythom +IMAGE_TAG=latest diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..663bd1f --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +requests \ No newline at end of file