Last active
April 17, 2025 23:36
-
-
Save ADoebeling/824c87533266675840b3a27f7e85d7cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# GitHub Actions Workflow: Deploy `main` Branch to Webserver and Create a Release | |
# | |
# Author: Andreas Döbeling <https://Andreas.Doebeling.de> | |
# Copyright: DÖBELING Projektbüro <https://Doebeling.de> | |
# License: GNU General Public License (GPL) v3 | |
# Link: https://gist.github.com/ADoebeling/824c87533266675840b3a27f7e85d7cf | |
# | |
# | |
# Summary: | |
# This GitHub Actions workflow automates the deployment of the `main` branch to a web server via SFTP | |
# and creates a corresponding GitHub release. | |
# | |
# Steps: | |
# 1. Checks out the repository for deployment. | |
# 2. Creates a deployment using the GitHub API. | |
# 3. Generates a timestamp for the release name. | |
# 4. Uploads files to a web server via SFTP. | |
# 5. Publishes a GitHub release with the deployment timestamp. | |
# 6. Marks the deployment as successful in GitHub. | |
# | |
# Parameters: | |
# | |
# Secrets: | |
# - GITHUB_TOKEN: | |
# Automatically provided by GitHub for authenticating API actions like creating deployments and releases. | |
# - SFTP_PWD: | |
# The password for the SFTP account used to upload files to the web server. | |
# - OPENSSH_PRIVATE: | |
# A private SSH key used for securely accessing the web server via SFTP. | |
# | |
# Variables: | |
# - SFTP_USER: | |
# Username for logging into the web server via SFTP. Example: `deploy_user`. | |
# - SFTP_HOST: | |
# Hostname or IP of the web server for deployment (e.g., `123.45.67.89` or `example.com`). | |
# - SFTP_PATH: | |
# Target directory on the web server where the files will be uploaded. Example: `/var/www/html`. | |
# | |
# Workflow overview: | |
# - Checkout Repository: | |
# Fetches the complete repository history to ensure proper deployment. | |
# - Create Deployment: | |
# Registers a deployment via the GitHub API and retrieves a `deployment_id` for tracking. | |
# - Generate Timestamp: | |
# Creates a timestamp (format: `yymmdd_HHMM`) for tagging and naming the release. | |
# - SFTP Upload: | |
# Uses the `actions-file-deployer` to upload files from the repository to the specified path on the web server. | |
# - Publish Release: | |
# Creates a GitHub release with a tag and name based on the timestamp, also adding deployment details. | |
# - Mark Deployment Successful: | |
# Updates the deployment status in GitHub as "success" after verification. | |
name: Deploy `main` to webserver and create release | |
run-name: Deploy `main` to webserver and create release | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
Deploy-and-Release: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Git Checkout | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Create Deployment | |
id: create_deployment | |
run: | | |
response=$(curl -s -X POST \ | |
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
https://api.github.com/repos/${{ github.repository }}/deployments \ | |
-d '{ | |
"ref": "main", | |
"required_contexts": [], | |
"environment": "${{ vars.SFTP_USER }}@${{ vars.SFTP_HOST }}", | |
"description": "Deploying to ${{ vars.SFTP_USER }}@${{ vars.SFTP_HOST }}" | |
}') | |
deployment_id=$(echo "$response" | jq -r '.id') | |
if [ "$deployment_id" == "null" ] || [ -z "$deployment_id" ]; then | |
echo "Error: Invalid Deployment ID" | |
echo "API Response: $response" | |
exit 1 | |
fi | |
echo "Deployment ID: $deployment_id" | |
echo "deployment_id=$deployment_id" >> $GITHUB_ENV | |
- name: Create Timestamp | |
id: tag_name | |
run: | | |
CURRENT_DATE=$(TZ="Europe/Berlin" date +"%y%m%d_%H%M") | |
echo "TIMESTAMP=${CURRENT_DATE}" >> $GITHUB_ENV | |
- name: "sFTP-Upload: ${{ vars.SFTP_USER }} }}@${{ vars.SFTP_HOST }}" | |
uses: milanmk/actions-file-deployer@master | |
with: | |
remote-protocol: "sftp" | |
remote-host: ${{ vars.SFTP_HOST }} | |
remote-user: ${{ vars.SFTP_USER }} | |
#remote-password: '${{ secrets.SFTP_PWD }}' | |
ssh-private-key: ${{ secrets.OPENSSH_PRIVATE}} | |
local-path: '.' | |
remote-path: '${{ vars.SFTP_PATH }}' | |
sync: full | |
- name: Create Release | |
id: create_release | |
uses: actions/create-release@v1 | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token | |
with: | |
tag_name: Published/${{ env.TIMESTAMP }} | |
release_name: Published ${{ env.TIMESTAMP }} | |
body: | | |
Deployed and published at ${{ vars.SFTP_HOST }} | |
draft: false | |
prerelease: false | |
- name: Deployment Successful | |
run: | | |
curl -X POST \ | |
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github.v3+json" \ | |
https://api.github.com/repos/${{ github.repository }}/deployments/${{ env.deployment_id }}/statuses \ | |
-d '{"state": "success", "description": "Successfully Deployed!"}' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment