Update test_ansible.yml #44
This file contains 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
name: Test Ansible Playbook Setup | |
on: | |
push: | |
branches: | |
- fb_ansible_test | |
jobs: | |
ansible: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
- name: Set up SSH agent and add both keys | |
uses: webfactory/[email protected] | |
with: | |
ssh-private-key: | | |
${{ secrets.SSH_PRIVATE_KEY_1 }} | |
- name: Add SSH key to the agent | |
run: | | |
mkdir -p ~/.ssh | |
echo "${{ secrets.SSH_PRIVATE_KEY_1 }}" > ~/.ssh/id_rsa | |
chmod 600 ~/.ssh/id_rsa | |
ssh-add ~/.ssh/id_rsa | |
- name: Check SSH agent keys | |
run: ssh-add -l | |
- name: Add known hosts | |
run: | | |
echo "${{ secrets.GATEWAY_HOST_KEY }}" >> ~/.ssh/known_hosts | |
echo "${{ secrets.TARGET_HOST_KEY }}" >> ~/.ssh/known_hosts | |
- name: Establish SSH tunnel | |
run: | | |
ssh -vvv -f -N -L 8157:localhost:8157 -o ServerAliveInterval=15 -J ${{ secrets.GATEWAY_USER }}@${{ secrets.GATEWAY_HOST }} ${{ secrets.TARGET_USER }}@${{ secrets.TARGET_HOST }} | |
- name: Test SSH tunnel | |
run: ssh -v -p 8157 ${{ secrets.TARGET_USER }}@localhost "echo Connected to target host through tunnel" | |
- name: Create .password file | |
run: | | |
echo "${{ secrets.ANSIBLE_VAULT_PASSWORD }}" > ~/.ssh/.password | |
chmod 600 ~/.ssh/.password | |
- name: Configure AWS credentials | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
run: | | |
mkdir -p ~/.aws | |
echo "[default]" > ~/.aws/credentials | |
echo "aws_access_key_id=${{ secrets.AWS_ACCESS_KEY_ID }}" >> ~/.aws/credentials | |
echo "aws_secret_access_key=${{ secrets.AWS_SECRET_ACCESS_KEY }}" >> ~/.aws/credentials | |
- name: Log in to ECR | |
run: | | |
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 100225593120.dkr.ecr.us-east-1.amazonaws.com | |
- name: Cache Docker layers | |
uses: actions/cache@v3 | |
with: | |
path: /tmp/.docker-cache | |
key: ${{ runner.os }}-docker-${{ github.sha }} | |
restore-keys: | | |
${{ runner.os }}-docker- | |
- name: Load Docker cache | |
run: | | |
if [ -d "/tmp/.docker-cache" ]; then | |
mkdir -p ~/.docker | |
cp /tmp/.docker-cache/* ~/.docker/ -r | |
fi | |
- name: Pull Ansible Docker Image | |
run: docker pull 100225593120.dkr.ecr.us-east-1.amazonaws.com/agr_ansible_run:stage | |
- name: Save Docker cache | |
run: | | |
mkdir -p /tmp/.docker-cache | |
cp ~/.docker/* /tmp/.docker-cache/ -r | |
- name: Test Docker Container | |
run: | | |
docker run --rm \ | |
--mount type=bind,source=${{ github.workspace }},target=/usr/src/ansible \ | |
--mount type=bind,source=$HOME/.ssh/.password,target=/usr/src/ansible/.password \ | |
100225593120.dkr.ecr.us-east-1.amazonaws.com/agr_ansible_run:stage \ | |
/bin/bash -c "echo 'Hello, World!'" | |
- name: Confirm Connection to Final Server | |
run: ssh -v target_host "touch ~/github_action_test_file" |