Ssh prep for pipeline
Jump to navigation
Jump to search
Example of Using SSH for simple deployment in pipeline
Code
main.sh
#!/usr/bin/env bash set -ex shopt -s expand_aliases prepSsh(){ ssh_host=$SSH_HOST ssh_user=$SSH_USER ssh_conn=$SSH_USER@$SSH_HOST ssh_id_file=.ssh/id mkdir -p .ssh echo "$SSH_KNOWN_HOSTS" > .ssh/known_hosts echo "$SSH_SECRET_KEY" > $ssh_id_file sudo chmod 600 $ssh_id_file ssh_opts="-o StrictHostKeyChecking=yes \ -o UserKnownHostsFile=.ssh/known_hosts \ -o ConnectionAttempts=10" ssh_cmd="ssh -p 22 -i $ssh_id_file ${ssh_opts}" alias ssh="${ssh_cmd}" alias scp="scp -P 22 -i $ssh_id_file ${ssh_opts}" alias rsync="rsync -avz --rsync-path=\"sudo rsync\" -e \"${ssh_cmd}\"" } prepSsh testConn(){ ssh $ssh_conn whoami ssh $ssh_conn hostname exit }#!/usr/bin/env bash set -ex shopt -s expand_aliases prepSsh(){ ssh_host=$SSH_HOST ssh_user=$SSH_USER ssh_conn=$SSH_USER@$SSH_HOST ssh_id_file=.ssh/id mkdir -p .ssh echo "$SSH_KNOWN_HOSTS" > .ssh/known_hosts echo "$SSH_SECRET_KEY" > $ssh_id_file sudo chmod 600 $ssh_id_file ssh_opts="-o StrictHostKeyChecking=yes \ -o UserKnownHostsFile=.ssh/known_hosts \ -o ConnectionAttempts=10" ssh_cmd="ssh -p 22 -i $ssh_id_file ${ssh_opts}" alias ssh="${ssh_cmd}" alias scp="scp -P 22 -i $ssh_id_file ${ssh_opts}" alias rsync="rsync -avz --rsync-path=\"sudo rsync\" -e \"${ssh_cmd}\"" } prepSsh testConn(){ ssh $ssh_conn whoami ssh $ssh_conn hostname exit } #!/usr/bin/env bash set -ex shopt -s expand_aliases prepSsh(){ ssh_host=$SSH_HOST ssh_user=$SSH_USER ssh_conn=$SSH_USER@$SSH_HOST ssh_id_file=.ssh/id mkdir -p .ssh echo "$SSH_KNOWN_HOSTS" > .ssh/known_hosts echo "$SSH_SECRET_KEY" > $ssh_id_file sudo chmod 600 $ssh_id_file ssh_opts="-o StrictHostKeyChecking=yes \ -o UserKnownHostsFile=.ssh/known_hosts \ -o ConnectionAttempts=10" ssh_cmd="ssh -p 22 -i $ssh_id_file ${ssh_opts}" alias ssh="${ssh_cmd}" alias scp="scp -P 22 -i $ssh_id_file ${ssh_opts}" alias rsync="rsync -avz --rsync-path=\"sudo rsync\" -e \"${ssh_cmd}\"" } prepSsh testConn(){ ssh $ssh_conn whoami ssh $ssh_conn hostname exit } deploy(){ scp -r files $ssh_conn:~/files ssh $ssh_conn 'bash -s' < updateMyapp.sh } main(){ testConn deploy }
Github Actions
.github/workflows/main.yaml
name: Manage k8s ADC/LB on: push: branches: [ main ] jobs: DeployApp: runs-on: [self-hosted] steps: - uses: actions/checkout@v2 - name: Run main.sh env: SSH_USER: ${{secrets.SSH_USER}} SSH_HOST: ${{secrets.SSH_HOST}} SSH_SECRET_KEY: ${{secrets.SSH_SECRET_KEY}} SSH_KNOWN_HOSTS: ${{secrets.SSH_KNOWN_HOSTS}} shell: bash run: ./main.sh
vim .env
export SSH_USER= export SSH_HOST= export SSH_SECRET_KEY= export SSH_KNOWN_HOSTS=
Source .env to set environmental variables.
. .env
Run main.sh
main.sh