From 42db33d0559dfa03eb52ff20181007b63be1a1cb Mon Sep 17 00:00:00 2001 From: bjarneo Date: Sat, 16 Nov 2024 15:03:01 +0100 Subject: [PATCH] feat: add the copepod action --- README.md | 4 +- workflows/action/codepod.yml | 104 +++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 workflows/action/codepod.yml diff --git a/README.md b/README.md index a079acf..c103b61 100644 --- a/README.md +++ b/README.md @@ -159,7 +159,7 @@ jobs: run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT - name: Deploy to production - uses: bjarneo/workflows/action/copepod/action.yml@master + uses: bjarneo/workflows/action/copepod.yml@main with: host: remote_host.com user: deploy_user @@ -203,7 +203,7 @@ jobs: # Example of rolling back if needed # NOTE: You want to have a manual approval step in between to ensure you want to rollback - name: Rollback production - uses: ./.github/action/copepod + uses: bjarneo/workflows/action/copepod.yml@main with: host: remote_host.com user: deploy_user diff --git a/workflows/action/codepod.yml b/workflows/action/codepod.yml new file mode 100644 index 0000000..c676f33 --- /dev/null +++ b/workflows/action/codepod.yml @@ -0,0 +1,104 @@ +name: Deploy with Copepod + +description: 'Deploy applications using Copepod' + +inputs: + host: + description: 'Remote host to deploy to' + required: true + user: + description: 'SSH user for remote host' + required: false + default: 'root' + image: + description: 'Docker image name' + required: true + dockerfile: + description: 'Path to the dockerfile' + required: false + default: 'Dockerfile' + tag: + description: 'Docker image tag' + required: false + default: 'latest' + platform: + description: 'Docker platform' + required: false + default: 'linux/amd64' + ssh_key: + description: 'SSH private key content' + required: true + container_name: + description: 'Name for the container' + required: true + container_port: + description: 'Container port' + required: true + host_port: + description: 'Host port' + required: true + env_file: + description: 'Environment file' + required: false + build_args: + description: 'Build arguments (comma-separated KEY=VALUE pairs)' + required: false + rollback: + description: 'Rollback to the previous version' + required: false + +runs: + using: "composite" + steps: + - name: Set up SSH key + shell: bash + run: | + mkdir -p ~/.ssh + echo "${{ inputs.ssh_key }}" > ~/.ssh/deploy_key + chmod 600 ~/.ssh/deploy_key + ssh-keyscan -H ${{ inputs.host }} >> ~/.ssh/known_hosts + + - name: Download Copepod + shell: bash + run: | + curl -L -o copepod "https://github.com/bjarneo/copepod/releases/latest/download/copepod-linux-amd64" + chmod +x copepod + + - name: Prepare build args + id: build_args + shell: bash + run: | + BUILD_ARGS="" + if [ -n "${{ inputs.build_args }}" ]; then + IFS=',' read -ra ARGS <<< "${{ inputs.build_args }}" + for arg in "${ARGS[@]}"; do + BUILD_ARGS="$BUILD_ARGS --build-arg $arg" + done + fi + echo "args=$BUILD_ARGS" >> $GITHUB_OUTPUT + + - name: Deploy with Copepod + shell: bash + env: + HOST: ${{ inputs.host }} + HOST_USER: ${{ inputs.user }} + HOST_PLATFORM: ${{ inputs.platform }} + HOST_PORT: ${{ inputs.host_port }} + DOCKER_IMAGE_NAME: ${{ inputs.image }} + DOCKER_IMAGE_TAG: ${{ inputs.tag }} + DOCKER_CONTAINER_NAME: ${{ inputs.container_name }} + DOCKER_CONTAINER_PORT: ${{ inputs.container_port }} + DOCKER_CONTAINER_ENV_FILE: ${{ inputs.env_file }} + run: | + if [ "${{ inputs.rollback }}" = "true" ]; then + ./copepod --rollback + else + ./copepod ${{ steps.build_args.outputs.args }} + fi + + - name: Upload deployment logs + if: always() + uses: actions/upload-artifact@v4 + with: + name: deployment-logs + path: copepod_deployment.log \ No newline at end of file