This guide will demonstrate:
- Login into any app having 2FA or third party dependencies and generate and save a session locally
- Reuse that session in CI securely
First, create your environment file and generate an encryption key:
# Copy the example environment file
cp .env.example .env
# Generate a secure encryption key and replace the placeholder in .env file
ENCRYPT_KEY=$(openssl rand -base64 32)
sed -i "" "s/ENCRYPT_SECRET_KEY=your-base64-encryption-key-here/ENCRYPT_SECRET_KEY=$ENCRYPT_KEY/" .envNow edit your .env file and fill in your Sigma Computing credentials:
SIGMA_USERNAME: Your Sigma Computing emailSIGMA_PASSWORD: Your Sigma Computing passwordENCRYPT_SECRET_KEY: (already generated above)
Then install dependencies and run the login test locally to authenticate and save your session in storageState.json file:
# Install all dependencies
npm install
# Run the login test with browser visible (chromium only)
npx playwright test sigma-login.spec.js --project=chromium --headedNote: The .env and storageState.json files should never be committed since they contain your secrets
This test will:
- Navigate to the Sigma Computing login page
- Use credentials from your
.envfile - Pause for manual 2FA entry
- Save the authenticated session to
storageState.json
After obtaining your storageState.json file, encrypt it for secure storage in version control using the environment variable:
# Load environment variables and encrypt the storage state
source .env
echo -n "$ENCRYPT_SECRET_KEY" | openssl enc -aes-256-cbc -pbkdf2 -in storageState.json -out storageState.json.enc -pass stdin
# Commit the encrypted file to the repository
git add storageState.json.enc
git commit -m "Add encrypted storage state file"- Copy the
ENCRYPT_SECRET_KEYvalue from your.envfile - Go to your GitHub repository Settings > Secrets and variables > Actions
- Create a new repository secret named:
STORAGE_STATE_KEY - Paste your encryption key as the value (the same value as
ENCRYPT_SECRET_KEYfrom your.envfile)
The GitHub Actions workflow (.github/workflows/playwright.yml) automatically:
-
Decrypts the storage state using the secret key:
- name: Decrypt and create storage state run: | echo "${{ secrets.STORAGE_STATE_KEY }}" | openssl enc -d -aes-256-cbc -pbkdf2 -in storageState.json.enc -out storageState.json -pass stdin
-
Runs tests with the restored session:
- name: Run Playwright tests run: npx playwright test sigma-workbook.spec.js --project=chromium
To see this in action, trigger the GitHub Actions workflow:
- Go to the Actions tab in your GitHub repository
- Select "Playwright Tests" workflow
- Click "Run workflow" button
- The test will run using the decrypted session state, bypassing login requirements
The workflow will automatically reuse the saved authentication session, making your tests faster and more reliable in CI environments.